Skip to content

7주차 알고리즘 문제 풀이(1) - 송헌욱 - #32

Merged
hwangheejung merged 3 commits into
mainfrom
heonuk
Aug 14, 2024
Merged

hwangheejung merged 3 commits into
mainfrom
heonuk

Conversation

@IToriginal

Copy link
Copy Markdown
Member

문제: BOJ5648. - 역원소 정렬

  • 시간복잡도: O(nlogn)
    Arrays.sort(result); 정렬은 nlogn의 시간복잡도를 가진다고 알고 있음.
  • 문자열을 StringBuilder를 활용해서 뒤집고, 뒤집은 문자는 숫자로 변환하여 처리
  • 오름차순 정렬을 위해 Arrays.sort() 메서드 사용

문제: BOJ1181 - 단어 정렬

  • 시간복잡도: O(nlogn)
    정렬의 시간 복잡도는 n log n 으로 알고 있음.
  • Set을 통해 단어의 중복을 제거하고 stream()을 활용하여 정렬하는 방식으로 처리하였다.
  • 정렬에 대한 요구사항이 주어진대로, 단어의 길이를 먼저 보고, 다음은 사전 순으로 정렬할 수 있도록 처리하였다.

문제: BOJ1822 - 차집합

  • 시간복잡도: O(nlogn)
    정렬의 시간 복잡도는 n log n 으로 알고 있음.
  • 배열A와 배열B에서 기준을 배열 A 로 잡았고, 기준에서 중복된 요소가 있을 수 있기 때문에 이를 제거하기 위해 Set으로 변환하였다.
  • 변환한 Set에서 배열B의 값을 제거해주면, 결과를 반환할 수 있었다.

@IToriginal IToriginal self-assigned this Aug 11, 2024

public static String solution(int N, String[] words) {
StringBuilder result = new StringBuilder();
Set<String> uniqueWords = new HashSet<>(List.of(words));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

중복제거를 Set을 이용해하셨네요! 이문제에서는 왜 생각을 못했는지,, 그리고 Set에서 배열을 List.of()로 바로생성해주는것을 알게되었어요👍

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

네! String 배열을 하나하나 확인하기 보다는 그냥 Set으로 바로 처리했어요! 새로운 걸 알게 되셨다니 다행입니다!

Comment on lines +17 to +25
List<String> sortedWords = uniqueWords.stream()
.sorted((a, b) -> {
if (a.length() == b.length()) { // 문자열 길이가 같은 경우
return a.compareTo(b); // 사전 순으로
} else { // 문자열 길이가 다른 경우
return Integer.compare(a.length(), b.length()); // 길이 비교
}
})
.collect(Collectors.toList());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

역시 stream과 람다식을 잘 활용해야겠다는 생각이 다시한번드네요.. 일일히 comparator내의 compare을 오버라이딩했는데, stream().sorted() 람다식으로 표현하니 가독성도 늘어나고 처리과정도 확 주는것같습니다👍

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stream을 활용하면 확실히 가독성, 코드 작성에 도움이 많이 되더라구요! 자바에서는 희망같습니다 ㅠ

long[] result = new long[n];

for (int i = 0; i < n; i++) {
String rev = new StringBuilder(strNum[i]).reverse().toString();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

StringBuilder의 reverse()메서드 사용하신게 인상깊네요😊

@hwangheejung
hwangheejung merged commit 1b0a96f into main Aug 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants