Easy to understand Java solution, beats 100%


  • 0
    S
    public class Solution {
        public int[] constructRectangle(int area) {
            int[] result = new int[2];
            int minDiff = Integer.MAX_VALUE;
            
            for (int i = 1; Math.sqrt(area) - i >= 0; i++) {
                if (area % i == 0) {
                    int j = area / i;
                    
                    // Update 
                    if ((j - i) < minDiff) {
                        minDiff = j - i;
                        result[1] = i;
                        result[0] = j;
                    }
                }
            }
            
            return result;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.