A school is trying to take an annual photo of all the students. The students are asked to stand in a single file line in non-decreasing order by height. Let this ordering be represented by the integer array expected where expected[i] is the expected height of the ith student in line.
You are given an integer array heights representing the current order that the students are standing in. Each heights[i] is the height of the ith student in line (0-indexed).
Return the number of indices where heights[i] != expected[i].
Example 1:
Input: heights = [1,1,4,2,1,3]
Output: 3
Explanation:
heights: [1,1,4,2,1,3]
expected: [1,1,1,2,3,4]
Indices 2, 4, and 5 do not match.
Example 2:
Input: heights = [5,1,2,3,4]
Output: 5
Explanation:
heights: [5,1,2,3,4]
expected: [1,2,3,4,5]
All indices do not match.
Example 3:
Input: heights = [1,2,3,4,5]
Output: 0
Explanation:
heights: [1,2,3,4,5]
expected: [1,2,3,4,5]
All indices match.
Constraints:
func heightChecker(heights []int) int {
if len(heights) <= 1 {
return 0
}
temp := make([]int, len(heights))
copy(temp, heights)
// for _, v := range heights {
// temp = append(temp, v)
// }
quickSort(temp, 0, len(temp)-1)
count := 0
for i := range temp {
if temp[i] != heights[i] {
count++
}
}
return count
}
func quickSort(arr []int, l, r int) {
if l < r {
p := partition(arr, l, r)
quickSort(arr, l, p-1)
quickSort(arr, p+1, r)
}
}
func partition(arr []int, l, r int) int {
pivot := arr[r] // pivot
i := (l - 1)
for j := l; j <= r-1; j++ {
if arr[j] <= pivot {
i++
arr[i], arr[j] = arr[j], arr[i]
}
}
arr[i+1], arr[r] = arr[r], arr[i+1]
return i + 1
}
https://go.dev/play/p/6b3F0PVS6Jd
func heightChecker(heights []int) int {
s := make([]int, len(heights))
i,count := 0,0
copy(s, heights)
sort.Ints(s)
for i < len(s) {
if s[i] != heights[i] {
count++
}
i++
}
return count
}
func heightChecker(heights []int) int {
var heightFreq [101]int
for _,i := range heights {
heightFreq[i]++
}
result := 0
currentHeight := 0
for i:=0; i < len(heights); i++ {
for heightFreq[currentHeight] == 0 {
currentHeight++
}
if currentHeight != heights[i] {
result++
}
heightFreq[currentHeight]--
}
return result
}
func heightChecker(heights []int) int {
heightfreq := make([]int, 101)
for _, v := range heights {
heightfreq[v]++
}
check := 0
result := 0
for _, v := range heights {
for heightfreq[check] == 0 {
check++
}
if check != v {
result++
}
heightfreq[check]--
}
return result
}
func heightChecker(heights []int) int {
var heights_sort = make([]int, len(heights))
copy(heights_sort, heights)
var result int = 0
sort.Ints(heights_sort)
for i, _ := range heights {
if heights_sort[i] != heights[i] {
result++
}
}
return result;
}
func heightChecker(heights []int) int {
freq := make([]int, 101)
for _, val := range heights {
freq[val]++
}
cur := 1
res := 0
for _, h := range heights {
for freq[cur] == 0 {
cur++
}
if cur != h {
res++
}
freq[cur]--
}
return res
}
func heightChecker(heights []int) int {
s := make([]int, len(heights))
i,count := 0,0
copy(s, heights)
sort.Ints(s)
for i < len(s) {
if s[i] != heights[i] {
count++
}
i++
}
return count
}
Array 101 Find All Numbers Disappeared in an Array in Go (0) | 2022.03.19 |
---|---|
Array 101 Third Maximum Number in go (0) | 2022.03.19 |
Array101 Remove Element (0) | 2022.03.15 |
Array 101 Sort Array By Parity (0) | 2022.03.15 |
Array 101 Move Zeroes in Go (0) | 2022.03.10 |
댓글 영역