File tree Expand file tree Collapse file tree 5 files changed +99
-0
lines changed
longest-consecutive-sequence Expand file tree Collapse file tree 5 files changed +99
-0
lines changed Original file line number Diff line number Diff line change 11import java .util .*;
2+ // 개선 방향
3+ class Solution {
4+ public boolean containsDuplicate (int [] nums ) {
5+ Set duplication = new HashSet <>();
6+
7+ for (int n : nums ){
8+ if (duplication .contains (n )){
9+ return true ;
10+ }
11+ duplication .add (n );
12+ }
13+ return false ;
14+ }
15+ }
16+
17+
18+ // 초기 문제 풀이
219class Solution {
320 public boolean containsDuplicate (int [] nums ) {
421 Arrays .sort (nums );
Original file line number Diff line number Diff line change 11import java .util .*;
2+ // 2번째 풀이
3+ class Solution {
4+ public int rob (int [] nums ) {
5+ int [] dp = new int [nums .length +1 ];
6+ dp [1 ] = nums [0 ];
7+
8+ for (int i = 1 ; i < nums .length ; i ++){
9+ dp [i +1 ] = Math .max (dp [i -1 ] + nums [i ], dp [i ]);
10+ }
11+ return dp [nums .length ];
12+ }
13+ }
14+
15+
16+
17+ // 1번째 풀이
218class Solution {
319 public int rob (int [] nums ) {
420 int [] house = new int [nums .length ];
Original file line number Diff line number Diff line change 11import java .util .*;
2+ // 2번째 푼 코드
3+ class Solution {
4+ public int longestConsecutive (int [] nums ) {
5+ Set <Integer > set = new HashSet <>();
6+ for (int n : nums ) {
7+ set .add (n );
8+ }
9+
10+ int cntMax = 0 ;
11+ for (int n : set ) {
12+ if (!set .contains (n - 1 )) {
13+ int end = n ;
14+ while (set .contains (end + 1 )) {
15+ end ++;
16+ }
17+ cntMax = Math .max (cntMax , end - n + 1 );
18+ }
19+ }
20+
21+ return cntMax ;
22+ }
23+ }
224
25+ // 처음 풀어본 코드
326class Solution {
427 public int longestConsecutive (int [] nums ) {
528 Set <Integer > checkList = new HashSet <>();
Original file line number Diff line number Diff line change 11import java .util .*;
2+ // 다른 방안
3+ class Solution {
4+ public int [] topKFrequent (int [] nums , int k ) {
5+ Map <Integer ,Integer > counting = new HashMap <>();
6+
7+ for (int n : nums ){
8+ if (!counting .containsKey (n )){
9+ counting .put (n ,0 );
10+ }
11+ counting .put (n , counting .get (n )+1 );
12+ }
13+
14+ List <Map .Entry <Integer ,Integer >>countList = new LinkedList <>(counting .entrySet ());
15+ countList .sort (Map .Entry .comparingByValue (Comparator .reverseOrder ()));
16+
17+ int [] answer = countList .stream ()
18+ .limit (k )
19+ .mapToInt (Map .Entry ::getKey )
20+ .toArray ();
21+ return answer ;
22+ }
23+ }
24+
225
26+ // 초안
327class Solution {
428 public int [] topKFrequent (int [] nums , int k ) {
529 Map <Integer ,Integer > counts = new HashMap <>();
Original file line number Diff line number Diff line change 11import java .util .*;
2+
3+ // 개선안
4+ class Solution {
5+ public int [] twoSum (int [] nums , int target ) {
6+ Map <Integer ,Integer > checkNums = new HashMap <>();
7+
8+ for (int i = 0 ; i < nums .length ; i ++){
9+ if (checkNums .containsKey (target - nums [i ])){
10+ int place = checkNums .get (target - nums [i ]);
11+ return new int []{place , i };
12+ }
13+ checkNums .put (nums [i ], i );
14+ }
15+ return new int []{};
16+ }
17+ }
18+
19+
20+ // 초기 구성안
221class Solution {
322 public int [] twoSum (int [] nums , int target ) {
423 Map <Integer ,Integer > element = new HashMap <>();
You can’t perform that action at this time.
0 commit comments