From 583aed5b12364d7a6e6e690e6f1f9d25cc4de204 Mon Sep 17 00:00:00 2001 From: "rica.rdo" Date: Fri, 9 Apr 2021 18:25:00 +0900 Subject: [PATCH 1/2] Add MinOperationV1 and MinOperationV2 --- .../com/company/study/MinOperationV1.java | 59 +++++++++++++++++++ .../com/company/study/MinOperationV2.java | 38 ++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 src/main/java/com/company/study/MinOperationV1.java create mode 100644 src/main/java/com/company/study/MinOperationV2.java diff --git a/src/main/java/com/company/study/MinOperationV1.java b/src/main/java/com/company/study/MinOperationV1.java new file mode 100644 index 0000000..e33f390 --- /dev/null +++ b/src/main/java/com/company/study/MinOperationV1.java @@ -0,0 +1,59 @@ +package com.company.study; + + +/** + * TimeComplexity O(N) + */ +public class MinOperationV1 { + public int minOperations(int n) { + + // [1, 3, 5, 7] leftValue: 3, rightValue: 5 + // + // [1, 3, 5, 7, 9] leftValue: 3, righValue: 7 + int ret = 0; + int mIdx = n/2; // 2 + + + int x; //왼쪽으로 이동할 예정 -- // 2 + int y; // 오른쪽으로 이동할 예정 ++// 2 + if (n % 2 == 1) { + x = mIdx - 1; + y = mIdx + 1; + } else { + x = mIdx - 1; + y = mIdx; + } + + int leftValue = getValue(x); // + int rightValue = getValue(y); // + + while(true) { + + if(x >= 0) { + // Move left + leftValue = getValue(x--); // leftValue = 1 x = 0 -> x = -1 + } + + if (y < n) { + rightValue = getValue(y++); // rightValue = 5, y = 2 -> y = 3 + } + + while (true) { + leftValue++; // 4 + rightValue--; // 6 + ret++; // ret = 1 + if(leftValue == rightValue) { + break; + } + } + if(x < 0) { + break; + } + } + return ret; + } + + private int getValue(int idx) { + return (2 * idx) + 1; + } +} diff --git a/src/main/java/com/company/study/MinOperationV2.java b/src/main/java/com/company/study/MinOperationV2.java new file mode 100644 index 0000000..1b7a9ba --- /dev/null +++ b/src/main/java/com/company/study/MinOperationV2.java @@ -0,0 +1,38 @@ +package com.company.study; + +/** + * Time complexity O(1) + */ +public class MinOperationV2 { + + // [1 (2), 3,(2) 5] answer =2 + //[1(4), 3(2), 5, (2) 7,(4) 9] = answer=6 + //[1(6), 3(4), 5(2), 7, 9, 11, 13] 2 * 1 + 2* 2, ..... + 2 * (n/2) + + // O(n) + public int minOperations(int n) { + if (n == 1) { + return 0; + } + if (n % 2 != 0) { + return sumOdds(n/2); + } else { + return sumEvens(n/2); + } + } + + private int sumOdds(int n) { + return n * (n + 1); + } + + private int sumEvens(int n) { + return n * (n-1 ) + n; + } + + /* + 홀수: 2 6 12 20 = 시그마 (2*n) -> 2 * 시그마(n) = (n-1) * (n) + 2 4 6 8 -> a(n) = 2*n + 짝수: 1 4 9 16 -> 시그마 (2*n + 1) -> 1 + n * (n-1) + (n-1) = n * (n-1) + n + 3 5 7 9 -> a(n) = 2*n + 1 + */ +} From 104a0bcdaa344c422dcdcbca8e4d5f1f5394920e Mon Sep 17 00:00:00 2001 From: "rica.rdo" Date: Fri, 9 Apr 2021 18:27:53 +0900 Subject: [PATCH 2/2] Add minoperation package and Problem.md --- .../MinOperationV1.java | 2 +- .../MinOperationV2.java | 2 +- .../java/com/company/minoperation/Problem.md | 18 ++++++++++++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) rename src/main/java/com/company/{study => minoperation}/MinOperationV1.java (97%) rename src/main/java/com/company/{study => minoperation}/MinOperationV2.java (96%) create mode 100644 src/main/java/com/company/minoperation/Problem.md diff --git a/src/main/java/com/company/study/MinOperationV1.java b/src/main/java/com/company/minoperation/MinOperationV1.java similarity index 97% rename from src/main/java/com/company/study/MinOperationV1.java rename to src/main/java/com/company/minoperation/MinOperationV1.java index e33f390..828e869 100644 --- a/src/main/java/com/company/study/MinOperationV1.java +++ b/src/main/java/com/company/minoperation/MinOperationV1.java @@ -1,4 +1,4 @@ -package com.company.study; +package com.company.minoperation; /** diff --git a/src/main/java/com/company/study/MinOperationV2.java b/src/main/java/com/company/minoperation/MinOperationV2.java similarity index 96% rename from src/main/java/com/company/study/MinOperationV2.java rename to src/main/java/com/company/minoperation/MinOperationV2.java index 1b7a9ba..b81e450 100644 --- a/src/main/java/com/company/study/MinOperationV2.java +++ b/src/main/java/com/company/minoperation/MinOperationV2.java @@ -1,4 +1,4 @@ -package com.company.study; +package com.company.minoperation; /** * Time complexity O(1) diff --git a/src/main/java/com/company/minoperation/Problem.md b/src/main/java/com/company/minoperation/Problem.md new file mode 100644 index 0000000..9dde3a4 --- /dev/null +++ b/src/main/java/com/company/minoperation/Problem.md @@ -0,0 +1,18 @@ +# Minimum Operations to Make Array Equal + +You have an array arr of length n where arr[i] = (2 * i) + 1 for all valid values of i (i.e. 0 <= i < n). + +In one operation, you can select two indices x and y where 0 <= x, y < n and subtract 1 from arr[x] and add 1 to arr[y] (i.e. perform arr[x] -=1 and arr[y] += 1). The goal is to make all the elements of the array equal. It is guaranteed that all the elements of the array can be made equal using some operations. + +Given an integer n, the length of the array. Return the minimum number of operations needed to make all the elements of arr equal. + +``` +Input: n = 3 +Output: 2 +Explanation: arr = [1, 3, 5] +First operation choose x = 2 and y = 0, this leads arr to be [2, 3, 4] +In the second operation choose x = 2 and y = 0 again, thus arr = [3, 3, 3]. +``` + +Source: +leetcode.com