Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions leetcode/Study Plan/128. 最长连续序列/1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <algorithm>
#include <unordered_set>
#include <vector>

class Solution {
public:
int longestConsecutive(std::vector<int> &nums) {
// 1. 将所有元素存入哈希集合
std::unordered_set<int> hmap(nums.begin(), nums.end());
int ans = 0;

// 获取不同元素的总个数 M
int M = hmap.size();

for (auto x : nums) {

// 核心优化:只从连续序列的起点开始检查
// 如果 x-1 存在,说明 x 不是起点,跳过
if (hmap.count(x - 1))
continue;

int length = 1;
int y = x + 1;

// 延伸序列
while (hmap.count(y)) {
length++;
y++;
}

// 更新答案
ans = std::max(ans, length);

// 【额外的剪枝优化】: 检查当前找到的最长长度 ans
// 如果 ans * 2 > M,那么后续找到更长的链的可能性极低(除非当前 ans 已经是最优解)
// 注意:这里 M 是 hmap 的大小,也就是不同元素的个数。
if (ans * 2 > M) {
return ans;
}
}

return ans;
}
};
33 changes: 33 additions & 0 deletions leetcode/Study Plan/128. 最长连续序列/1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
func longestConsecutive(nums []int) int {
numset := make(map[int]bool)
for _,v := range nums{
numset[v] = true
}
ans := 0
for x := range numset{
_,existsPrev := numset[x-1]
if existsPrev{
continue
}
currentNum := x
currentLength := 1
for{
_,existsNext := numset[currentNum+1]
if !existsNext{
break
}
currentNum++
currentLength++
}
ans = max(ans,currentLength)
}
return ans
}

// func max(a,b int)int{
// if a > b{
// return a
// }else{
// return b
// }
// }
54 changes: 54 additions & 0 deletions leetcode/Study Plan/3433. 统计用户被提及情况/1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
func countMentions(numberOfUsers int, events [][]string) []int {
ans := make([]int, numberOfUsers)
// Event struct: timestamp, type (1=offline, -1=online, 2=HERE mention), user id
type event struct {
timestamp, type_, id int
}
es := []event{}
all := 0
// Process raw events and separate them into individual event objects
for _, e := range events {
curT, _ := strconv.Atoi(e[1])
mention := e[2]
if e[0] == "OFFLINE" {
// Create offline(1) and online(-1) events 60 seconds later
i, _ := strconv.Atoi(mention)
es = append(es, event{curT, 1, i}, event{curT + 60, -1, i})
} else if mention == "ALL" {
all++
} else if mention == "HERE" {
// Increment all user mentions for HERE events (process later)
all++
es = append(es, event{curT, 2, -1})
} else {
// Direct user mentions: add immediately
for _, s := range strings.Split(mention, " ") {
i, _ := strconv.Atoi(s[2:])
ans[i]++
}
}
}

// Sort events by timestamp, then by type (process in correct order)
slices.SortFunc(es, func(a, b event) int {
return cmp.Or(a.timestamp-b.timestamp, a.type_-b.type_)
})

// Calculate HERE mentions: count online users at each HERE event
here := 0
for _, e := range es {
if e.type_ == 2 {
// HERE mention: increment all currently online users by 'here' count
here++
} else {
// OFFLINE/ONLINE event: update online status (1 removes, -1 adds)
ans[e.id] += e.type_ * here
}
}

// Add ALL mentions to all users
for i := range ans {
ans[i] += all
}
return ans
}
30 changes: 30 additions & 0 deletions leetcode/Study Plan/3433. 统计用户被提及情况/1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Solution:
def countMentions(self, numberOfUsers: int, events: List[List[str]]) -> List[int]:
# Sort events by timestamp, then by event type (to process in chronological order)
events.sort(key = lambda e:(int(e[1]),e[0][2]))

# ans[i] stores the mention count for user i
ans = [0]*numberOfUsers
# online_t[i] stores when user i will be back online (0 means currently online)
online_t = [0]*numberOfUsers

