soln2: class Solution: def shiftDistance(self, s: str, t: str, nextCost: list[int], previousCost: list[int]) -> int: n=len(s) totalCost=0 for i in range(n): start=ord(s[i])-ord('a') target=ord(t[i])-ord('a') clockwiseShifts=(target-start+26)%26 clockwiseCost=0 for j in range(clockwiseShifts): clockwiseCost+=nextCost[(start+j)%26] counterClockwiseShifts=(start-target+26)%26 counterClockwiseCost=0 for j in range(counterClockwiseShifts): counterClockwiseCost+=previousCost[(start-j+26)%26] totalCost+=min(clockwiseCost,counterClockwiseCost) return totalCost
Nice 👍
int step=10;
int flag =1;
while(n>=step){
n-=step;
flag*=-1;
step--;
}
return flag!=1;
soln2:
class Solution:
def shiftDistance(self, s: str, t: str, nextCost: list[int], previousCost: list[int]) -> int:
n=len(s)
totalCost=0
for i in range(n):
start=ord(s[i])-ord('a')
target=ord(t[i])-ord('a')
clockwiseShifts=(target-start+26)%26
clockwiseCost=0
for j in range(clockwiseShifts):
clockwiseCost+=nextCost[(start+j)%26]
counterClockwiseShifts=(start-target+26)%26
counterClockwiseCost=0
for j in range(counterClockwiseShifts):
counterClockwiseCost+=previousCost[(start-j+26)%26]
totalCost+=min(clockwiseCost,counterClockwiseCost)
return totalCost
Subscribers target 150