-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsortByHeight.R
More file actions
executable file
·84 lines (79 loc) · 2.44 KB
/
sortByHeight.R
File metadata and controls
executable file
·84 lines (79 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# Some people are standing in a row in a park. There are trees between them which cannot be moved. Your task is to rearrange the people by their heights in a non-descending order without moving the trees. People can be very tall!
#
# Example
#
# For a = [-1, 150, 190, 170, -1, -1, 160, 180], the output should be
# sortByHeight(a) = [-1, 150, 160, 170, -1, -1, 180, 190].
#
# Input/Output
#
# [execution time limit] 5 seconds (r)
#
# [input] array.integer a
#
# If a[i] = -1, then the ith position is occupied by a tree. Otherwise a[i] is the height of a person standing in the ith position.
#
# Guaranteed constraints:
# 1 ≤ a.length ≤ 1000,
# -1 ≤ a[i] ≤ 1000.
#
# [output] array.integer
# Sorted array a with all the trees untouched.
# a = list(-1, 150, 190, 170, -1, -1, 160, 180)
#comments: max pain because of the output format required. R does not have array,
#hence conversion from list etc.
library(data.table)
sortByHeight <- function(a) {
if (length(a) == 1) {
return(a)
} else {
a = as.vector(unlist(a))
a = as.data.table(a);
a[,"pos"] <- seq(1:nrow(a))
treespos = a[a == -1]
getallpeopleheights <- a[a > -1]
if (length(getallpeopleheights) > 0) {
orderpeopleinheight <- getallpeopleheights[order(a)]
newpos <- (orderpeopleinheight[,sort(pos)])
orderpeopleinheight[,pos := newpos]
a <- rbind(orderpeopleinheight,treespos)
a <- a[order(pos)]
a[,pos := NULL]
} else {
a[,pos := NULL]
}
a <- unname(a)
return(unlist(as.list(a)))
}
}
# #Solution a: using R.utils
# a = as.vector(unlist(a))
# getallpeopleheights <- a[a != -1]
# if (length(getallpeopleheights) > 0) {
# orderpeopleinheight <- getallpeopleheights[order(getallpeopleheights)]
# finalOutput <- insert(orderpeopleinheight,which(a == -1),-1)
# return(finalOutput)
# } else {
# return(a)
# }
#SolutionB: without R.utils
# a = as.vector(unlist(a))
# b = data.table(a)
# b = b[ifelse(b == -1,-1,NA) ]
# getallpeopleheights <- a[a != -1]
# if (length(getallpeopleheights) > 0) {
# orderpeopleinheight <- getallpeopleheights[order(getallpeopleheights)]
# finalOutput <- insert(orderpeopleinheight,which(a == -1),-1)
# return(finalOutput)
# } else {
# return(a)
# }
# }
# count = 1;
# check <- lapply(seq(1:length(a)), function(x) {
# if (a[x] == -1) {
# return (a[x]) } else {
# finalValue = getallpeopleheights[count]
# count = count + 1;
# return(finalValue)
# }