Page tree
Skip to end of metadata
Go to start of metadata
1 (fail)
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {

    // Complete the minimumSwaps function below.
    static int minimumSwaps(int[] arr) {
        int count = 0;
        List<Integer> swapIndex = new ArrayList<>();

        for (int index = 0; index < arr.length; index++) {
            for (int i = 1; i < arr.length; i++) {
                if (swapIndex.size() == 2) {
                    int temp = arr[swapIndex.get(0)];
                    arr[swapIndex.get(0)] = arr[swapIndex.get(1)];
                    arr[swapIndex.get(1)] = temp;
                    count++;
                    swapIndex.clear();
                    break;
                }

                if (arr[i - 1] > arr[i]) {
                    swapIndex.add(i - 1);
                    
                }
            }
        }

        return count;
    }

    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) throws IOException {
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        int n = scanner.nextInt();
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

        int[] arr = new int[n];

        String[] arrItems = scanner.nextLine().split(" ");
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

        for (int i = 0; i < n; i++) {
            int arrItem = Integer.parseInt(arrItems[i]);
            arr[i] = arrItem;
        }

        int res = minimumSwaps(arr);

        bufferedWriter.write(String.valueOf(res));
        bufferedWriter.newLine();

        bufferedWriter.close();

        scanner.close();
    }
}

1번 회고
		int count = 0;
        int pivot = 0;
		// 배열의 인덱스 + 1 값과 배열의 값은 같다. 
        for (int index = 0; index < arr.length; index++) {
            if (arr[index] != index + 1) {
                for (int i = index + 1; i < arr.length ; i++) {
                    if (arr[i] == index + 1) {
                        pivot = i;
                        break;
                    }
                }
                int temp = arr[index];
                arr[index] = arr[pivot];
                arr[pivot] = temp;
                count++;
            }
        }

        return count;


2.fail

String s = "kkkk";
        char[] chars = s.toCharArray();

        for (int i = 1; i < chars.length; i++) {

            Map<String, Integer> map = new HashMap<>();

            for (int j = 0; j <= chars.length - i; j++) {
                String key = s.substring(j, j + i);
                char[] chars1 = key.toCharArray();
                Arrays.sort(chars1);
                key = Arrays.toString(chars1);
                map.put(key, map.getOrDefault(key, 0) + 1);
            }

            for (final Map.Entry<String, Integer> stringIntegerEntry : map.entrySet()) {
                System.out.println(stringIntegerEntry.getKey() + " : " + stringIntegerEntry.getValue());
            }
        }



3.

  • No labels