classSolution { public: using ll = longlong; longlongrepairCars(vector<int>& ranks, int cars){ ll l = 1, r = 1ll * ranks[0] * cars * cars; auto check = [&](ll m) { ll cnt = 0; for (auto x : ranks) { cnt += sqrt(m / x); } return cnt >= cars; }; while (l < r) { ll m = l + r >> 1; if (check(m)) { r = m; } else { l = m + 1; } } return l; } };
classSolution { publiclongrepairCars(int[] ranks, int cars) { longl=1, r = 1l * ranks[0] * cars * cars; while (l < r) { longm= l + r >> 1; if (check(ranks, cars, m)) { r = m; } else { l = m + 1; } } return l; }
publicbooleancheck(int[] ranks, int cars, long m) { longcnt=0; for (int x : ranks) { cnt += (long) Math.sqrt(m / x); } return cnt >= cars; } }
publicclassSolution { publiclongRepairCars(int[] ranks, int cars) { long l = 1, r = 1l * ranks[0] * cars * cars; while (l < r) { long m = l + r >> 1; if (Check(ranks, cars, m)) { r = m; } else { l = m + 1; } } return l; }
publicboolCheck(int[] ranks, int cars, long m) { long cnt = 0; foreach (int x in ranks) { cnt += (long) Math.Sqrt(m / x); } return cnt >= cars; } }
staticboolcheck(ll m, int* ranks, int ranksSize, int cars) { ll cnt = 0; for (int i = 0; i < ranksSize; i++) { int x = ranks[i]; cnt += sqrt(m / x); } return cnt >= cars; }
longlongrepairCars(int* ranks, int ranksSize, int cars) { ll l = 1, r = 1ll * ranks[0] * cars * cars; while (l < r) { ll m = l + r >> 1; if (check(m, ranks, ranksSize, cars)) { r = m; } else { l = m + 1; } } return l; }
[sol1-Python3]
1 2 3 4 5 6 7 8 9 10 11 12
classSolution: defrepairCars(self, ranks: List[int], cars: int) -> int: l , r = 1, ranks[0] * cars * cars defcheck(m: int) -> bool: returnsum([floor(sqrt(m // x)) for x in ranks]) >= cars while l < r: m = l + r >> 1 if check(m): r = m else: l = m + 1 return l
funcrepairCars(ranks []int, cars int)int64 { l , r := 1, ranks[0] * cars * cars var check = func(m int)bool { cnt := 0 for _, x := range ranks { cnt += int(math.Sqrt(float64(m / x))) } return cnt >= cars } for l < r { m := (l + r) >> 1 if check(m) { r = m } else { l = m + 1 } } returnint64(l) }
var repairCars = function(ranks, cars) { let l = 1; let r = ranks[0] * cars * cars; constcheck = (m) => { let cnt = 0; for (const x of ranks) { cnt += Math.floor(Math.sqrt(m / x)); } return cnt >= cars; } while (l < r) { const m = Math.floor((l + r) / 2); if (check(m)) { r = m; } else { l = m + 1; } } return l; };