알고리즘 전 정리

임지원·2024년 6월 28일

`public static class

lcs

  • 같으면 대각선 전것 +1 / 아니면 옆 아래 중 큰 값
if (str1.charAt(i - 1) == str2.charAt(j - 1)) {
	dp[i][j] = dp[i - 1][j - 1] + 1;
}
else {
	dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
}

sort 재정의

 Arrays.sort(files, new Comparator<String> () {
           @Override
            public int compare(String o1, String o2) {
                String head1 = o1.split("[0-9]")[0];
                String head2 = o2.split("[0-9]")[0];
                
                int result = head1.toLowerCase().compareTo(head2.toLowerCase());
                //head가 같을경우 
                if(result == 0) {
                    result = compareNumber(o1, head1) - compareNumber(o2, head2);
                }
                return result;
            }
Collections.sort(people, new Comparator<Person>() {
            @Override
            public int compare(Person p1, Person p2) {
                return Integer.compare(p1.age, p2.age);
            }
        });

HashMap

  • put 넣기
  • get 가져오기
  • containsKey() 있는지 확인
Map<String, Integer> map = new HashMap<>();

for (int i = 0; i < participant.length; i++) {
            if (map.containsKey(participant[i])) {
                map.put(participant[i], map.get(participant[i]) + 1);
            } else {
                map.put(participant[i], 1);
            }
        }
        
        
for (Map.Entry<String, Integer> entry : map.entrySet()) {
      String key = entry.getKey();
      Integer value = entry.getValue();
}

priority queue

PriorityQueue<Integer> minHeap = new PriorityQueue<>();
PriorityQueue<Integer> minHeap = new PriorityQueue<?>(Collections.reverseOrder()); // 내림차순

minHeap.add(i); // 넣기
minHeap.peek(); // 값 확인
minHeap.poll(); // 빼기
profile
백엔드 새싹

0개의 댓글