iris1230xs il y a 5 ans
commit
4abf88440b

+ 3 - 0
.gitignore

@@ -0,0 +1,3 @@
+.idea
+out
+*.iml

+ 46 - 0
src/main/java/com/xs/leetcode/editor/en/AggressiveCows.java

@@ -0,0 +1,46 @@
+package com.xs.leetcode.editor.en;
+
+import java.util.Arrays;
+import java.util.Scanner;
+
+public class AggressiveCows {
+    private boolean isOk(int N, int C, int[] X, int d) {
+        int cnt = 1;
+        int i = 0;
+        for (int j = i + 1; j < N; j++) {
+            if (X[j] >= X[i] + d) {
+              cnt++;
+              i = j;
+            }
+        }
+        if (cnt >= C) return true;
+        return false;
+    }
+    private int aggressiveCows(int N, int C, int[] X) {
+        Arrays.sort(X);
+        int min = 1;
+        int max = (X[N - 1] - X[0]) / (C - 1);
+        int beg = min;
+        int end = max + 1;
+        int mid;
+        while (beg < end - 1) {
+            mid = beg + (end - beg) / 2;
+            if (isOk(N, C, X, mid)) beg = mid;
+            else end = mid;
+        }
+        return beg;
+    }
+
+    public static void main(String[] args) {
+        Scanner scanner = new Scanner(System.in);
+        int N = scanner.nextInt();
+        int C = scanner.nextInt();
+        int[] X = new int[N];
+        for (int i = 0; i < N; i++) {
+            X[i] = scanner.nextInt();
+        }
+        AggressiveCows aggressiveCows = new AggressiveCows();
+        int res = aggressiveCows.aggressiveCows(N, C, X);
+        System.out.println(res);
+    }
+}

+ 56 - 0
src/main/java/com/xs/leetcode/editor/en/GrayCode.java

@@ -0,0 +1,56 @@
+//The gray code is a binary numeral system where two successive values differ in
+// only one bit. 
+//
+// Given a non-negative integer n representing the total number of bits in the c
+//ode, print the sequence of gray code. A gray code sequence must begin with 0. 
+//
+// Example 1: 
+//
+// 
+//Input: 2
+//Output: [0,1,3,2]
+//Explanation:
+//00 - 0
+//01 - 1
+//11 - 3
+//10 - 2
+//
+//For a given n, a gray code sequence may not be uniquely defined.
+//For example, [0,2,3,1] is also a valid gray code sequence.
+//
+//00 - 0
+//10 - 2
+//11 - 3
+//01 - 1
+// 
+//
+// Example 2: 
+//
+// 
+//Input: 0
+//Output: [0]
+//Explanation: We define the gray code sequence to begin with 0.
+//             A gray code sequence of n has size = 2n, which for n = 0 the size
+// is 20 = 1.
+//             Therefore, for n = 0 the gray code sequence is [0].
+// 
+// Related Topics Backtracking
+
+  
+package com.xs.leetcode.editor.en;
+
+import java.util.List;
+
+public class GrayCode{
+    public static void main(String[] args) {
+         Solution solution = new GrayCode().new Solution();
+    }
+    //leetcode submit region begin(Prohibit modification and deletion)
+class Solution {
+    public List<Integer> grayCode(int n) {
+        return null;
+    }
+}
+//leetcode submit region end(Prohibit modification and deletion)
+
+}

+ 70 - 0
src/main/java/com/xs/leetcode/editor/en/MergeSort.java

@@ -0,0 +1,70 @@
+package com.xs.leetcode.editor.en;
+
+import java.util.Optional;
+
+public class MergeSort {
+    private static int[] tmp;
+
+    private void merge(int[] nums, int beg, int mid, int end) {
+        int i = beg, j = mid + 1, k = beg;
+        while (k <= end) {
+            if(j > end || i <= mid && nums[i] <= nums[j]) {
+                tmp[k] = nums[i];
+                i++;
+            } else {
+                tmp[k] = nums[j];
+                j++;
+            }
+            k++;
+        }
+        System.arraycopy(tmp, beg, nums, beg, end - beg + 1);
+    }
+
+    private void mergeSort(int[] nums, int beg, int end) {
+        if (beg >= end) return;
+        int mid = beg + (end - beg) / 2;
+        mergeSort(nums, beg, mid);
+        mergeSort(nums, mid + 1, end);
+        merge(nums, beg, mid, end);
+    }
+
+    private void print(int[] nums) {
+        if (nums == null) {
+            System.out.println("the array is null");
+            return;
+        }
+        for (int i : nums) {
+            System.out.print(i + " ");
+        }
+        System.out.println();
+    }
+
+    public void mergeSort(int[] nums) {
+        if (nums == null) return;
+        tmp = new int[nums.length];
+        mergeSort(nums, 0, nums.length - 1);
+    }
+
+    public static void main(String[] args) {
+        MergeSort mergeSort = new MergeSort();
+        int[] nums = new int[]{4,1,3,2};
+        mergeSort.mergeSort(nums);
+        mergeSort.print(nums);
+
+        nums = new int[]{};
+        mergeSort.mergeSort(nums);
+        mergeSort.print(nums);
+
+        nums = new int[]{0};
+        mergeSort.mergeSort(nums);
+        mergeSort.print(nums);
+
+        nums = null;
+        mergeSort.mergeSort(nums);
+        mergeSort.print(nums);
+
+        nums = new int[]{5,5,5,2,7,8,9,0,1,2,3,4,3,2,1};
+        mergeSort.mergeSort(nums);
+        mergeSort.print(nums);
+    }
+}

Fichier diff supprimé car celui-ci est trop grand
+ 0 - 0
src/main/java/com/xs/leetcode/editor/en/all.json


+ 6 - 0
src/main/java/com/xs/leetcode/editor/en/input.txt

@@ -0,0 +1,6 @@
+5 3
+1
+2
+8
+4
+9

Certains fichiers n'ont pas été affichés car il y a eu trop de fichiers modifiés dans ce diff