Skip to content
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

Comparator Sorting #11

Open
WonYong-Jang opened this issue Jul 14, 2018 · 2 comments
Open

Comparator Sorting #11

WonYong-Jang opened this issue Jul 14, 2018 · 2 comments

Comments

@WonYong-Jang
Copy link
Owner

WonYong-Jang commented Jul 14, 2018

2018-07-14 2 04 34

백준 10825 국영수 문제 중 일부 소스

@WonYong-Jang
Copy link
Owner Author

WonYong-Jang commented Jan 30, 2020

leetcode 1029 Two City Schedule

메소드 안에서 선언 후 사용

int[][] costs ==> 2차원 배열 sorting 
===> ( costs[i][0] 과 costs[i][1] 과의 절대값 차이를 기준으로 내림차순! )

Comparator<int[]> mySort = new Comparator<int[]>() {
            
            public int compare(int[] a, int[] b) {
                return Math.abs(b[0]-b[1]) - Math.abs(a[0]-a[1]);
            }
        };
Arrays.sort(costs, mySort);
List<Integer> list = new ArrayList<>(map.keySet());
        
        Comparator<Integer> mySort = new Comparator<Integer>() {
            @Override
            public int compare(Integer a, Integer b) {
                return map.get(b) - map.get(a);
            }  
        };
Collections.sort(list, mySort);
List<Map.Entry<Integer, Integer>> list = new ArrayList<>(map.entrySet());
        Collections.sort(list, new mySort());

public class mySort implements Comparator<Map.Entry<Integer, Integer>> {
        public int compare(Map.Entry<Integer, Integer> a, Map.Entry<Integer, Integer> b) {
            
            if(a.getValue() == b.getValue()) return a.getKey() - b.getKey();
            else return a.getValue() - b.getValue();
        }
    }

람다 표현

Arrays.sort(costs, (a, b) -> {
            return Math.abs(b[0]-b[1]) - Math.abs(a[0]-a[1]);
        });

leetcode 406 Queue reconstruction by height

  • ( 쏘팅 + 자료구조 !!!!!!!!!!!!!!!)

ArrayList<int[]> arr = new ArrayList<>();
==> arr.add(index, value); // 해당 index에 value 삽입하는데 값이 있다면 전부 오른쪽으로 밀고 삽입!!

class Solution {
    public int[][] reconstructQueue(int[][] people) {
        
        Arrays.sort(people, (a,b) -> {
            return a[0] == b[0] ? a[1] - b[1] : b[0] - a[0];
        });
            
        ArrayList<int[]> arr = new ArrayList<>();
        
        for(int[] cur : people)
        {
            arr.add(cur[1], cur); // 주의
        }
        
        return arr.toArray(new int[people.length][2]);
    }
}

@WonYong-Jang
Copy link
Owner Author

179. Largest Number

Comparator 변형 !

  • 각 숫자를 더해봐서 더큰 값으로 정렬!!!
 public class mySort implements Comparator<String> {
        public int compare(String a, String b) {
            String ab = a + b;
            String ba = b + a;
            return ba.compareTo(ab);
        }
    }

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

No branches or pull requests

1 participant