-
Notifications
You must be signed in to change notification settings - Fork 3
week2/nonani #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+146
−0
Merged
week2/nonani #12
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| /* | ||
| ## ✏️ [프로그래머스] 멀리 뛰기 | ||
| 📶 문제 난이도 | ||
| Lv. 2 | ||
|
|
||
| 🔗 문제 링크 | ||
| https://school.programmers.co.kr/learn/courses/30/lessons/12914?language=java | ||
|
|
||
| ⏱️ 풀이 시간 | ||
| 5분 | ||
|
|
||
| ✅ 풀이 근거 | ||
| 고민할 필요없이 점화식이 피보나치 수열이었다. | ||
| 2칸 전에 뛴 경우 + 1칸 전 뛴 경우 더해주기만 하면 되는 문제 | ||
| */ | ||
| class Solution { | ||
| public long solution(int n) { | ||
|
|
||
| int[] dp = new int[2001]; | ||
| dp[1] = 1; | ||
| dp[2] = 2; | ||
| for(int i=3;i<=n;i++) { | ||
| dp[i] = (dp[i-1] + dp[i-2]) % 1234567; | ||
| } | ||
| return dp[n]; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| /* | ||
| ## ✏️ [프로그래머스] 섬 연결하기 | ||
| 📶 문제 난이도 | ||
| Lv. 3 | ||
|
|
||
| 🔗 문제 링크 | ||
| https://school.programmers.co.kr/learn/courses/30/lessons/42861?language=java | ||
|
|
||
| ⏱️ 풀이 시간 | ||
| 30분 | ||
|
|
||
| ✅ 풀이 근거 | ||
| //MST 구하는 문제 | ||
| // Prim으로 풀어보자 | ||
| // Prim은 엣지수가 많은 상황에서 이득을 볼 수 있는 알고리즘 | ||
| // 만약 엣지수가 적은 상황에서는 크루스칼보단 성능이 떨어지는 상황이 나올 가능성이 높지만 그런 세부 지시사항은 없으니 뭘 선택하든 상관은 없을듯 | ||
| */ | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| import java.util.*; | ||
|
|
||
| class Solution { | ||
| public int solution(int n, int[][] costs) { | ||
| int answer = 0; | ||
| boolean[] visited = new boolean[n]; | ||
| int cnt = 0; // 방문한 정점 개수를 카운트, N이면 다 방문한거니까 더 진행할 필요가 없음 | ||
|
|
||
| //int[0] : 다음으로 방문할 노드 번호, int[1] : 그 노드까지의 이동하는 거리(간선 거리) | ||
| PriorityQueue<int[]> pq = new PriorityQueue<>((o1, o2) -> { | ||
| return Integer.compare(o1[1], o2[1]); | ||
| }); | ||
|
|
||
| ArrayList<int[]> graph[] = new ArrayList[n]; | ||
|
|
||
| for (int i = 0; i < n; i++) { | ||
| graph[i] = new ArrayList<int[]>(); | ||
| } | ||
|
|
||
| for (int[] cost : costs) { | ||
| int a = cost[0]; | ||
| int b = cost[1]; | ||
| int c = cost[2]; | ||
| graph[a].add(new int[]{b, c}); | ||
| graph[b].add(new int[]{a, c}); | ||
| } | ||
|
|
||
| //임의의 정점하나를 고른다. | ||
| pq.add(new int[]{0, 0}); | ||
|
|
||
| while (!pq.isEmpty() && cnt < n) { | ||
| // 현재 pq에 있는 후보 정점 중 거리가 가장 짧은 걸 고른다. | ||
| int[] node = pq.poll(); | ||
| int cur = node[0]; | ||
| int dist = node[1]; | ||
|
|
||
| if (visited[cur]) | ||
| continue; | ||
|
|
||
| visited[cur] = true; | ||
| cnt++; | ||
| answer += dist; | ||
|
|
||
| // 다음 정점 후보를 고른다. | ||
| for (int i = 0; i < graph[cur].size(); i++) { | ||
| int[] nextNode = graph[cur].get(i); | ||
| int next = nextNode[0]; | ||
| int cost = nextNode[1]; | ||
| if (visited[next]) | ||
| continue; | ||
| pq.add(new int[]{next, cost}); | ||
| } | ||
| } | ||
| return answer; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| /* | ||
| ## ✏️ [프로그래머스] 튜플 | ||
| 📶 문제 난이도 | ||
| Lv. 2 | ||
|
|
||
| 🔗 문제 링크 | ||
| https://school.programmers.co.kr/learn/courses/30/lessons/64065 | ||
|
|
||
| ⏱️ 풀이 시간 | ||
| 35분 | ||
|
|
||
| ✅ 풀이 근거 | ||
| },{ 로 split하면 좋을 것 같다. | ||
| 이후엔 길이를 기준으로 정렬한 뒤 visited set에 추가하다가 신규로 추가된 녀석을 배열에 추가는 식으로 하면 정렬도 깔끔하게 될 것 같았다. | ||
| */ | ||
|
|
||
| import java.util.*; | ||
|
|
||
| class Solution { | ||
| public int[] solution(String s) { | ||
|
|
||
| String[] subStrings = s.substring(2, s.length() - 2).split("\\},\\{"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. LGTM 💯 ! |
||
| String str = ""; | ||
| Set<Integer> visited = new HashSet<>(); | ||
| int[] answer = new int[subStrings.length]; | ||
| int index = 0; | ||
|
|
||
| Arrays.sort(subStrings, (o1, o2) -> Integer.compare(o1.length(), o2.length())); | ||
| for (String sub : subStrings) { | ||
| for (String number : sub.split(",")) { | ||
| int value = Integer.parseInt(number); | ||
|
|
||
| if (visited.add(value)) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| answer[index++] = value; | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| return answer; | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
처음에 제가 느낀 생각은 graph로 초기화하는 것이 costs의 전체 길이라면 그냥,
costs[2]로 Arraysort() 한 다음에 그냥 간선의 개수가 n - 1 이 될 때까지 visited한게 더 좋지 않을까 ? 생각했었던 것 같습니다!
뭔가 임의의 시작점에서 정해진 목표로 가는데 이용하면 최적의 효율로 찾을 수 있겠다?라고 생각이 드는데 모든 점이 연결되어야 하는 문제 상황에서 graph로 초기화해야 하나? 라는 생각입니다 ㅋㅎㅋㅎㅋ
사실 제가 틀릴 수도 있어서 아래는 두 알고리즘을 비교한, AI 답변 첨부 드립니다.
아.. 풀이 그건에 이미 알고 있었군요 죄송합니다 ......
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
싸이클 체크하는게 귀찮아서 크루스칼은 조금 꺼려지더라구요 ㅋㅋㅋ
이 문제는 입력 데이터 형식부터 크루스칼 쓰라고 의도한 문제가 맞는 것 같습니다 🤣