Skip to content

Commit 46e414b

Browse files
committed
Checkpoint from VS Code for coding agent session
1 parent 09613a4 commit 46e414b

10 files changed

Lines changed: 589 additions & 0 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <algorithm>
2+
#include <unordered_set>
3+
#include <vector>
4+
5+
class Solution {
6+
public:
7+
int longestConsecutive(std::vector<int> &nums) {
8+
// 1. 将所有元素存入哈希集合
9+
std::unordered_set<int> hmap(nums.begin(), nums.end());
10+
int ans = 0;
11+
12+
// 获取不同元素的总个数 M
13+
int M = hmap.size();
14+
15+
for (auto x : nums) {
16+
17+
// 核心优化:只从连续序列的起点开始检查
18+
// 如果 x-1 存在,说明 x 不是起点,跳过
19+
if (hmap.count(x - 1))
20+
continue;
21+
22+
int length = 1;
23+
int y = x + 1;
24+
25+
// 延伸序列
26+
while (hmap.count(y)) {
27+
length++;
28+
y++;
29+
}
30+
31+
// 更新答案
32+
ans = std::max(ans, length);
33+
34+
// 【额外的剪枝优化】: 检查当前找到的最长长度 ans
35+
// 如果 ans * 2 > M,那么后续找到更长的链的可能性极低(除非当前 ans 已经是最优解)
36+
// 注意:这里 M 是 hmap 的大小,也就是不同元素的个数。
37+
if (ans * 2 > M) {
38+
return ans;
39+
}
40+
}
41+
42+
return ans;
43+
}
44+
};
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
func longestConsecutive(nums []int) int {
2+
numset := make(map[int]bool)
3+
for _,v := range nums{
4+
numset[v] = true
5+
}
6+
ans := 0
7+
for x := range numset{
8+
_,existsPrev := numset[x-1]
9+
if existsPrev{
10+
continue
11+
}
12+
currentNum := x
13+
currentLength := 1
14+
for{
15+
_,existsNext := numset[currentNum+1]
16+
if !existsNext{
17+
break
18+
}
19+
currentNum++
20+
currentLength++
21+
}
22+
ans = max(ans,currentLength)
23+
}
24+
return ans
25+
}
26+
27+
// func max(a,b int)int{
28+
// if a > b{
29+
// return a
30+
// }else{
31+
// return b
32+
// }
33+
// }
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
func countMentions(numberOfUsers int, events [][]string) []int {
2+
ans := make([]int, numberOfUsers)
3+
// Event struct: timestamp, type (1=offline, -1=online, 2=HERE mention), user id
4+
type event struct {
5+
timestamp, type_, id int
6+
}
7+
es := []event{}
8+
all := 0
9+
// Process raw events and separate them into individual event objects
10+
for _, e := range events {
11+
curT, _ := strconv.Atoi(e[1])
12+
mention := e[2]
13+
if e[0] == "OFFLINE" {
14+
// Create offline(1) and online(-1) events 60 seconds later
15+
i, _ := strconv.Atoi(mention)
16+
es = append(es, event{curT, 1, i}, event{curT + 60, -1, i})
17+
} else if mention == "ALL" {
18+
all++
19+
} else if mention == "HERE" {
20+
// Increment all user mentions for HERE events (process later)
21+
all++
22+
es = append(es, event{curT, 2, -1})
23+
} else {
24+
// Direct user mentions: add immediately
25+
for _, s := range strings.Split(mention, " ") {
26+
i, _ := strconv.Atoi(s[2:])
27+
ans[i]++
28+
}
29+
}
30+
}
31+
32+
// Sort events by timestamp, then by type (process in correct order)
33+
slices.SortFunc(es, func(a, b event) int {
34+
return cmp.Or(a.timestamp-b.timestamp, a.type_-b.type_)
35+
})
36+
37+
// Calculate HERE mentions: count online users at each HERE event
38+
here := 0
39+
for _, e := range es {
40+
if e.type_ == 2 {
41+
// HERE mention: increment all currently online users by 'here' count
42+
here++
43+
} else {
44+
// OFFLINE/ONLINE event: update online status (1 removes, -1 adds)
45+
ans[e.id] += e.type_ * here
46+
}
47+
}
48+
49+
// Add ALL mentions to all users
50+
for i := range ans {
51+
ans[i] += all
52+
}
53+
return ans
54+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution:
2+
def countMentions(self, numberOfUsers: int, events: List[List[str]]) -> List[int]:
3+
# Sort events by timestamp, then by event type (to process in chronological order)
4+
events.sort(key = lambda e:(int(e[1]),e[0][2]))
5+
6+
# ans[i] stores the mention count for user i
7+
ans = [0]*numberOfUsers
8+
# online_t[i] stores when user i will be back online (0 means currently online)
9+
online_t = [0]*numberOfUsers
10+
11+
for type_,timestamp,mention in events:
12+
curr_t = int(timestamp)
13+
# Handle OFFLINE event: user goes offline for 60 seconds
14+
if type_ == "OFFLINE":
15+
online_t[int(mention)] = curr_t + 60
16+
# Handle ALL mentions: all users are mentioned
17+
elif mention == "ALL":
18+
for i in range(numberOfUsers):
19+
ans[i] += 1
20+
# Handle HERE mentions: only currently online users are mentioned
21+
elif mention == "HERE":
22+
for i,t in enumerate(online_t):
23+
if curr_t >= t:
24+
ans[i] += 1
25+
# Handle individual user mentions (e.g., "id0 id1 id2")
26+
else:
27+
for s in mention.split():
28+
# Extract user ID from "id<number>" format
29+
ans[int(s[2:])] += 1
30+
return ans
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include<vector>
2+
#include<string>
3+
#include<algorithm>
4+
using namespace std;
5+
class Solution {
6+
public:
7+
vector<string> validateCoupons(vector<string>& code, vector<string>& businessLine, vector<bool>& isActive) {
8+
int n = code.size();
9+
// Store pairs of (businessLine priority, code)
10+
vector<pair<int, string>> ans;
11+
for(int i = 0; i < n; ++i)
12+
{
13+
if(isActive[i] && isValid(code[i], businessLine[i]))
14+
{
15+
int priority = getBusinessPriority(businessLine[i]);
16+
ans.push_back({priority, code[i]});
17+
}
18+
}
19+
// Sort by priority first, then by code lexicographically
20+
sort(ans.begin(), ans.end());
21+
22+
// Extract just the codes for the result
23+
vector<string> result;
24+
for(auto &p : ans)
25+
{
26+
result.push_back(p.second);
27+
}
28+
return result;
29+
}
30+
31+
private:
32+
vector<string> vaild_business = {
33+
"electronics",
34+
"grocery",
35+
"pharmacy",
36+
"restaurant"
37+
};
38+
39+
// Get priority of business line (0 = highest priority)
40+
int getBusinessPriority(const string &b)
41+
{
42+
auto it = find(vaild_business.begin(), vaild_business.end(), b);
43+
if(it == vaild_business.end()) return -1;
44+
return it - vaild_business.begin();
45+
}
46+
47+
bool isValid(const string &c, const string &b)
48+
{
49+
if(find(vaild_business.begin(), vaild_business.end(), b) == vaild_business.end())
50+
return false;
51+
if(c == "")
52+
return false;
53+
for(auto ch : c)
54+
{
55+
bool isAlpha = (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z');
56+
bool isDigit = ch >= '0' && ch <= '9';
57+
bool isUnderscore = ch == '_';
58+
59+
if(!isAlpha && !isDigit && !isUnderscore)
60+
return false;
61+
}
62+
return true;
63+
}
64+
};
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
select u.unique_id,e.name from Employees as e left join EmployeeUNI as u
2+
on e.id = u.id;
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include <algorithm>
2+
#include <functional>
3+
#include <iomanip>
4+
#include <iostream>
5+
#include <queue>
6+
#include <string>
7+
#include <unordered_map>
8+
#include <vector>
9+
#define il inline
10+
#define endl '\n'
11+
using namespace std;
12+
13+
#define pb push_back
14+
#define fastio \
15+
ios::sync_with_stdio(false); \
16+
cin.tie(0);
17+
18+
typedef long long ll;
19+
typedef unsigned long long ull;
20+
21+
const ll N = 5e5 + 5, mod = 1e9 + 7, inf = 2e18;
22+
const double eps = 1e-9;
23+
const double PI = 3.1415926;
24+
25+
class Student {
26+
public:
27+
string name;
28+
unordered_map<string, int> scores = {
29+
{"chinese", 0},
30+
{"math", 0},
31+
{"english", 0},
32+
};
33+
int total_score = 0;
34+
35+
Student(string _name, int math, int chinese, int english) : name(_name) {
36+
scores["chinese"] = chinese;
37+
scores["math"] = math;
38+
scores["english"] = english;
39+
total_score = chinese + math + english;
40+
}
41+
};
42+
43+
auto cmp = [](const Student &a, const Student &b) {
44+
return a.total_score < b.total_score; // Max heap: highest score on top
45+
};
46+
47+
priority_queue<Student, vector<Student>, decltype(cmp)> pq(cmp);
48+
49+
il void solve() {
50+
string name;
51+
int chinese, math, english;
52+
cin >> name >> chinese >> math >> english;
53+
Student stu(name, math, chinese, english);
54+
pq.push(stu);
55+
56+
}
57+
58+
int main() {
59+
fastio;
60+
61+
int t = 1;
62+
cin >> t;
63+
64+
while (t--) {
65+
solve();
66+
}
67+
68+
if (!pq.empty()) {
69+
Student top_student = pq.top();
70+
cout << top_student.name << " " << top_student.scores["chinese"] << " " << top_student.scores["math"] << " "
71+
<< top_student.scores["english"] << endl;
72+
}
73+
return 0;
74+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* struct Point {
3+
* int x;
4+
* int y;
5+
* Point(int xx, int yy) : x(xx), y(yy) {}
6+
* };
7+
*/
8+
class Solution {
9+
public:
10+
/**
11+
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
12+
*
13+
* 计算A点与B点之间的距离
14+
* @param point_A Point类 A点
15+
* @param point_B Point类 B点
16+
* @return double浮点型
17+
*/
18+
double calculateDistance(Point point_A, Point point_B) {
19+
// write code here
20+
double dx = static_cast<double>(point_A.x - point_B.x);
21+
double dy = static_cast<double>(point_A.y - point_B.y);
22+
return sqrt(dx * dx + dy * dy);
23+
}
24+
};

0 commit comments

Comments
 (0)