aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--magnetic_force_2_balls.c43
-rw-r--r--magnetic_force_2_balls.cpp17
-rw-r--r--magnetic_force_2_balls.py45
3 files changed, 102 insertions, 3 deletions
diff --git a/magnetic_force_2_balls.c b/magnetic_force_2_balls.c
new file mode 100644
index 0000000..4a408b1
--- /dev/null
+++ b/magnetic_force_2_balls.c
@@ -0,0 +1,43 @@
+// 1552. Magnetic Force Between Two Balls
+#include "leetcode.h"
+
+/*
+ * in the universe earth c-137 rick discovered a special form of magnetic force
+ * between two balls if they are put in his new invented basket. rick has 'n'
+ * empty baskets the i'th basket is at 'position[i]', more has 'm' ball and
+ * needs to distribute the balls into the baskets such that the minimum magnetic
+ * force between any two balls is maximum. rick stated that magnetic force
+ * betweeen two different balls at positions 'x' and 'y' is '|x - y|'. given the
+ * integer array 'position' and the integer 'm', return the required force.
+ */
+
+int cmp(const void *a, const void *b) { return *(int *)a - *(int *)b; }
+
+int cnt_ball(int *position, int positionSize, int distance) {
+ int cnt = 1, ptr = position[0];
+ for (int i = 1; i < positionSize; i++)
+ if (position[i] - ptr >= distance) {
+ ptr = position[i];
+ cnt++;
+ }
+ return cnt;
+}
+
+int maxDistance(int *position, int positionSize, int m) {
+ qsort(position, positionSize, sizeof(int), cmp);
+ int left = 1, right = position[positionSize - 1], mid;
+ while (left + 1 < right) {
+ mid = (left + right) / 2;
+ if (cnt_ball(position, positionSize, mid) >= m)
+ left = mid;
+ else
+ right = mid - 1;
+ }
+ return cnt_ball(position, positionSize, right) >= m ? right : left;
+}
+
+int main() {
+ int p1[] = {1, 2, 3, 4, 7}, p2[] = {5, 4, 3, 2, 1, 1000000000};
+ printf("%d\n", maxDistance(p1, ARRAY_SIZE(p1), 3)); // expect: 3
+ printf("%d\n", maxDistance(p2, ARRAY_SIZE(p2), 2)); // expect: 9999999
+}
diff --git a/magnetic_force_2_balls.cpp b/magnetic_force_2_balls.cpp
index af40160..8416deb 100644
--- a/magnetic_force_2_balls.cpp
+++ b/magnetic_force_2_balls.cpp
@@ -1,5 +1,16 @@
+// 1552. Magnetic Force Between Two Balls
#include "leetcode.h"
+/*
+ * in the universe earth c-137 rick discovered a special form of magnetic force
+ * between two balls if they are put in his new invented basket. rick has 'n'
+ * empty baskets the i'th basket is at 'position[i]', more has 'm' ball and
+ * needs to distribute the balls into the baskets such that the minimum magnetic
+ * force between any two balls is maximum. rick stated that magnetic force
+ * betweeen two different balls at positions 'x' and 'y' is '|x - y|'. given the
+ * integer array 'position' and the integer 'm', return the required force.
+ */
+
class Solution {
public:
int maxDistance(vector<int> &position, int m) {
@@ -29,7 +40,7 @@ private:
int main() {
Solution obj;
- vector<int> position = {1, 2, 3, 4, 7};
- int m = 3;
- cout << obj.maxDistance(position, m);
+ vector<int> p1 = {1, 2, 3, 4, 7}, p2 = {5, 4, 3, 2, 1, 1000000000};
+ printf("%d\n", obj.maxDistance(p1, 3)); // expect: 3
+ printf("%d\n", obj.maxDistance(p2, 2)); // expect: 999999999
}
diff --git a/magnetic_force_2_balls.py b/magnetic_force_2_balls.py
new file mode 100644
index 0000000..c206750
--- /dev/null
+++ b/magnetic_force_2_balls.py
@@ -0,0 +1,45 @@
+# 1552. Magnetic Force Between Two Balls
+
+"""
+in the universe earth c-137 rick discovered a special form of magnetic force
+between two balls if they are put in his new invented basket. rick has 'n'
+empty baskets the i'th basket is at 'position[i]', more has 'm' ball and
+needs to distribute the balls into the baskets such that the minimum magnetic
+force between any two balls is maximum. rick stated that magnetic force
+betweeen two different balls at positions 'x' and 'y' is '|x - y|'. given the
+integer array 'position' and the integer 'm', return the required force.
+"""
+
+
+class Solution(object):
+ def check(self, position, m, mid):
+ cnt, prev = 1, position[0]
+ for i in range(1, len(position)):
+ if position[i] - prev >= mid:
+ cnt += 1
+ prev = position[i]
+ return cnt >= m
+
+ def maxDistance(self, position, m):
+ """
+ :type position: List[int]
+ :type m: int
+ :rtype: int
+ """
+ position.sort()
+ left, right = 1, position[-1] - position[0]
+ while right - left > 1:
+ mid = (left + right) // 2
+ if self.check(position, m, mid):
+ left = mid
+ else:
+ right = mid - 1
+ if self.check(position, m, right):
+ return right
+ return left
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.maxDistance(position=[1, 2, 3, 4, 7], m=3))
+ print(obj.maxDistance(position=[5, 4, 3, 2, 1, 1000000000], m=2))