Advertisement
Iam_Sandeep

Untitled

Jul 1st, 2022
689
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.45 KB | None
  1. import sys
  2. sys.setrecursionlimit(10**9)
  3. from functools import lru_cache
  4. def wordWrap(words, k, n):
  5.     @lru_cache()
  6.     def help(c,i):
  7.         #print(c,i)
  8.         if i==n:return (k-c+1)**3
  9.         l=nums[i]
  10.         ans=float('inf')
  11.         rem=k-c
  12.         if l<=rem:
  13.            ans=min(ans,help(c+l+1,i+1))
  14.         if c!=0:
  15.             ans=min(ans,help(0,i)+(1+rem)**3)
  16.         return ans
  17.     nums=[len(i) for i in words]
  18.     return help(0,0)
  19.  
  20.  
  21.  
Advertisement
RAW Paste Data Copied
Advertisement