From de318d5735d8c1278ab786957f0e5dd8afe2eb8c Mon Sep 17 00:00:00 2001 From: Nishant Nirmal Date: Mon, 27 Jan 2020 21:31:01 +0530 Subject: [PATCH 1/3] Check for unsorted elements in the array --- 1051-Height-Checker.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 1051-Height-Checker.py diff --git a/1051-Height-Checker.py b/1051-Height-Checker.py new file mode 100644 index 0000000..63042ad --- /dev/null +++ b/1051-Height-Checker.py @@ -0,0 +1,40 @@ +''' +Students are asked to stand in non-decreasing order of heights for an annual photo. + +Return the minimum number of students that must move in order for all students to be standing in non-decreasing order of height. + + + +Example 1: + +Input: heights = [1,1,4,2,1,3] +Output: 3 + + + +Constraints: + + 1 <= heights.length <= 100 + 1 <= heights[i] <= 100 + + +''' +class Solution: + def heightChecker(self, heights: List[int]) -> int: + if len(heights) == 1: + return 0 + arr = [i for i in heights] + res = 0 + for i in range(len(arr)-1, 0, -1): + flag = True + for j in range(i): + if arr[j]>arr[j+1]: + arr[j], arr[j+1] = arr[j+1], arr[j] + flag = False + if flag: + break + for i in range(len(heights)): + if heights[i] != arr[i]: + res += 1 + return res + From f2453fa65d11e05c1cbbeddec445cd79cf60dccc Mon Sep 17 00:00:00 2001 From: Nishant Nirmal Date: Tue, 28 Jan 2020 15:15:05 +0530 Subject: [PATCH 2/3] Check for unsorted elements in array --- 1051-Height-Checker.py | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/1051-Height-Checker.py b/1051-Height-Checker.py index 63042ad..cfffe29 100644 --- a/1051-Height-Checker.py +++ b/1051-Height-Checker.py @@ -23,18 +23,9 @@ class Solution: def heightChecker(self, heights: List[int]) -> int: if len(heights) == 1: return 0 - arr = [i for i in heights] + arr = sorted(heights) res = 0 - for i in range(len(arr)-1, 0, -1): - flag = True - for j in range(i): - if arr[j]>arr[j+1]: - arr[j], arr[j+1] = arr[j+1], arr[j] - flag = False - if flag: - break for i in range(len(heights)): if heights[i] != arr[i]: res += 1 return res - From d9b4b6ff35be8bc17dae61358f6fac3b835c1a76 Mon Sep 17 00:00:00 2001 From: Nishant Nirmal Date: Wed, 29 Jan 2020 10:28:02 +0530 Subject: [PATCH 3/3] Check for unsorted elements in array --- 1051-Height-Checker.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/1051-Height-Checker.py b/1051-Height-Checker.py index cfffe29..3993ab1 100644 --- a/1051-Height-Checker.py +++ b/1051-Height-Checker.py @@ -21,8 +21,6 @@ ''' class Solution: def heightChecker(self, heights: List[int]) -> int: - if len(heights) == 1: - return 0 arr = sorted(heights) res = 0 for i in range(len(heights)):