algorithmzuo / coding-for-great-offer Goto Github PK
View Code? Open in Web Editor NEW大厂算法和数据结构刷题班
大厂算法和数据结构刷题班
public int minAoeNums(int[] x, int[] hp, int range) {
int ans = 0;
int monsterNums = x.length;
//当前攻击范围的左侧怪兽的下标
int currentLeftI = 0;
// 当前攻击范围的右侧怪兽下标
int targetI = 0;
//当前技能指向的怪兽下标
int currentRightI = 0;
//清空窗口内的怪兽,需要释放的技能次数
int windowSum = 0;
// hp窗口,记录某位置进入窗口后,要出窗口,作为最左侧未死亡怪兽时要承受的技能次数
LinkedList<Integer> hpWindow = new LinkedList<>();
// 对应hp窗口的怪兽坐标,用于获取下一个最左侧未死亡怪兽的坐标
LinkedList<Integer> iWindow = new LinkedList<>();
do {
if (hpWindow.isEmpty()) {
hpWindow.addLast(hp[currentLeftI]);
iWindow.addLast(currentLeftI);
windowSum += hp[currentLeftI];
continue;
}
targetI = Math.max(currentLeftI, targetI) + 1;
while (targetI < monsterNums && x[targetI] - x[currentLeftI] <= range) {
// 假设要清空窗口中的怪物,targetI位置的怪物会被附带减伤windowSum的伤害
int targetRestHp = hp[targetI] - windowSum;
if (targetI > currentRightI && targetRestHp > 0) {
hpWindow.addLast(targetRestHp);
iWindow.addLast(targetI);
windowSum += targetRestHp;
}
targetI++;
}
targetI--;
currentRightI = Math.max(targetI, currentRightI) + 1;
while (currentRightI < monsterNums && x[currentRightI] - x[targetI] <= range) {
int rightRestHp = hp[currentRightI] - windowSum;
if (rightRestHp > 0) {
hpWindow.addLast(rightRestHp);
iWindow.addLast(currentRightI);
windowSum += rightRestHp;
}
currentRightI++;
}
// 快速结束,也可以去掉这个if判断
if (currentRightI >= monsterNums) {
ans += windowSum;
return ans;
}
currentRightI--;
// 施法范围的左侧怪兽死亡,结算需要释放的aoe次数
if (!hpWindow.isEmpty()) {
int leftMonsterDieHp = hpWindow.pollFirst();
ans += leftMonsterDieHp;
windowSum -= leftMonsterDieHp;
}
// 左侧怪兽下标调整,窗口为空,继续选择下一个怪兽,否则,从窗口中选择一个
iWindow.pollFirst();
if (iWindow.isEmpty()) {
currentLeftI = currentRightI + 1;
} else {
currentLeftI = iWindow.peekFirst();
}
} while (currentLeftI < monsterNums);
return ans;
}
左神,左神,请教一个问题
题目:
给定两个字符串s1和s2,问s2最少删除多少字符可以成为s1的子串?比如 s1 = "abcde",s2 = "axbc",s2删掉'x'即可,返回1
用最长公共子序列可以解决吗。 我思路是找到最长的公共子序列,然后s2删除 s2.length -最长长度,即是要删除的字符个数
def longestCommonSubsequence(s1,s2):
n1 = len(s1)
n2 = len(s2)
dp = [[0]*(n2+1) for _ in range(n1+1)] # dp[i][j] 是 s1[:i] 和s2[:j]的最长公共子序列
for i in range(1,n1+1):
for j in range(1,n2+1):
if s1[i-1] == s2[j-1]:
dp[i][j] = dp[i-1][j-1] +1
else:
dp[i][j] = max(dp[i-1][j],dp[i][j-1])
return dp[-1][-1]
def minRemove(s1,s2):
length = longestCommonSubsequence(s1, s2)
return len(s2) - length
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.