classSolution{public:intgetMaxGridHappiness(intm,intn,intintrovertsCount,intextrovertsCount){constinttwoToThePowerOfN=pow(2,n);vector<vector<vector<vector<vector<int>>>>>mem(m*n,vector<vector<vector<vector<int>>>>(twoToThePowerOfN,vector<vector<vector<int>>>(twoToThePowerOfN,vector<vector<int>>(introvertsCount+1,vector<int>(extrovertsCount+1)))));returngetMaxGridHappiness(m,n,0,0,0,introvertsCount,extrovertsCount,mem);}private:// Calculates the cost based on left and up neighbors.//// The `diff` parameter represents the happiness change due to the current// placed person in (i, j). We add `diff` each time we encounter a neighbor// (left or up) who is already placed.//// 1. If the neighbor is an introvert, we subtract 30 from cost.// 2. If the neighbor is an extrovert, we add 20 to from cost.intgetPlacementCost(intn,inti,intj,intinMask,intexMask,intdiff){intcost=0;if(i>0){if((1<<(n-1))&inMask)cost+=diff-30;if((1<<(n-1))&exMask)cost+=diff+20;}if(j>0){if(1&inMask)cost+=diff-30;if(1&exMask)cost+=diff+20;}returncost;}intgetMaxGridHappiness(intm,intn,intpos,intinMask,intexMask,intinCount,intexCount,vector<vector<vector<vector<vector<int>>>>>&mem){// `inMask` is the placement of introvert people in the last n cells.// e.g. if we have m = 2, n = 3, i = 1, j = 1, then inMask = 0b101 means//// ? 1 0// 1 x ? (x := current position)constinti=pos/n;constintj=pos%n;if(i==m)return0;if(mem[pos][inMask][exMask][inCount][exCount]>0)returnmem[pos][inMask][exMask][inCount][exCount];constintshiftedInMask=(inMask<<1)&((1<<n)-1);constintshiftedExMask=(exMask<<1)&((1<<n)-1);constintskip=getMaxGridHappiness(m,n,pos+1,shiftedInMask,shiftedExMask,inCount,exCount,mem);constintplaceIntrovert=inCount>0?120+getPlacementCost(n,i,j,inMask,exMask,-30)+getMaxGridHappiness(m,n,pos+1,shiftedInMask|1,shiftedExMask,inCount-1,exCount,mem):INT_MIN;constintplaceExtrovert=exCount>0?40+getPlacementCost(n,i,j,inMask,exMask,20)+getMaxGridHappiness(m,n,pos+1,shiftedInMask,shiftedExMask|1,inCount,exCount-1,mem):INT_MIN;returnmem[pos][inMask][exMask][inCount][exCount]=max({skip,placeIntrovert,placeExtrovert});}};
classSolution{publicintgetMaxGridHappiness(intm,intn,intintrovertsCount,intextrovertsCount){finalinttwoToThePowerOfN=(int)Math.pow(2,n);int[][][][][]mem=newint[m*n][twoToThePowerOfN][twoToThePowerOfN][introvertsCount+1][extrovertsCount+1];returngetMaxGridHappiness(m,n,0,0,0,introvertsCount,extrovertsCount,mem);}// Calculates the cost based on left and up neighbors.//// The `diff` parameter represents the happiness change due to the current// placed person in (i, j). We add `diff` each time we encounter a neighbor// (left or up) who is already placed.//// 1. If the neighbor is an introvert, we subtract 30 from cost.// 2. If the neighbor is an extrovert, we add 20 to from cost.privateintgetPlacementCost(intn,inti,intj,intinMask,intexMask,intdiff){intcost=0;if(i>0){if(((1<<(n-1))&inMask)>0)cost+=diff-30;if(((1<<(n-1))&exMask)>0)cost+=diff+20;}if(j>0){if((1&inMask)>0)cost+=diff-30;if((1&exMask)>0)cost+=diff+20;}returncost;}privateintgetMaxGridHappiness(intm,intn,intpos,intinMask,intexMask,intinCount,intexCount,int[][][][][]mem){// `inMask` is the placement of introvert people in the last n cells.// e.g. if we have m = 2, n = 3, i = 1, j = 1, then inMask = 0b101 means//// ? 1 0// 1 x ? (x := current position)finalinti=pos/n;finalintj=pos%n;if(i==m)return0;if(mem[pos][inMask][exMask][inCount][exCount]>0)returnmem[pos][inMask][exMask][inCount][exCount];finalintshiftedInMask=(inMask<<1)&((1<<n)-1);finalintshiftedExMask=(exMask<<1)&((1<<n)-1);finalintskip=getMaxGridHappiness(m,n,pos+1,shiftedInMask,shiftedExMask,inCount,exCount,mem);finalintplaceIntrovert=inCount>0?120+getPlacementCost(n,i,j,inMask,exMask,-30)+getMaxGridHappiness(m,n,pos+1,shiftedInMask|1,shiftedExMask,inCount-1,exCount,mem):Integer.MIN_VALUE;finalintplaceExtrovert=exCount>0?40+getPlacementCost(n,i,j,inMask,exMask,20)+getMaxGridHappiness(m,n,pos+1,shiftedInMask,shiftedExMask|1,inCount,exCount-1,mem):Integer.MIN_VALUE;returnmem[pos][inMask][exMask][inCount][exCount]=Math.max(skip,Math.max(placeIntrovert,placeExtrovert));}}
classSolution:defgetMaxGridHappiness(self,m:int,n:int,introvertsCount:int,extrovertsCount:int,)->int:defgetPlacementCost(i:int,j:int,inMask:int,exMask:int,diff:int,)->int:"""Calculates the cost based on left and up neighbors. The `diff` parameter represents the happiness change due to the current placed person in (i, j). We add `diff` each time we encounter a neighbor (left or up) who is already placed. 1. If the neighbor is an introvert, we subtract 30 from cost. 2. If the neighbor is an extrovert, we add 20 to from cost. """cost=0ifi>0:if(1<<(n-1))&inMask:cost+=diff-30if(1<<(n-1))&exMask:cost+=diff+20ifj>0:if1&inMask:cost+=diff-30if1&exMask:cost+=diff+20returncost@functools.lru_cache(None)defdp(pos:int,inMask:int,exMask:int,inCount:int,exCount:int)->int:# `inMask` is the placement of introvert people in the last n cells.# e.g. if we have m = 2, n = 3, i = 1, j = 1, then inMask = 0b101 means## ? 1 0# 1 x ? (x := current position)i,j=divmod(pos,n)ifi==m:return0shiftedInMask=(inMask<<1)&((1<<n)-1)shiftedExMask=(exMask<<1)&((1<<n)-1)skip=dp(pos+1,shiftedInMask,shiftedExMask,inCount,exCount)placeIntrovert=(120+getPlacementCost(i,j,inMask,exMask,-30)+dp(pos+1,shiftedInMask+1,shiftedExMask,inCount-1,exCount)ifinCount>0else-math.inf)placeExtrovert=(40+getPlacementCost(i,j,inMask,exMask,20)+dp(pos+1,shiftedInMask,shiftedExMask+1,inCount,exCount-1)ifexCount>0else-math.inf)returnmax(skip,placeIntrovert,placeExtrovert)returndp(0,0,0,introvertsCount,extrovertsCount)
Thanks for stopping by! If you find this site helpful, consider buying me some protein powder to keep me fueled! π