classSolution: defminStartValue(self, nums: List[int]) -> int: m = min(nums) if m >= 0: return1 left, right = 1, -m * len(nums) + 1 while left < right: medium = (left + right) // 2 if self.valid(medium, nums): right = medium else: left = medium + 1 return left defvalid(self, startValue: int, nums: List[int]) -> bool: for num in nums: startValue += num if startValue <= 0: returnFalse returnTrue
publicclassSolution { publicintMinStartValue(int[] nums) { int m = nums.Min(); if (m >= 0) { return1; } int left = 1, right = -m * nums.Length + 1; while (left < right) { int medium = (left + right) / 2; if (Valid(medium, nums)) { right = medium; } else { left = medium + 1; } } return left; }
publicboolValid(int startValue, int[] nums) { foreach (int num in nums) { startValue += num; if (startValue <= 0) { returnfalse; } } returntrue; } }
classSolution { public: intminStartValue(vector<int>& nums){ int m = *min_element(nums.begin(), nums.end()); if (m >= 0) { return1; } int left = 1, right = -m * nums.size() + 1; while (left < right) { int medium = (left + right) / 2; if (valid(medium, nums)) { right = medium; } else { left = medium + 1; } } return left; }
boolvalid(int startValue, vector<int>& nums){ for (int num : nums) { startValue += num; if (startValue <= 0) { returnfalse; } } returntrue; } };
boolvalid(int startValue, constint* nums, int numsSize) { for (int i = 0; i < numsSize; i++) { startValue += nums[i]; if (startValue <= 0) { returnfalse; } } returntrue; }
intminStartValue(int* nums, int numsSize){ int m = nums[0]; for (int i = 1; i < numsSize; i++) { m = MIN(m, nums[i]); } if (m >= 0) { return1; } int left = 1, right = -m * numsSize + 1; while (left < right) { int medium = (left + right) / 2; if (valid(medium, nums, numsSize)) { right = medium; } else { left = medium + 1; } } return left; }
funcminStartValue(nums []int)int { m := nums[0] for _, num := range nums[1:] { m = min(m, num) } return1 + sort.Search(-m*len(nums), func(startValue int)bool { startValue++ for _, num := range nums { startValue += num if startValue <= 0 { returnfalse } } returntrue }) }
funcmin(a, b int)int { if a > b { return b } return a }
var minStartValue = function(nums) { const m = _.min(nums); if (m >= 0) { return1; } let left = 1, right = -m * nums.length + 1; while (left < right) { const medium = Math.floor((left + right) / 2); if (valid(medium, nums)) { right = medium; } else { left = medium + 1; } } return left; };
constvalid = (startValue, nums) => { for (const num of nums) { startValue += num; if (startValue <= 0) { returnfalse; } } returntrue; }