Last active
April 12, 2023 02:29
-
-
Save jc3wrld999/442cdd27e0effe32620c00d473499e64 to your computer and use it in GitHub Desktop.
This file contains 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
import java.util.*; | |
public int solution(int k, int[] tangerine) { | |
int answer = 0; | |
HashMap<Integer,Integer> map =new HashMap<>(); | |
for (int t : tangerine) { | |
map.put(t, map.getOrDefault(t, 0) + 1); | |
} | |
List<Integer> list = new ArrayList<>(map.keySet()); | |
list.sort((o1, o2) -> map.get(o2)-map.get(o1)); | |
for(Integer key:list){ | |
k -=map.get(key); | |
answer++; | |
if(k<=0){ | |
break; | |
} | |
} | |
return answer; | |
} | |
/** | |
다른 풀이 | |
**/ | |
import java.util.*; | |
class Solution { | |
public int solution(int k, int[] tangerine) { | |
int answer = 0; | |
HashMap<Integer,Integer> map =new HashMap<>(); | |
for (int t : tangerine) { | |
map.put(t, map.getOrDefault(t, 0) + 1); | |
} | |
List<Integer> list = new ArrayList<>(map.keySet()); | |
list.sort((o1, o2) -> map.get(o2)-map.get(o1)); | |
for(Integer key:list){ | |
k -=map.get(key); | |
answer++; | |
if(k<=0){ | |
break; | |
} | |
} | |
return answer; | |
} | |
} |
This file contains 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
import java.util.*; | |
import java.util.stream.*; | |
class Solution { | |
public int solution(int[] elements) { | |
int[] newElements = new int[elements.length * 2]; | |
for(int i = 0; i < elements.length; i++) { | |
newElements[i] = newElements[i + elements.length] = elements[i]; | |
} | |
Set<Integer> set = new HashSet<>(); | |
for(int i = 1; i <= elements.length; i++) { | |
for(int j = 0; j < elements.length; j++) { | |
set.add(Arrays.stream(newElements, j, j+i).sum()); | |
} | |
} | |
return set.size(); | |
} | |
} |
This file contains 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
import java.util.Collections; | |
import java.util.PriorityQueue; | |
class Solution { | |
public int[] solution(String[] arguments) { | |
int[] answer = {0,0}; | |
PriorityQueue<Integer> pq = new PriorityQueue<Integer>(); | |
PriorityQueue<Integer> reverse_pq = new PriorityQueue<Integer>(Collections.reverseOrder()); | |
for(int i=0; i<arguments.length; i++) { | |
String temp[] = arguments[i].split(" "); | |
switch(temp[0]) { | |
case "I" : | |
pq.add(Integer.parseInt(temp[1])); | |
reverse_pq.add(Integer.parseInt(temp[1])); | |
break; | |
case "D" : | |
if(pq.size() > 0) { | |
if(Integer.parseInt(temp[1]) == 1) { | |
// 최댓값 삭제 | |
int max = reverse_pq.poll(); | |
pq.remove(max); | |
} else { | |
// 최솟값 삭제 | |
int min = pq.poll(); | |
reverse_pq.remove(min); | |
} | |
} | |
break; | |
} | |
} | |
if(pq.size() >= 2) { | |
answer[0] = reverse_pq.poll(); | |
answer[1] = pq.poll(); | |
} | |
System.out.println(answer[0] + ":" + answer[1]); | |
return answer; | |
} | |
} |
This file contains 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
import java.util.Arrays; | |
class Solution { | |
public boolean solution(String[] phoneBook) { | |
// 1. phoneBook을 sorting한다. | |
Arrays.sort(phoneBook); | |
// 2. 1중 Loop을 돌며 앞 번호가 뒷 번호의 접두어인지 확인한다. | |
for (int i = 0; i < phoneBook.length - 1; i++) | |
if (phoneBook[i + 1].startsWith(phoneBook[i])) | |
return false; | |
// 3. 여기까지 오면 접두어가 없다는 것이다. | |
return true; | |
} | |
} | |
import java.util.HashMap; | |
import java.util.Map; | |
class Solution { | |
public boolean solution(String[] phoneBook) { | |
// 1. HashMap을 선언한다. | |
Map<String, Integer> map = new HashMap<>(); | |
// 2. 모든 전화번호를 HashMap에 넣는다. | |
for (int i = 0; i < phoneBook.length; i++) | |
map.put(phoneBook[i], i); | |
// 3. 모든 전화번호의 substring이 HashMap에 존재하는지 확인한다. | |
for (int i = 0; i < phoneBook.length; i++) | |
for (int j = 0; j < phoneBook[i].length(); j++) | |
if (map.containsKey(phoneBook[i].substring(0, j))) | |
return false; | |
return true; | |
} | |
} |
This file contains 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
import java.util.*; | |
class Solution { | |
public int solution(int[][] triangle) { | |
for (int i = 1; i < triangle.length; i++) { | |
triangle[i][0] += triangle[i-1][0]; | |
triangle[i][i] += triangle[i-1][i-1]; | |
for (int j = 1; j < i; j++) | |
triangle[i][j] += Math.max(triangle[i-1][j-1], triangle[i-1][j]); | |
} | |
return Arrays.stream(triangle[triangle.length-1]).max().getAsInt(); | |
} | |
} |
This file contains 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
/** | |
프로그래머스 타겟 넘버 | |
dfs 활용 | |
**/ | |
import java.util.ArrayList; | |
class Solution { | |
private int res = 0; | |
public void dfs(int i, int total, int target, int[] n) { | |
if(total == target && i == n.length){ | |
res += 1; | |
return; | |
} | |
if (i >= n.length) { | |
return; | |
} | |
// 더하는 선택 | |
dfs(i + 1, total + n[i], target, n); | |
// 빼는 선택 | |
dfs(i + 1, total - n[i], target, n); | |
} | |
public int solution(int[] n, int target) { | |
dfs(0, 0, target, n); | |
return res; | |
} | |
} | |
/** | |
다른 사람 정답 | |
**/ | |
class Solution { | |
public int solution(int[] numbers, int target) { | |
int answer = 0; | |
answer = dfs(numbers, 0, 0, target); | |
return answer; | |
} | |
int dfs(int[] numbers, int n, int sum, int target) { | |
if(n == numbers.length) { | |
if(sum == target) { | |
return 1; | |
} | |
return 0; | |
} | |
return dfs(numbers, n + 1, sum + numbers[n], target) + dfs(numbers, n + 1, sum - numbers[n], target); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment