File tree Expand file tree Collapse file tree 5 files changed +157
-0
lines changed
longest-consecutive-sequence Expand file tree Collapse file tree 5 files changed +157
-0
lines changed Original file line number Diff line number Diff line change 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+
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You canโt perform that action at this time.
0 commit comments