Skip to content

Commit c97b11e

Browse files
authored
Merge pull request #1672 from renovizee/main
[renovziee] WEEK 01 Solutions
2 parents 29ef951 + d4312fb commit c97b11e

File tree

5 files changed

+176
-0
lines changed

5 files changed

+176
-0
lines changed

โ€Žcontains-duplicate/renovizee.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import java.util.HashMap;
2+
import java.util.HashSet;
3+
import java.util.Map;
4+
import java.util.Set;
5+
6+
// tag renovizee 1week
7+
// https://github.com/DaleStudy/leetcode-study/issues/217
8+
// https://leetcode.com/problems/contains-duplicate/
9+
class Solution {
10+
11+
// Solv2: hash set (feedback)
12+
public boolean containsDuplicate(int[] nums) {
13+
// ์‹œ๊ฐ„๋ณต์žก๋„ : O(n)
14+
// ๊ณต๊ฐ„๋ณต์žก๋„ : O(n)
15+
Set<Integer> numsSet = new HashSet<>();
16+
for (int num : nums) {
17+
if (numsSet.contains(num)) {
18+
return true;
19+
}
20+
numsSet.add(num);
21+
}
22+
return false;
23+
}
24+
25+
// Solv1: hash map
26+
// public boolean containsDuplicate(int[] nums) {
27+
// // ์‹œ๊ฐ„๋ณต์žก๋„ : O(n)
28+
// // ๊ณต๊ฐ„๋ณต์žก๋„ : O(n)
29+
// Map<Integer,Integer> countMap = new HashMap<>();
30+
// for (int num : nums) {
31+
// int count = countMap.getOrDefault(num, 0);
32+
// int addCount = count + 1;
33+
// countMap.put(num, addCount);
34+
// if (addCount == 2) {
35+
// return true;
36+
// }
37+
// }
38+
// return false;
39+
// }
40+
}
41+
42+
//-------------------------------------------------------------------------------------------------------------
43+
// ๊ธฐ๋ณธ ๋ฌธ๋ฒ• ํ”ผ๋“œ๋ฐฑ
44+
// 1) Map ๊ธฐ๋ณธ ๋ฌธ๋ฒ•, ~.getOrDefault()
45+
//-------------------------------------------------------------------------------------------------------------

