-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
162 lines (139 loc) · 4.15 KB
/
index.js
File metadata and controls
162 lines (139 loc) · 4.15 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
function appendAndDelete(s, t, k) {
// Write your code here
let sSplit = s.split("")
let tSplit = t.split("")
let toAdd = []
let beginCollation = false
for (let i = 0; i <= k; i++) {
const bIdx = (k - 1) - i
if (s[0] === t[0] && s[s.length - 1] !== t[t.length - 1]) {
if (sSplit[i + 1] === tSplit[i + 1] && tSplit[i + 1] !== undefined) {
sSplit.pop()
}
if (sSplit[i] === undefined) {
sSplit.push(t[i])
}
if (s[i] !== t[i]) {
beginCollation = true
}
}
if (beginCollation) {
toAdd.push(t[i])
}
// if t first character does not equal s first character
// push all the characters of tsplit to add because
// we're deleting all of s and putting all of t
if (s[0] !== t[0] && i < t.length) {
sSplit.pop()
toAdd.push(tSplit[i])
continue
}
if (s[0] !== t[0] && i >= t.length) {
const idx = i - t.length
sSplit.push(toAdd[idx])
}
sSplit = sSplit.filter(ch => ch !== undefined)
console.log("sSplit", sSplit)
// if it's the last index and characters match return "YES"
if (i === k && sSplit.join("") === tSplit.join("")) {
return "Yes"
}
// if it's the last index and characters don't match return "NO"
if (i === k && sSplit.join("") !== tSplit.join("")) {
return "No"
}
}
}
// console.log(appendAndDelete("aba", "aba", 7))
// console.log(appendAndDelete("ashley", "ash", 2))
// console.log(appendAndDelete("hackerhappy", "hackerrank", 9))
// console.log(appendAndDelete("abc", "def", 6))
function divisibleSumPairs(n, k, ar) {
// Write your code here
const pairs = [], memo = {}
for (let i = 0; i < ar.length; i++) {
const aPair = ar[i]
for (let j = i + 1; j < ar.length; j++) {
const bPair = ar[j]
const modulo = (aPair + bPair) % k
if (modulo === 0) {
const sorted = [aPair, bPair].sort((a, b) => a - b)
// const str = sorted.join(",")
// if (str in memo) continue
// memo[str] = pairs.push(sorted)
pairs.push(sorted)
}
}
}
return pairs.length
}
// console.log(divisibleSumPairs(6, 3, [1, 3, 2, 6, 1, 2]))
function climbingLeaderboard2(ranked, player) {
// Write your code here
let posMap = {}
const posList = []
const scoresToPos = scores => {
const set = [...new Set(scores)].sort((a, b) => b - a)
set.map((score, idx) => posMap[score] = idx + 1)
return posMap
}
const start = Date.now()
while (player.length > 0) {
const currentScore = player.shift()
const temp = [...ranked, currentScore]
scoresToPos(temp)
posList.push(posMap[currentScore])
posMap = {}
}
console.log(Date.now() - start)
return posList
}
function climbingLeaderboard(ranked, player) {
const positions = []
const set = [...new Set(ranked)]
const start = Date.now()
for (let score of player) {
const pos = explorePosition(set, set.length + 1, score)
positions.push(pos)
}
console.log("TimeLapse", Date.now() - start)
return positions
}
const explorePosition = (set, position, score, memo={}) => {
for (score in memo) return memo[score]
if (position === 0) return 1
if (score < set[position - 1]) return position + 1
--position
memo[score] = explorePosition(set, position, score)
return memo[score]
}
function climbingLeaderboard3(ranked, player) {
const set = [...new Set(ranked)]
const map = {}
set.forEach((num, idx) => map[num] = idx + 1)
let low = 0
let high = ranked.length - 1
let item = player[2]
const positions = []
// todo use binary search
while (low <= high) {
console.log(ranked, [low, high])
const mid = Math.floor((low + high) / 2)
const guess = ranked[mid]
if (guess === item) {
return mid
}
if (guess > item) {
high = mid - 1
} else {
low = mid + 1
}
}
return map
}
console.log(climbingLeaderboard3([100,90,90,80], [70,80,90,105]))
// console.log(climbingLeaderboard3([100,100,50,40,40,20,10], [5,25,50,120]))
// console.log(climbingLeaderboard3([100,90,90,80,75,60], [50,65,77,90,102]))
// ranking = ranking.split(" ")
// player = player.split(" ")
// console.log(climbingLeaderboard2(ranking, player))