Skip to content

Commit 25c4e25

Browse files
Solutions
1 parent d80c863 commit 25c4e25

File tree

8 files changed

+261
-0
lines changed

8 files changed

+261
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
impl Solution {
2+
pub fn people_aware_of_secret(n: i32, delay: i32, forget: i32) -> i32 {
3+
let n = n as usize;
4+
let delay = delay as usize;
5+
let forget = forget as usize;
6+
let m = (n << 1) + 10;
7+
let modulo: i64 = 1_000_000_007;
8+
9+
let mut d = vec![0i64; m];
10+
let mut cnt = vec![0i64; m];
11+
12+
cnt[1] = 1;
13+
for i in 1..=n {
14+
if cnt[i] > 0 {
15+
d[i] = (d[i] + cnt[i]) % modulo;
16+
d[i + forget] = (d[i + forget] - cnt[i] + modulo) % modulo;
17+
let mut nxt = i + delay;
18+
while nxt < i + forget {
19+
cnt[nxt] = (cnt[nxt] + cnt[i]) % modulo;
20+
nxt += 1;
21+
}
22+
}
23+
}
24+
25+
let mut ans: i64 = 0;
26+
for i in 1..=n {
27+
ans = (ans + d[i]) % modulo;
28+
}
29+
ans as i32
30+
}
31+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
impl Solution {
2+
pub fn max_frequency_elements(nums: Vec<i32>) -> i32 {
3+
let mut cnt = [0; 101];
4+
for &x in &nums {
5+
cnt[x as usize] += 1;
6+
}
7+
let mut ans = 0;
8+
let mut mx = -1;
9+
for &x in &cnt {
10+
if mx < x {
11+
mx = x;
12+
ans = x;
13+
} else if mx == x {
14+
ans += x;
15+
}
16+
}
17+
ans
18+
}
19+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Solution {
2+
public:
3+
long long modeWeight(vector<int>& nums, int k) {
4+
unordered_map<int, int> cnt;
5+
priority_queue<pair<int, int>> pq; // {freq, -val}
6+
7+
for (int i = 0; i < k; i++) {
8+
int x = nums[i];
9+
cnt[x]++;
10+
pq.push({cnt[x], -x});
11+
}
12+
13+
auto get_mode = [&]() {
14+
while (true) {
15+
auto [freq, negVal] = pq.top();
16+
int val = -negVal;
17+
if (cnt[val] == freq) {
18+
return 1LL * freq * val;
19+
}
20+
pq.pop();
21+
}
22+
};
23+
24+
long long ans = 0;
25+
ans += get_mode();
26+
27+
for (int i = k; i < nums.size(); i++) {
28+
int x = nums[i], y = nums[i - k];
29+
cnt[x]++;
30+
cnt[y]--;
31+
pq.push({cnt[x], -x});
32+
pq.push({cnt[y], -y});
33+
ans += get_mode();
34+
}
35+
36+
return ans;
37+
}
38+
};
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
func modeWeight(nums []int, k int) int64 {
2+
cnt := make(map[int]int)
3+
pq := &MaxHeap{}
4+
heap.Init(pq)
5+
6+
for i := 0; i < k; i++ {
7+
x := nums[i]
8+
cnt[x]++
9+
heap.Push(pq, pair{cnt[x], x})
10+
}
11+
12+
getMode := func() int64 {
13+
for {
14+
top := (*pq)[0]
15+
if cnt[top.val] == top.freq {
16+
return int64(top.freq) * int64(top.val)
17+
}
18+
heap.Pop(pq)
19+
}
20+
}
21+
22+
var ans int64
23+
ans += getMode()
24+
25+
for i := k; i < len(nums); i++ {
26+
x, y := nums[i], nums[i-k]
27+
cnt[x]++
28+
cnt[y]--
29+
heap.Push(pq, pair{cnt[x], x})
30+
heap.Push(pq, pair{cnt[y], y})
31+
ans += getMode()
32+
}
33+
34+
return ans
35+
}
36+
37+
type pair struct {
38+
freq int
39+
val int
40+
}
41+
42+
type MaxHeap []pair
43+
44+
func (h MaxHeap) Len() int { return len(h) }
45+
func (h MaxHeap) Less(i, j int) bool {
46+
if h[i].freq != h[j].freq {
47+
return h[i].freq > h[j].freq
48+
}
49+
return h[i].val < h[j].val
50+
}
51+
func (h MaxHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
52+
func (h *MaxHeap) Push(x any) {
53+
*h = append(*h, x.(pair))
54+
}
55+
func (h *MaxHeap) Pop() any {
56+
old := *h
57+
n := len(old)
58+
x := old[n-1]
59+
*h = old[:n-1]
60+
return x
61+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Solution {
2+
public long modeWeight(int[] nums, int k) {
3+
Map<Integer, Integer> cnt = new HashMap<>();
4+
PriorityQueue<int[]> pq = new PriorityQueue<>(
5+
(a, b) -> a[0] != b[0] ? Integer.compare(a[0], b[0]) : Integer.compare(a[1], b[1]));
6+
7+
for (int i = 0; i < k; i++) {
8+
int x = nums[i];
9+
cnt.merge(x, 1, Integer::sum);
10+
pq.offer(new int[] {-cnt.get(x), x});
11+
}
12+
13+
long ans = 0;
14+
15+
Supplier<Long> getMode = () -> {
16+
while (true) {
17+
int[] top = pq.peek();
18+
int val = top[1];
19+
int freq = -top[0];
20+
if (cnt.getOrDefault(val, 0) == freq) {
21+
return 1L * freq * val;
22+
}
23+
pq.poll();
24+
}
25+
};
26+
27+
ans += getMode.get();
28+
29+
for (int i = k; i < nums.length; i++) {
30+
int x = nums[i], y = nums[i - k];
31+
cnt.merge(x, 1, Integer::sum);
32+
pq.offer(new int[] {-cnt.get(x), x});
33+
cnt.merge(y, -1, Integer::sum);
34+
pq.offer(new int[] {-cnt.get(y), y});
35+
ans += getMode.get();
36+
}
37+
38+
return ans;
39+
}
40+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution:
2+
def modeWeight(self, nums: List[int], k: int) -> int:
3+
pq = []
4+
cnt = defaultdict(int)
5+
for x in nums[:k]:
6+
cnt[x] += 1
7+
heappush(pq, (-cnt[x], x))
8+
9+
def get_mode() -> int:
10+
while -pq[0][0] != cnt[pq[0][1]]:
11+
heappop(pq)
12+
freq, val = -pq[0][0], pq[0][1]
13+
return freq * val
14+
15+
ans = 0
16+
ans += get_mode()
17+
18+
for i in range(k, len(nums)):
19+
x, y = nums[i], nums[i - k]
20+
cnt[x] += 1
21+
cnt[y] -= 1
22+
heappush(pq, (-cnt[x], x))
23+
heappush(pq, (-cnt[y], y))
24+
25+
ans += get_mode()
26+
27+
return ans
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import pandas as pd
2+
3+
4+
def find_zombie_sessions(app_events: pd.DataFrame) -> pd.DataFrame:
5+
if not pd.api.types.is_datetime64_any_dtype(app_events["event_timestamp"]):
6+
app_events["event_timestamp"] = pd.to_datetime(app_events["event_timestamp"])
7+
8+
grouped = app_events.groupby(["session_id", "user_id"])
9+
10+
result = grouped.agg(
11+
session_duration_minutes=(
12+
"event_timestamp",
13+
lambda x: (x.max() - x.min()).total_seconds() // 60,
14+
),
15+
scroll_count=("event_type", lambda x: (x == "scroll").sum()),
16+
click_count=("event_type", lambda x: (x == "click").sum()),
17+
purchase_count=("event_type", lambda x: (x == "purchase").sum()),
18+
).reset_index()
19+
20+
result = result[
21+
(result["session_duration_minutes"] >= 30)
22+
& (result["click_count"] / result["scroll_count"] < 0.2)
23+
& (result["purchase_count"] == 0)
24+
& (result["scroll_count"] >= 5)
25+
]
26+
27+
result = result.sort_values(
28+
by=["scroll_count", "session_id"], ascending=[False, True]
29+
).reset_index(drop=True)
30+
31+
return result[["session_id", "user_id", "session_duration_minutes", "scroll_count"]]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Write your MySQL query statement below
2+
SELECT
3+
session_id,
4+
user_id,
5+
TIMESTAMPDIFF(MINUTE, MIN(event_timestamp), MAX(event_timestamp)) session_duration_minutes,
6+
SUM(event_type = 'scroll') scroll_count
7+
FROM app_events
8+
GROUP BY session_id
9+
HAVING
10+
session_duration_minutes >= 30
11+
AND SUM(event_type = 'click') / SUM(event_type = 'scroll') < 0.2
12+
AND SUM(event_type = 'purchase') = 0
13+
AND SUM(event_type = 'scroll') >= 5
14+
ORDER BY scroll_count DESC, session_id;

0 commit comments

Comments
 (0)