Skip to content

Commit 850b057

Browse files
committed
adding algo minimum number of operations to turn array of positive numbers into a palindrome
1 parent a37336d commit 850b057

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
class Solution:
5+
def minimumOperations(self, nums: List[int]) -> int:
6+
7+
l, r = 0, len(nums) -1
8+
ls = nums[l]
9+
rs = nums[r]
10+
answer = 0
11+
12+
while l < r:
13+
14+
if ls == rs:
15+
l += 1
16+
r -= 1
17+
ls = nums[l]
18+
rs = nums[r]
19+
elif ls <= rs:
20+
l += 1
21+
ls += nums[l]
22+
answer += 1
23+
else:
24+
r -= 1
25+
rs += nums[r]
26+
answer += 1
27+
28+
return answer
29+

src/my_project/interviews/amazon_high_frequency_23/round_2/minimum_swaps_to_group_all_ones.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,13 @@ def minSwaps(self, data: List[int]) -> int:
1010
if i >= k: val -= data[i-k]
1111
if i+1 >= k: ans = max(ans, val)
1212
return k - ans
13+
14+
'''
15+
Window size (k): Count total number of 1s in the array. This is the size of the window needed to fit all 1s.
16+
17+
Sliding window: Move a window of size k across the array and count how many 1s are already in each window position.
18+
19+
Find maximum 1s: Track the maximum number of 1s found in any window (ans).
20+
21+
Calculate swaps: The minimum swaps needed = k - ans (total 1s minus the maximum 1s already grouped in any window).
22+
'''

0 commit comments

Comments
 (0)