โ€Žhouse-robber/renovizee.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
// https://github.com/DaleStudy/leetcode-study/issues/264
3+
// https://leetcode.com/problems/house-robber/
4+
class Solution {
5+
public int rob(int[] nums) {
6+
7+
return 1;
8+
}
9+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import java.util.HashSet;
2+
import java.util.Set;
3+
4+
// tag renovizee 1week unresolved
5+
// https://github.com/DaleStudy/leetcode-study/issues/240
6+
// https://leetcode.com/problems/longest-consecutive-sequence/
7+
class Solution {
8+
public int longestConsecutive(int[] nums) {
9+
// ์‹œ๊ฐ„๋ณต์žก๋„ : O(n)
10+
// ๊ณต๊ฐ„๋ณต์žก๋„ : O(n)
11+
12+
Set<Integer> numSet = new HashSet<>();
13+
for (int num : nums) {
14+
numSet.add(num);
15+
}
16+
17+
int maxCount = 0;
18+
for (int num : nums) {
19+
if (numSet.contains(num - 1)) continue;
20+
int currentCount = 1;
21+
while (numSet.contains(num + currentCount)) {
22+
currentCount++;
23+
}
24+
maxCount = Math.max(maxCount, currentCount);
25+
}
26+
return maxCount;
27+
}
28+
}
29+
30+
31+
//-------------------------------------------------------------------------------------------------------------
32+
// ๊ธฐ๋ณธ ๋ฌธ๋ฒ• ํ”ผ๋“œ๋ฐฑ
33+
// 1) Set<Integer> numSet = new HashSet<>();
34+
// 2) Math ํ™œ์šฉ Math.max(maxCount, currentCount);
35+
//-------------------------------------------------------------------------------------------------------------
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
// https://github.com/DaleStudy/leetcode-study/issues/237
3+
// https://leetcode.com/problems/top-k-frequent-elements/
4+
class Solution {
5+
public int[] topKFrequent(int[] nums, int k) {
6+
int[] result = {1, 2, 3};
7+
8+
return result;
9+
}
10+
}

โ€Žtwo-sum/renovizee.java

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import java.util.HashMap;
2+
import java.util.Map;
3+
4+
// tag renovizee 1week
5+
// https://github.com/DaleStudy/leetcode-study/issues/219
6+
// https://leetcode.com/problems/two-sum/description/
7+
8+
// #์š”๊ตฌ์‚ฌํ•ญ ์š”์•ฝ
9+
// 1. int[] nums์™€ int target์ด ์ฃผ์–ด์ง„๋‹ค.
10+
// 2. nums์˜ ๋‘ ์ˆ˜์˜ ํ•ฉ์ด target๊ณผ ๊ฐ™์€ int[] index๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค. (์ˆœ์„œ ์ƒ๊ด€ x)
11+
// 3. ๋˜‘๊ฐ™์€ ์›์†Œ๋ฅผ ๋‘๋ฒˆ ์‚ฌ์šฉํ•˜์ง€ ๋ชปํ•˜๊ณ , ์ •ํ™•ํžˆ ํ•˜๋‚˜์˜ ์ •๋‹ต๋งŒ ์žˆ๋‹ค.
12+
13+
class Solution {
14+
// Solv3: map ์ตœ์ ํ™”
15+
// ์‹œ๊ฐ„๋ณต์žก๋„ : O(n)
16+
// ๊ณต๊ฐ„๋ณต์žก๋„ : O(n)
17+
public int[] twoSum(int[] nums, int target) {
18+
Map<Integer, Integer> map = new HashMap<>();
19+
int[] result = new int[2];
20+
for (int i = 0; i < nums.length; i++) {
21+
int key = target - nums[i];
22+
if (map.containsKey(key) && map.get(key) != i) {
23+
result[0] = i;
24+
result[1] = map.get(key);
25+
}
26+
map.put(nums[i], i);
27+
}
28+
return result;
29+
}
30+
//-------------------------------------------------------------------------------------------------------------
31+
// Solv2: map
32+
// ์‹œ๊ฐ„๋ณต์žก๋„ : O(n)
33+
// ๊ณต๊ฐ„๋ณต์žก๋„ : O(n)
34+
// public int[] twoSum(int[] nums, int target) {
35+
// Map<Integer, Integer> map = new HashMap<>();
36+
// int[] result = new int[2];
37+
// for (int i = 0; i < nums.length; i++) {
38+
// map.put(nums[i], i);
39+
// }
40+
//
41+
// for (int i = 0; i < nums.length; i++) {
42+
// int key = target - nums[i];
43+
// if (map.containsKey(key) && map.get(key) != i) {
44+
// result[0] = i;
45+
// result[1] = map.get(key);
46+
// }
47+
// }
48+
// return result;
49+
//
50+
// }
51+
//-------------------------------------------------------------------------------------------------------------
52+
// Solv1: Brute Force
53+
// ์‹œ๊ฐ„๋ณต์žก๋„ : O(n^2)
54+
// ๊ณต๊ฐ„๋ณต์žก๋„ : O(1)
55+
// public int[] twoSum(int[] nums, int target) {
56+
// int size = nums.length;
57+
// for(int i = 0; i < size - 1; i++) {
58+
// for(int j = i+1; j < size; j++) {
59+
// if(target == (nums[i] + nums[j])){
60+
// return new int[]{i,j};
61+
// }
62+
// }
63+
// }
64+
// return new int[]{};
65+
// }
66+
67+
68+
// 1) ==: ๋‘ ๊ฐ’์ด ๊ฐ™์€์ง€ ๋น„๊ต. ๊ธฐ๋ณธ ํƒ€์ž…์€ ๊ฐ’์„ ๋น„๊ตํ•˜๊ณ , ์ฐธ์กฐ ํƒ€์ž…์€ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ(๋™์ผํ•œ ๊ฐ์ฒด์ธ์ง€)๋ฅผ ๋น„๊ต
69+
// ์ฐธ์กฐ ํƒ€์ž… ๊ฐ์ฒด์˜ ๋‚ด์šฉ์ด ๊ฐ™์€์ง€๋ฅผ ๋น„๊ตํ•˜๋ ค๋ฉด ์ฃผ๋กœ a.equals(b)๋ฅผ ์‚ฌ์šฉ
70+
//
71+
// 2) ์ดˆ๊ธฐํ™” ๋ฐฐ์—ด๊ณผ ๋งต
72+
// - new int[2] :size ์ดˆ๊ธฐํ™”
73+
// - new int[]{1,2,3} : ์‹ค์ œ ๊ฐ’ ์ดˆ๊ธฐํ™”
74+
// - Map<String,String> test = new HashMap<>(); ๋งต์˜ k/v ํƒ€์ž…์€ ์•ž ๋ณ€์ˆ˜์— ์„ค์ •ํ•œ๋‹ค. val ๋งŒ์‚ฌ์šฉํ•˜๋‹ค..
75+
//-------------------------------------------------------------------------------------------------------------
76+
77+
}

0 commit comments

Comments
ย (0)