forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add solutions to lc problem: No.1208
No.1208.Get Equal Substrings Within Budget
- Loading branch information
Showing
16 changed files
with
584 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
solution/1200-1299/1208.Get Equal Substrings Within Budget/Solution.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
class Solution { | ||
public: | ||
int equalSubstring(string s, string t, int maxCost) { | ||
int n = s.size(); | ||
vector<int> presum(n + 1); | ||
for (int i = 0; i < n; ++i) presum[i + 1] = presum[i] + abs(s[i] - t[i]); | ||
int left = 0, right = n; | ||
while (left < right) | ||
{ | ||
int mid = left + right + 1 >> 1; | ||
if (check(mid, presum, maxCost, n)) left = mid; | ||
else right = mid - 1; | ||
} | ||
return left; | ||
} | ||
|
||
bool check(int l, vector<int>& s, int maxCost, int n) { | ||
int i = 0; | ||
while (i + l - 1 < n) | ||
{ | ||
int j = i + l - 1; | ||
if (s[j + 1] - s[i] <= maxCost) return true; | ||
++i; | ||
} | ||
return false; | ||
} | ||
}; |
36 changes: 36 additions & 0 deletions
36
solution/1200-1299/1208.Get Equal Substrings Within Budget/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
func equalSubstring(s string, t string, maxCost int) int { | ||
n := len(s) | ||
presum := make([]int, n+1) | ||
for i, c := range s { | ||
presum[i+1] = presum[i] + abs(int(c)-int(t[i])) | ||
} | ||
|
||
left, right := 0, n | ||
check := func(l int) bool { | ||
i := 0 | ||
for i+l-1 < n { | ||
j := i + l - 1 | ||
if presum[j+1]-presum[i] <= maxCost { | ||
return true | ||
} | ||
i++ | ||
} | ||
return false | ||
} | ||
for left < right { | ||
mid := (left + right + 1) >> 1 | ||
if check(mid) { | ||
left = mid | ||
} else { | ||
right = mid - 1 | ||
} | ||
} | ||
return left | ||
} | ||
|
||
func abs(x int) int { | ||
if x > 0 { | ||
return x | ||
} | ||
return -x | ||
} |
31 changes: 31 additions & 0 deletions
31
solution/1200-1299/1208.Get Equal Substrings Within Budget/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
class Solution { | ||
public int equalSubstring(String s, String t, int maxCost) { | ||
int n = s.length(); | ||
int[] presum = new int[n + 1]; | ||
for (int i = 0; i < n; ++i) { | ||
presum[i + 1] = presum[i] + Math.abs(s.charAt(i) - t.charAt(i)); | ||
} | ||
int left = 0, right = n; | ||
while (left < right) { | ||
int mid = (left + right + 1) >>> 1; | ||
if (check(mid, presum, maxCost, n)) { | ||
left = mid; | ||
} else { | ||
right = mid - 1; | ||
} | ||
} | ||
return left; | ||
} | ||
|
||
private boolean check(int l, int[] s, int maxCost, int n) { | ||
int i = 0; | ||
while (i + l - 1 < n) { | ||
int j = i + l - 1; | ||
if (s[j + 1] - s[i] <= maxCost) { | ||
return true; | ||
} | ||
++i; | ||
} | ||
return false; | ||
} | ||
} |
Oops, something went wrong.