-
-
Notifications
You must be signed in to change notification settings - Fork 8.1k
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.3422
No.3422.Minimum Operations to Make Subarray Elements Equal
- Loading branch information
Showing
3 changed files
with
139 additions
and
3 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
46 changes: 46 additions & 0 deletions
46
solution/3400-3499/3422.Minimum Operations to Make Subarray Elements Equal/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,46 @@ | ||
func minOperations(nums []int, k int) int64 { | ||
l := redblacktree.New[int, int]() | ||
r := redblacktree.New[int, int]() | ||
merge := func(st *redblacktree.Tree[int, int], x, v int) { | ||
c, _ := st.Get(x) | ||
if c+v == 0 { | ||
st.Remove(x) | ||
} else { | ||
st.Put(x, c+v) | ||
} | ||
} | ||
var s1, s2, sz1, sz2 int | ||
ans := math.MaxInt64 | ||
for i, x := range nums { | ||
merge(l, x, 1) | ||
s1 += x | ||
y := l.Right().Key | ||
merge(l, y, -1) | ||
s1 -= y | ||
merge(r, y, 1) | ||
s2 += y | ||
sz2++ | ||
if sz2-sz1 > 1 { | ||
y = r.Left().Key | ||
merge(r, y, -1) | ||
s2 -= y | ||
sz2-- | ||
merge(l, y, 1) | ||
s1 += y | ||
sz1++ | ||
} | ||
if j := i - k + 1; j >= 0 { | ||
ans = min(ans, s2-r.Left().Key*sz2+r.Left().Key*sz1-s1) | ||
if _, ok := r.Get(nums[j]); ok { | ||
merge(r, nums[j], -1) | ||
s2 -= nums[j] | ||
sz2-- | ||
} else { | ||
merge(l, nums[j], -1) | ||
s1 -= nums[j] | ||
sz1-- | ||
} | ||
} | ||
} | ||
return int64(ans) | ||
} |