1954. Minimum Garden Perimeter to Collect Enough Apples Medium

@problem@discussion
#Math#Binary Search



1/**
2 * [1954] Minimum Garden Perimeter to Collect Enough Apples
3 *
4 * In a garden represented as an infinite 2D grid, there is an apple tree planted at every integer coordinate. The apple tree planted at an integer coordinate (i, j) has |i| + |j| apples growing on it.
5 * You will buy an axis-aligned square plot of land that is centered at (0, 0).
6 * Given an integer neededApples, return the minimum perimeter of a plot such that at least neededApples apples are inside or on the perimeter of that plot.
7 * The value of |x| is defined as:
8 * 
9 * 	x if x >= 0
10 * 	-x if x < 0
11 * 
12 *  
13 * Example 1:
14 * <img alt="" src="https://assets.leetcode.com/uploads/2019/08/30/1527_example_1_2.png" style="width: 442px; height: 449px;" />
15 * Input: neededApples = 1
16 * Output: 8
17 * Explanation: A square plot of side length 1 does not contain any apples.
18 * However, a square plot of side length 2 has 12 apples inside (as depicted in the image above).
19 * The perimeter is 2 * 4 = 8.
20 * 
21 * Example 2:
22 * 
23 * Input: neededApples = 13
24 * Output: 16
25 * 
26 * Example 3:
27 * 
28 * Input: neededApples = 1000000000
29 * Output: 5040
30 * 
31 *  
32 * Constraints:
33 * 
34 * 	1 <= neededApples <= 10^15
35 * 
36 */
37pub struct Solution {}
38
39// problem: https://leetcode.com/problems/minimum-garden-perimeter-to-collect-enough-apples/
40// discuss: https://leetcode.com/problems/minimum-garden-perimeter-to-collect-enough-apples/discuss/?currentPage=1&orderBy=most_votes&query=
41
42// submission codes start here
43
44impl Solution {
45    pub fn minimum_perimeter(needed_apples: i64) -> i64 {
46        
47    }
48}
49
50// submission codes end
51
52#[cfg(test)]
53mod tests {
54    use super::*;
55
56    #[test]
57    fn test_1954() {
58    }
59}
60


Back
© 2025 bowen.ge All Rights Reserved.