반응형
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
# #Find Min Sum in a row
# sum_in_a_row_dict = dict() # key : (start_idx, # of elemnets) value : sum
# sum_in_a_row_dict[(0,1)] = cardPoints[0]
# # print(sum_in_a_row_dict[(0,1)])
# min_sum = pow(10,5) * len(cardPoints)
# if len(cardPoints) - k == 1: #second wrong
# min_sum = sum_in_a_row_dict[(0,1)]
# elif len(cardPoints) - k == 0:#third wrong
# min_sum = 0
# for idx in range(1, len(cardPoints)):
# sum_in_a_row_dict[(idx,1)] = cardPoints[idx]
# if len(cardPoints) - k == 1: #second wrong
# if sum_in_a_row_dict[(idx,1)] < min_sum:
# min_sum = sum_in_a_row_dict[(idx,1)]
# # print("Min Sum : {}".format(min_sum))
# # print(sum_in_a_row_dict[(idx,1)])
# for prev_idx in range(1, idx+1):
# sum_in_a_row_dict[(idx-prev_idx, 1+prev_idx)] = sum_in_a_row_dict[(idx-prev_idx, 1+prev_idx-1)] + cardPoints[idx]
# # print( sum_in_a_row_dict[(idx-prev_idx, 1+prev_idx)] )
# if 1+prev_idx == len(cardPoints) - k:
# if sum_in_a_row_dict[(idx-prev_idx, 1+prev_idx)] < min_sum:
# min_sum = sum_in_a_row_dict[(idx-prev_idx, 1+prev_idx)]
# print("Min Sum : {}".format(min_sum))
# return sum_in_a_row_dict[(0,len(cardPoints))] - min_sum
min_len = len(cardPoints) - k
sum_all = 0
sum_window = 0
min_sum = 0
for idx in range(len(cardPoints)):
sum_window += cardPoints[idx]
# print("SUM WINDOW : {}".format(sum_window))
sum_all += cardPoints[idx]
if idx == min_len-1: #fifth wrong
min_sum = sum_window
if idx >= min_len:
sum_window -= cardPoints[idx-min_len]
# print("SUM WINDOW Fixed : {}".format(sum_window))
if min_sum == 0:
min_sum = sum_window
else:
if min_sum > sum_window:
min_sum = sum_window
# print(min_sum)
if min_len == 0:
return sum_all
else:
return sum_all - min_sum
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
#sorted_cardPoints = sorted(cardPoints)
#print(sorted_cardPoints)
#max_point_list_1 = cardPoints[:k]
#max_point_list_2 = cardPoints[-k:]
#sum_1 = 0
#sum_2 = 0
#for i in max_point_list_1:
# sum_1 += i
#for i in max_point_list_2:
# sum_2 += i
#sum = sum_1
#if sum_2 > sum_1:
# sum = sum_2
left_idx = 0
right_idx = len(cardPoints) - 1
sel_cnt = 0
sum = 0
while sel_cnt == k:
left_value = cardPoints[left_idx]
right_value = cardPoints[right_idx]
if left_value > right_value:
elif left_value < right_value:
else: #Consider l == r...
#ㅇㅏㄴㅣㅁㅕㄴ ㅇㅕㄱㅂㅏㄹㅅㅏㅇ.
#Consider Min Sum of elements in a row.
return sum
반응형
'SW > Algorithm Problem (Coding)' 카테고리의 다른 글
304. Range Sum Query 2D - Immutable(Medium) (0) | 2020.11.05 |
---|---|
1640. Check Array Formation Through Concatenation(Easy) (0) | 2020.11.05 |
1301. Number of Paths with Max Score(Hard) (0) | 2020.11.03 |
댓글