for type_,timestamp,mention in events:
curr_t = int(timestamp)
# Handle OFFLINE event: user goes offline for 60 seconds
if type_ == "OFFLINE":
online_t[int(mention)] = curr_t + 60
# Handle ALL mentions: all users are mentioned
elif mention == "ALL":
for i in range(numberOfUsers):
ans[i] += 1
# Handle HERE mentions: only currently online users are mentioned
elif mention == "HERE":
for i,t in enumerate(online_t):
if curr_t >= t:
ans[i] += 1
# Handle individual user mentions (e.g., "id0 id1 id2")
else:
for s in mention.split():
# Extract user ID from "id<number>" format
ans[int(s[2:])] += 1
return ans
64 changes: 64 additions & 0 deletions leetcode/Study Plan/3606. 优惠券校验器/1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
class Solution {
public:
vector<string> validateCoupons(vector<string>& code, vector<string>& businessLine, vector<bool>& isActive) {
int n = code.size();
// Store pairs of (businessLine priority, code)
vector<pair<int, string>> ans;
for(int i = 0; i < n; ++i)
{
if(isActive[i] && isValid(code[i], businessLine[i]))
{
int priority = getBusinessPriority(businessLine[i]);
ans.push_back({priority, code[i]});
}
}
// Sort by priority first, then by code lexicographically
sort(ans.begin(), ans.end());

// Extract just the codes for the result
vector<string> result;
for(auto &p : ans)
{
result.push_back(p.second);
}
return result;
}

private:
vector<string> vaild_business = {
"electronics",
"grocery",
"pharmacy",
"restaurant"
};

// Get priority of business line (0 = highest priority)
int getBusinessPriority(const string &b)
{
auto it = find(vaild_business.begin(), vaild_business.end(), b);
if(it == vaild_business.end()) return -1;
return it - vaild_business.begin();
}

bool isValid(const string &c, const string &b)
{
if(find(vaild_business.begin(), vaild_business.end(), b) == vaild_business.end())
return false;
if(c == "")
return false;
for(auto ch : c)
{
bool isAlpha = (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z');
bool isDigit = ch >= '0' && ch <= '9';
bool isUnderscore = ch == '_';

if(!isAlpha && !isDigit && !isUnderscore)
return false;
}
return true;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
select u.unique_id,e.name from Employees as e left join EmployeeUNI as u
on e.id = u.id;
74 changes: 74 additions & 0 deletions niuke/practice/noob/noob69 最厉害的学生/1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <queue>
#include <string>
#include <unordered_map>
#include <vector>
#define il inline
#define endl '\n'
using namespace std;

#define pb push_back
#define fastio \
ios::sync_with_stdio(false); \
cin.tie(0);

typedef long long ll;
typedef unsigned long long ull;

const ll N = 5e5 + 5, mod = 1e9 + 7, inf = 2e18;
const double eps = 1e-9;
const double PI = 3.1415926;

class Student {
public:
string name;
unordered_map<string, int> scores = {
{"chinese", 0},
{"math", 0},
{"english", 0},
};
int total_score = 0;

Student(string _name, int math, int chinese, int english) : name(_name) {
scores["chinese"] = chinese;
scores["math"] = math;
scores["english"] = english;
total_score = chinese + math + english;
}
};

auto cmp = [](const Student &a, const Student &b) {
return a.total_score < b.total_score; // Max heap: highest score on top
};

priority_queue<Student, vector<Student>, decltype(cmp)> pq(cmp);

il void solve() {
string name;
int chinese, math, english;
cin >> name >> chinese >> math >> english;
Student stu(name, math, chinese, english);
pq.push(stu);

}

int main() {
fastio;

int t = 1;
cin >> t;

while (t--) {
solve();
}

if (!pq.empty()) {
Student top_student = pq.top();
cout << top_student.name << " " << top_student.scores["chinese"] << " " << top_student.scores["math"] << " "
<< top_student.scores["english"] << endl;
}
return 0;
}
24 changes: 24 additions & 0 deletions niuke/practice/noob/noob70 两点间距离/1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* struct Point {
* int x;
* int y;
* Point(int xx, int yy) : x(xx), y(yy) {}
* };
*/
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* 计算A点与B点之间的距离
* @param point_A Point类 A点
* @param point_B Point类 B点
* @return double浮点型
*/
double calculateDistance(Point point_A, Point point_B) {
// write code here
double dx = static_cast<double>(point_A.x - point_B.x);
double dy = static_cast<double>(point_A.y - point_B.y);
return sqrt(dx * dx + dy * dy);
}
};
Loading
Loading