Skip to content

Commit

Permalink
contest-133
Browse files Browse the repository at this point in the history
  • Loading branch information
Azureki committed Apr 21, 2019
1 parent c456456 commit 6463cab
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
11 changes: 11 additions & 0 deletions 1029. Two City Scheduling/better-soln.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution:
def twoCitySchedCost(self, costs: List[List[int]]) -> int:
costs.sort(key = lambda x: x[0]-x[1])

N = len(costs) // 2
cost = 0
for i in range(N):
cost += costs[i][0]
for i in range(N):
cost += costs[N+i][1]
return cost
28 changes: 28 additions & 0 deletions 1029. Two City Scheduling/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import itertools


class Solution:
def twoCitySchedCost(self, costs):
def get_diff(cost):
return abs(cost[0]-cost[1])

res = 0
N = len(costs)//2
d = {0: 0, 1: 0}
lst = sorted(costs, key=get_diff, reverse=True)
for cost in lst:
idx = 0 if cost[0]<cost[1] else 1
if d[idx]!=N:
d[idx]+=1
res+=cost[idx]
else:
d[1-idx]+=1
res+=cost[1-idx]

return res


costs = [[259,770],[448,54],[926,667],[184,139],[840,118],[577,469]]
res = Solution().twoCitySchedCost(costs)
print(res)

12 changes: 12 additions & 0 deletions 1030. Matrix Cells in Distance Order/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import itertools

class Solution:

def allCellsDistOrder(self, R: int, C: int, r0: int, c0: int) -> List[List[int]]:

def get_distance(point1):
return abs(point1[1]-c0)+ abs(point1[0]-r0)

it = itertools.product(range(R),range(C))
return sorted(list(it), key = get_distance)

0 comments on commit 6463cab

Please sign in to comment.