인프런 음수가 있는 부분수열
문제

나의 풀이
import java.util.HashMap;
public class NegativeSubsequence {
public static int solution(int[] nums, int m) {
int answer = 0;
HashMap<Integer, Integer> nh = new HashMap<>();
int sum = 0;
nh.put(0, 1);
for(int x : nums) {
sum += x;
if(nh.containsKey(sum - m)) {
answer += nh.get(sum - m);
}
nh.put(sum, nh.getOrDefault(sum, 0) + 1);
}
return answer;
}
public static void main(String[] args) {
System.out.println(NegativeSubsequence.solution(new int[]{1, 1, 2, 3, -2, 1, 2, 2, -3}, 5));
}
}
결과

인프런 회장 선거
문제

나의 풀이
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
public class Election {
public static String solution(String[] votes, int k) {
String answer = " ";
HashMap<String, HashSet<String>> voteHash = new HashMap<>();
HashMap<String, Integer> candidate = new HashMap<>();
HashMap<String, Integer> present = new HashMap<>();
for(String x : votes) {
String a = x.split(" ")[0];
String b = x.split(" ")[1];
voteHash.putIfAbsent(a, new HashSet<>());
voteHash.get(a).add(b);
candidate.put(b, candidate.getOrDefault(b, 0) + 1);
}
int max = Integer.MIN_VALUE;
for(String a : voteHash.keySet()) {
int count = 0;
for(String b : voteHash.get(a)) {
if(candidate.get(b) >= k) {
count++;
}
present.put(a, count);
max = Math.max(max, count);
}
}
ArrayList<String> tmp = new ArrayList<>();
for(String name : present.keySet()) {
if(present.get(name) == max) {
tmp.add(name);
}
}
tmp.sort((a, b) -> a.compareTo(b));
answer = tmp.get(0);
return answer;
}
public static void main(String[] args) {
System.out.println(Election.solution(new String[]{"john tom", "daniel luis", "john luis", "luis tom", "daniel tom", "luis john"}, 2));
}
}
결과

좋은 글 잘 읽었습니다, 감사합니다.