Skip to content

Latest commit

 

History

History
57 lines (47 loc) · 1.22 KB

Question_2148.md

File metadata and controls

57 lines (47 loc) · 1.22 KB

LeetCode Records - Question 2148 Count Elements With Strictly Smaller and Greater Elements

Attempt 1: Use Array.sort() and a for loop

class Solution {
    public int countElements(int[] nums) {
        Arrays.sort(nums);

        int min = nums[0];
        int max = nums[nums.length - 1];

        int count = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != min && nums[i] != max) {
                count++;
            }
        }

        return count;
    }
}
  • Runtime: 2 ms (Beats: 63.74%)
  • Memory: 41.39 MB (Beats: 88.11%)

Attempt 2: Use two for loops

class Solution {
    public int countElements(int[] nums) {
        int min = nums[0];
        int max = nums[0];

        for (int i = 1; i < nums.length; i++) {
            if (nums[i] > max) {
                max = nums[i];
            } else if (nums[i] < min) {
                min = nums[i];
            }
        }

        int count = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != min && nums[i] != max) {
                count++;
            }
        }

        return count;
    }
}
  • Runtime: 0 ms (Beats: 100.00%)
  • Memory: 41.69 MB (Beats: 50.10%)