question_slug
stringlengths
3
77
title
stringlengths
1
183
slug
stringlengths
12
45
summary
stringlengths
1
160
author
stringlengths
2
30
certification
stringclasses
2 values
created_at
stringdate
2013-10-25 17:32:12
2025-04-12 09:38:24
updated_at
stringdate
2013-10-25 17:32:12
2025-04-12 09:38:24
hit_count
int64
0
10.6M
has_video
bool
2 classes
content
stringlengths
4
576k
upvotes
int64
0
11.5k
downvotes
int64
0
358
tags
stringlengths
2
193
comments
int64
0
2.56k
minimize-the-difference-between-target-and-chosen-elements
c++ bitset. meh...
c-bitset-meh-by-0xffffffff-agsj
p is the bitset from 0 to 5000 (big enough) if the bit i is set, which means i can be the sum result by far, and do it row by row. \n\n\nclass Solution {\npubli
0xffffffff
NORMAL
2021-08-22T04:03:37.132860+00:00
2021-08-22T04:13:59.217133+00:00
6,026
false
p is the bitset from 0 to 5000 (big enough) if the bit `i` is set, which means `i` can be the sum result by far, and do it row by row. \n\n```\nclass Solution {\npublic:\n int minimizeTheDifference(vector<vector<int>>& mat, int target) {\n bitset<5000> p(1);\n for (auto& r : mat) {\n bitset<...
72
5
[]
23
minimize-the-difference-between-target-and-chosen-elements
c++ solution using dp and memoization
c-solution-using-dp-and-memoization-by-d-e3gz
https://leetcode.com/problems/cherry-pickup/\n similar problem\n\nclass Solution \n{\npublic:\n int dp[8000][71];\n int n,m;\n int find(vector<vector<i
dilipsuthar17
NORMAL
2021-08-22T06:02:21.135283+00:00
2022-01-20T08:45:24.992519+00:00
8,320
false
[https://leetcode.com/problems/cherry-pickup/](http://)\n similar problem\n```\nclass Solution \n{\npublic:\n int dp[8000][71];\n int n,m;\n int find(vector<vector<int>>&mat,int r,int sum,int &target)\n {\n if(r>=n)\n {\n return abs(sum-target);\n }\n if(dp[sum][r]!=-1...
52
5
['Dynamic Programming', 'Memoization', 'C', 'C++']
11
minimize-the-difference-between-target-and-chosen-elements
[JAVA] DP Memoization
java-dp-memoization-by-grvk28-xwn9
Straightforward memoization solution\n\n\n\nclass Solution {\n public int minimizeTheDifference(int[][] mat, int target) {\n Integer[][] dp = new Inte
grvk28
NORMAL
2021-08-22T05:11:22.586689+00:00
2021-08-22T05:11:58.265553+00:00
4,705
false
Straightforward memoization solution\n\n\n```\nclass Solution {\n public int minimizeTheDifference(int[][] mat, int target) {\n Integer[][] dp = new Integer[mat.length][5001];\n return minDiff(mat, 0, target,0, dp);\n }\n \n public int minDiff(int[][] mat,int index,int target, int val, Integer...
48
0
['Dynamic Programming', 'Memoization', 'Java']
8
minimize-the-difference-between-target-and-chosen-elements
Thought Process || C++ || DP + Memoization || Explanation
thought-process-c-dp-memoization-explana-7u9e
If this helped you in anyway, please consider giving an UPVOTE\n\nIntution :\n\n Looking at the constraints we can surely think of a brute force approach, which
i_quasar
NORMAL
2021-09-10T15:45:39.305396+00:00
2021-09-10T15:45:39.305443+00:00
3,396
false
**If this helped you in anyway, please consider giving an UPVOTE**\n\n**Intution :**\n\n* Looking at the constraints we can surely think of a brute force approach, which we can later optimize. \n* Now, in the question we are asked to pick a element from each row and find the total sum. Then, out of all possible combina...
44
3
['Dynamic Programming', 'Memoization', 'C']
5
minimize-the-difference-between-target-and-chosen-elements
✅ 100% efficient | Pruning + Memoization | Dynamic Programming | Explanation
100-efficient-pruning-memoization-dynami-am3b
Explanation:\n\n\nPlease note following important observations from question,\n1. We must choose exactly one element from each row\n2. Minimize the diff between
captainx
NORMAL
2021-08-22T04:02:31.696345+00:00
2021-08-22T13:11:54.033054+00:00
3,976
false
**Explanation:**\n\n\nPlease note following important observations from question,\n1. We must choose exactly one element from each row\n2. Minimize the diff between target and the sum of all chosen elements\n\nApproach (and how to solve TLE)\n\nNow, the general idea is to explore all possibilities, however, that\'ll be...
30
1
['Dynamic Programming', 'Memoization', 'Python', 'Python3']
8
minimize-the-difference-between-target-and-chosen-elements
Java dp code with proper comments and explanation
java-dp-code-with-proper-comments-and-ex-2m2r
We can simply iterate over all the possible cases and memoise the already taken path to avoid repetitive calculation.\nDp with states as current row and sum til
Jay_Bisht
NORMAL
2021-08-22T04:01:29.037568+00:00
2021-08-22T04:01:29.037609+00:00
2,132
false
We can simply iterate over all the possible cases and memoise the already taken path to avoid repetitive calculation.\nDp with states as current row and sum till now would do it.\n```\n int ans = Integer.MAX_VALUE;\n boolean[][] dp; // For memoisation\n void func(int[][] mat , int r, int sum, int t){ // where...
16
7
[]
4
minimize-the-difference-between-target-and-chosen-elements
from TLEs To getting Accepted | C++
from-tles-to-getting-accepted-c-by-spart-wuh3
Brute Force\nThe Brute Force Way of doing it is exploring all cells from each row and passing our decisions downwards.\n\nBase Case\nWhat if you have only 1 row
spartanleonidis
NORMAL
2021-08-22T04:28:10.281140+00:00
2021-09-04T08:13:45.496013+00:00
1,888
false
**Brute Force**\nThe Brute Force Way of doing it is exploring all cells from each row and passing our decisions downwards.\n\n***Base Case***\nWhat if you have only 1 row ? then you choose the cell which gives minimum difference with target\n\nBut this is going to get TLE:\n\n\n**Observation**\n\nNext step to a recursi...
15
0
[]
3
minimize-the-difference-between-target-and-chosen-elements
Python [set]
python-set-by-gsan-hq4p
python\nclass Solution:\n def minimizeTheDifference(self, mat: List[List[int]], target: int) -> int:\n R, C = len(mat), len(mat[0])\n \n
gsan
NORMAL
2021-08-22T04:04:44.504384+00:00
2021-08-22T04:04:44.504411+00:00
1,358
false
```python\nclass Solution:\n def minimizeTheDifference(self, mat: List[List[int]], target: int) -> int:\n R, C = len(mat), len(mat[0])\n \n S = set(mat[0])\n for r in range(1, R):\n NS = set()\n for x in set(mat[r]):\n for y in S:\n ...
14
0
[]
3
minimize-the-difference-between-target-and-chosen-elements
Simple C++ (Recursion + Memo)
simple-c-recursion-memo-by-mazhar_mik-nm6q
This solution was accepted earlier.\nAccepted:\n\n\n\nclass Solution {\npublic:\n int t[5001][71];\n int row, col;\n \n int find(vector<vector<int>>
mazhar_mik
NORMAL
2021-08-22T04:14:04.095739+00:00
2021-09-01T19:12:38.558961+00:00
1,088
false
This solution was accepted earlier.\nAccepted:\n![image](https://assets.leetcode.com/users/images/1142deb7-90af-4830-b69a-dba21cffbf0d_1629606425.2784035.png)\n\n```\nclass Solution {\npublic:\n int t[5001][71];\n int row, col;\n \n int find(vector<vector<int>>& mat, int row, int sum, int& target)\n {\n ...
12
5
[]
4
minimize-the-difference-between-target-and-chosen-elements
Easy to Understand Java Solution with Explanation
easy-to-understand-java-solution-with-ex-3ud5
Sharing easy to understand solution using 2 sets.\n\nIdea:\n1) Brute Force (TLE): Create 2 sets (1 representing all the possible sums up to but not including th
simonzhu91
NORMAL
2021-08-22T04:27:36.129699+00:00
2021-08-22T04:30:42.023110+00:00
1,553
false
Sharing easy to understand solution using 2 sets.\n\nIdea:\n1) **Brute Force (TLE):** Create 2 sets (1 representing all the possible sums up to but not including the current row, 1 representing all the possible sums up to and including the current row). Iterate matrix row by row, for each element in set, column by colu...
11
0
[]
6
minimize-the-difference-between-target-and-chosen-elements
Python 3 || 4 lines, sets || T/S: 58% / 51%
python-3-4-lines-sets-ts-58-51-by-spauld-r872
\nclass Solution:\n def minimizeTheDifference(self, mat: List[List[int]], target: int) -> int:\n\n mat = [set(row) for row in mat]\n \n
Spaulding_
NORMAL
2023-02-12T19:41:30.028602+00:00
2024-06-22T22:01:23.054970+00:00
704
false
```\nclass Solution:\n def minimizeTheDifference(self, mat: List[List[int]], target: int) -> int:\n\n mat = [set(row) for row in mat]\n \n rSet = set(mat.pop())\n\n for row in mat: rSet = {m+n for m in row for n in rSet}\n \n return min(abs(n - target) for n in rSet)\n\n```\...
10
0
['Python3']
1
minimize-the-difference-between-target-and-chosen-elements
Simple DP Solution||C++
simple-dp-solutionc-by-rk_code-sa9l
\nclass Solution {\npublic:\n int dp[5001][71];\n int find(vector<vector<int>>& mat,int n,int sum,int tar)\n {\n if(n<=0)\n return ab
rk_code
NORMAL
2021-08-22T04:01:26.441098+00:00
2021-08-22T04:01:26.441135+00:00
999
false
```\nclass Solution {\npublic:\n int dp[5001][71];\n int find(vector<vector<int>>& mat,int n,int sum,int tar)\n {\n if(n<=0)\n return abs(sum-tar);\n if(dp[sum][n]!=-1)return dp[sum][n];\n \n int ans=INT_MAX;\n for(int j=0;j<mat[n-1].size();j++)\n {\n ...
9
7
[]
10
minimize-the-difference-between-target-and-chosen-elements
simple c++ memoization
simple-c-memoization-by-ranjan_1997-msta
\nclass Solution {\npublic:\n int pq[10000][100];\n int helper(vector<vector<int>>&mat,int row,int sum,int target)\n { \n if(row >= mat.size()
ranjan_1997
NORMAL
2021-08-23T06:03:43.570741+00:00
2021-08-23T06:03:43.570794+00:00
908
false
```\nclass Solution {\npublic:\n int pq[10000][100];\n int helper(vector<vector<int>>&mat,int row,int sum,int target)\n { \n if(row >= mat.size())\n return abs(sum-target);\n if(pq[sum][row] != -1)\n return pq[sum][row];\n int ans = INT_MAX;\n for(int i = 0;i<...
8
1
[]
0
minimize-the-difference-between-target-and-chosen-elements
Python Set and Early Stop (2700ms)
python-set-and-early-stop-2700ms-by-john-y22t
\nclass Solution:\n def minimizeTheDifference(self, mat: List[List[int]], target: int) -> int:\n possibleSum = set([0])\n for m in mat:\n
johnnylu305
NORMAL
2021-08-22T04:31:22.566844+00:00
2021-08-22T04:31:22.566872+00:00
580
false
```\nclass Solution:\n def minimizeTheDifference(self, mat: List[List[int]], target: int) -> int:\n possibleSum = set([0])\n for m in mat:\n temp = set()\n for y in possibleSum:\n for x in sorted(set(m)):\n temp.add(x+y)\n if x+...
7
1
['Python']
2
minimize-the-difference-between-target-and-chosen-elements
Simple C++ Bottom Up Approach O(n*m*target)
simple-c-bottom-up-approach-onmtarget-by-g12w
\n\nclass Solution {\npublic:\n \n int m, n;\n \n int minimizeTheDifference(vector<vector<int>>& mat, int target) {\n \n m = mat.size(
akshitgarg09
NORMAL
2021-08-22T04:12:38.056514+00:00
2021-08-22T04:15:37.405029+00:00
1,332
false
```\n\nclass Solution {\npublic:\n \n int m, n;\n \n int minimizeTheDifference(vector<vector<int>>& mat, int target) {\n \n m = mat.size();\n n = mat[0].size();\n \n vector<vector<int>> dp(m+1, vector<int>(target+1, INT_MAX));\n \n // dp[i][j] ans for upto it...
7
1
['Dynamic Programming', 'C', 'C++']
2
minimize-the-difference-between-target-and-chosen-elements
Bit-Manipulation (Bitset)|| Meet in the Middle || C++ ||
bit-manipulation-bitset-meet-in-the-midd-1rk9
Intuition\n Describe your first thoughts on how to solve this problem. \nPoints to Notice:-\n\nQuestion 1755 - https://leetcode.com/problems/closest-subsequence
intern_grind
NORMAL
2023-06-15T06:12:18.852841+00:00
2023-06-15T07:10:23.949729+00:00
151
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nPoints to Notice:-\n\nQuestion 1755 - [https://leetcode.com/problems/closest-subsequence-sum/]()\nQuestion 2035 - [https://leetcode.com/problems/partition-array-into-two-arrays-to-minimize-sum-difference/]()\n\nIn these two questions, MEE...
5
0
['Bit Manipulation', 'Bitmask', 'C++']
1
minimize-the-difference-between-target-and-chosen-elements
python dp using @cache
python-dp-using-cache-by-takaos-0xk8
\nclass Solution:\n def minimizeTheDifference(self, mat: List[List[int]], target: int) -> int:\n m = len(mat)\n \n for i in range(m):\n
takasoft
NORMAL
2021-08-22T05:01:34.265857+00:00
2021-08-22T05:02:01.448209+00:00
912
false
```\nclass Solution:\n def minimizeTheDifference(self, mat: List[List[int]], target: int) -> int:\n m = len(mat)\n \n for i in range(m):\n mat[i] = set(mat[i]) # remove duplicates\n \n self.min_diff = inf\n \n @cache\n def search(i, s...
5
0
['Dynamic Programming', 'Python']
0
minimize-the-difference-between-target-and-chosen-elements
Python 3 - DP with Bit Masking
python-3-dp-with-bit-masking-by-yixianns-eh36
Intuition\nUpon first consideration, this problem resembles a variant of the knapsack problem, where we aim to find a combination of elements (one from each row
yixianns
NORMAL
2024-01-25T08:19:42.748426+00:00
2024-01-25T08:19:42.748447+00:00
267
false
# Intuition\nUpon first consideration, this problem resembles a variant of the knapsack problem, where we aim to find a combination of elements (one from each row of the matrix) that sums up as close as possible to a given target. A direct approach would be to enumerate all possible combinations, but that would be inef...
4
0
['Python3']
1
minimize-the-difference-between-target-and-chosen-elements
✅Accepted || ✅Short & Simple || ✅Best Method || ✅Easy-To-Understand
accepted-short-simple-best-method-easy-t-od2x
\n# Code\n\nclass Solution {\npublic:\n int m, n;\n int dp[70*70+1][70];\n int dfs(vector<vector<int>>& mat, int r, int sum, int target)\n {\n
sanjaydwk8
NORMAL
2023-01-29T08:28:02.460794+00:00
2023-01-29T08:28:02.460829+00:00
831
false
\n# Code\n```\nclass Solution {\npublic:\n int m, n;\n int dp[70*70+1][70];\n int dfs(vector<vector<int>>& mat, int r, int sum, int target)\n {\n if(r>=m)\n return abs(sum-target);\n if(dp[sum][r]!=-1)\n return dp[sum][r];\n int ans=INT_MAX;\n for(int i=0;i<n;i++)\n...
4
0
['C++']
1
minimize-the-difference-between-target-and-chosen-elements
C++, using bitset in O(1) space
c-using-bitset-in-o1-space-by-roshan_186-t4r0
Intuition\n->1st observation is that sum <= 70 * 70 \n->2nd obserbation is that to reduce space complexity (solution with dp have same complexity of O(500071))
Roshan_1860
NORMAL
2023-01-28T08:23:05.076567+00:00
2023-01-28T08:24:40.992840+00:00
197
false
# Intuition\n->1st observation is that sum <= 70 * 70 \n->2nd obserbation is that to reduce space complexity (solution with dp have same complexity of O(5000*71)) we have to use something that can tell in linear time that if this sum is possible or not. so bitset<> is best for that \n\n# Approach\n1. First row values m...
4
0
['Bit Manipulation', 'C++']
1
minimize-the-difference-between-target-and-chosen-elements
[Java] Short and Concise DP-Memoize solution
java-short-and-concise-dp-memoize-soluti-zb9j
Logic : Create a 2-D dp array with dimesions row*(max sum possible) . Traverse through each path possible from first row to last row taking one element form eac
chemEn
NORMAL
2021-08-22T04:10:05.096102+00:00
2021-08-22T18:02:14.223000+00:00
766
false
Logic : Create a 2-D dp array with dimesions row*(max sum possible) . Traverse through each path possible from first row to last row taking one element form each row . \n\n```\nclass Solution {\n\n int[][] dp;\n public int minimizeTheDifference(int[][] mat, int target) {\n dp=new int[mat.length][4901];\n ...
4
0
['Java']
1
minimize-the-difference-between-target-and-chosen-elements
DP memoization || Easy to understand || C++
dp-memoization-easy-to-understand-c-by-p-uruf
\nclass Solution {\npublic:\n int dp[71][5701];\n int minDiff(vector<vector<int>>& mat,int ind,int sum){\n if(ind==mat.size())return abs(sum);\n
praveen1208
NORMAL
2021-08-22T04:06:34.080252+00:00
2024-08-31T18:17:23.167654+00:00
661
false
```\nclass Solution {\npublic:\n int dp[71][5701];\n int minDiff(vector<vector<int>>& mat,int ind,int sum){\n if(ind==mat.size())return abs(sum);\n \n if(dp[ind][4900+sum]!=-1) return dp[ind][4900+sum];\n int res=INT_MAX;\n for(int i=0;i<mat[0].size();i++){\n res=min(res,mi...
4
1
['Dynamic Programming', 'Memoization', 'C']
2
minimize-the-difference-between-target-and-chosen-elements
✅Clean DP Code || ⚡Memoization
clean-dp-code-memoization-by-adish_21-o2uo
\n\n# Complexity\n\n- Time complexity:\nO(m * m * 70)\n\n- Space complexity:\nO(m * m * 70)\n\n\n# Code\n## Please Upvote if it helps\uD83E\uDD17\n\nclass Solut
aDish_21
NORMAL
2024-06-09T12:38:40.829197+00:00
2024-06-09T12:38:40.829222+00:00
385
false
\n\n# Complexity\n```\n- Time complexity:\nO(m * m * 70)\n\n- Space complexity:\nO(m * m * 70)\n```\n\n# Code\n## Please Upvote if it helps\uD83E\uDD17\n```\nclass Solution {\npublic:\n int dp[71][4901];\n int helper(int i, int m, int n, vector<vector<int>>& mat, int target, int curr_sum){\n if(i == m)\n ...
3
0
['Array', 'Dynamic Programming', 'Memoization', 'Matrix', 'C++']
0
minimize-the-difference-between-target-and-chosen-elements
Bitset Solution
bitset-solution-by-techtinkerer-bn9h
\nm=mat.size();\nn=mat[0].size();\n# Complexity\n- Time complexity:O(mn(maxSum/64))\n Add your time complexity here, e.g. O(n) \n\n- Space complexity:O(maxSum)\
TechTinkerer
NORMAL
2024-06-09T10:38:26.915173+00:00
2024-06-09T10:38:26.915205+00:00
198
false
\nm=mat.size();\nn=mat[0].size();\n# Complexity\n- Time complexity:O(m*n*(maxSum/64))\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(maxSum)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n int minimizeTheDifference(vector<vector<int>...
3
0
['C++']
0
minimize-the-difference-between-target-and-chosen-elements
[C++] Simple C++ Code
c-simple-c-code-by-prosenjitkundu760-dkue
If you like the implementation then Please help me by increasing my reputation. By clicking the up arrow on the left of my image.\n\nclass Solution {\n int a
_pros_
NORMAL
2022-08-04T04:58:26.671030+00:00
2022-08-04T04:58:26.671075+00:00
772
false
# **If you like the implementation then Please help me by increasing my reputation. By clicking the up arrow on the left of my image.**\n```\nclass Solution {\n int ans = INT_MAX;\n void dfs(int x, int s, vector<vector<int>> &dp, vector<vector<int>>& mat, int target, int n, int m)\n {\n if(s-ans >= targ...
3
1
['Dynamic Programming', 'Recursion', 'Memoization', 'C', 'C++']
2
minimize-the-difference-between-target-and-chosen-elements
[Python] BruteForce with memo
python-bruteforce-with-memo-by-artod-vg7q
The bruteforce solution here is just to calculate all possible sums and then find the best that gives minium absolut difference with target. \n\nIf we start ite
artod
NORMAL
2021-11-11T04:56:01.845335+00:00
2021-11-11T05:39:24.793647+00:00
291
false
The bruteforce solution here is just to calculate all possible sums and then find the best that gives minium absolut difference with `target`. \n\nIf we start iterating all possible combinations of elements in columns, that algorithm would take O(m^n) time. So we are trying to optimize it by iterating the matrix from t...
3
1
['Dynamic Programming', 'Memoization']
1
minimize-the-difference-between-target-and-chosen-elements
C++ Solution Using DP
c-solution-using-dp-by-ariyanlaskar-7wy4
\n int dp[5001][71];\n int minimizeTheDifference(vector<vector<int>>& mat,int target) {\n memset(dp,-1,sizeof(dp));\n int n=mat.size();\n
Ariyanlaskar
NORMAL
2021-10-24T10:53:02.702155+00:00
2021-10-24T10:53:02.702211+00:00
451
false
```\n int dp[5001][71];\n int minimizeTheDifference(vector<vector<int>>& mat,int target) {\n memset(dp,-1,sizeof(dp));\n int n=mat.size();\n return findmat(mat,0,0,target);\n }\n int findmat(vector<vector<int>>&mat,int i,int sum,int &target){\n if(i>=mat.size()){\n retur...
3
2
['Dynamic Programming', 'Memoization']
0
minimize-the-difference-between-target-and-chosen-elements
[Python3] Fast Fourier Transform O(sum*log(sum)*m)
python3-fast-fourier-transform-osumlogsu-jram
Using FFT for faster Sumset, works only for small bounded values(in this question sum cannot exceed 4900).\n\nfrom numpy.fft import rfft, irfft\nclass Solution:
adihosapate
NORMAL
2021-08-22T04:36:21.782598+00:00
2021-08-22T04:46:17.036063+00:00
181
false
Using FFT for faster Sumset, works only for small bounded values(in this question sum cannot exceed 4900).\n```\nfrom numpy.fft import rfft, irfft\nclass Solution:\n \n \n def fftrealpolymul(self,arr_a, arr_b): #fft based real-valued polynomial multiplication\n L = len(arr_a) + len(arr_b)\n a_f ...
3
0
[]
0
minimize-the-difference-between-target-and-chosen-elements
java easy to understand dp
java-easy-to-understand-dp-by-ratatat-rarn
\nclass Solution {\n int min=Integer.MAX_VALUE;\n boolean[][] dp;\n public int minimizeTheDifference(int[][] mat, int target) {\n dp = new boole
ratAtat
NORMAL
2021-08-22T04:14:53.728286+00:00
2021-08-22T04:14:53.728322+00:00
268
false
```\nclass Solution {\n int min=Integer.MAX_VALUE;\n boolean[][] dp;\n public int minimizeTheDifference(int[][] mat, int target) {\n dp = new boolean[mat.length][5000];\n backTrack(0,mat, target, 0);\n return min;\n }\n \n public void backTrack(int row, int[][] mat, int target, in...
3
1
[]
2
minimize-the-difference-between-target-and-chosen-elements
✅✅✅100% working solution in java || dp || memoization✅✅
100-working-solution-in-java-dp-memoizat-hzi4
```\nclass Solution {\n public int minimizeTheDifference(int[][] mat, int t) {\n int m= mat.length;\n int[][] dp= new int[m][5001];\n fo
sona_14_28
NORMAL
2023-03-25T10:44:50.125571+00:00
2023-03-25T10:44:50.125606+00:00
1,040
false
```\nclass Solution {\n public int minimizeTheDifference(int[][] mat, int t) {\n int m= mat.length;\n int[][] dp= new int[m][5001];\n for(int i=0; i<m; i++){\n for(int j=0; j<5001; j++){\n dp[i][j]=-1;\n }\n }\n return solve(mat,dp,0,t,0);\n ...
2
0
['Dynamic Programming', 'Memoization', 'Java']
0
minimize-the-difference-between-target-and-chosen-elements
C++ simple solution
c-simple-solution-by-akshat0610-wvpx
\nclass Solution {\npublic:\n int dp[4901][71];\n int minimizeTheDifference(vector<vector<int>>& mat, int target) \n {\n //int fun(vector<vector
akshat0610
NORMAL
2022-10-15T06:11:58.671507+00:00
2022-10-15T06:11:58.671533+00:00
975
false
```\nclass Solution {\npublic:\n int dp[4901][71];\n int minimizeTheDifference(vector<vector<int>>& mat, int target) \n {\n //int fun(vector<vector<int>>&mat,int &target,int sum,int row);\n memset(dp,-1,sizeof(dp));\n int row=0;\n int sum=0;\n return fun(mat,target,sum,row);\...
2
0
['Dynamic Programming', 'Depth-First Search', 'Recursion', 'Memoization', 'C', 'C++']
0
minimize-the-difference-between-target-and-chosen-elements
[Java] [DP] Clean Code
java-dp-clean-code-by-java-lang-goutam-kz9i
```\nclass Solution {\n \n int min = Integer.MAX_VALUE;\n\n public int minimizeTheDifference(int[][] mat, int target) {\n final int maxM = 70;\n
java-lang-goutam
NORMAL
2022-06-04T03:53:26.565449+00:00
2022-06-04T03:55:55.706488+00:00
247
false
```\nclass Solution {\n \n int min = Integer.MAX_VALUE;\n\n public int minimizeTheDifference(int[][] mat, int target) {\n final int maxM = 70;\n final int maxN = 70;\n boolean[][] dp = new boolean[mat.length][(maxM * maxN) + 1];\n helper(mat, target, dp, 0, 0); \n return mi...
2
0
['Memoization']
1
minimize-the-difference-between-target-and-chosen-elements
C++ || DP Solution
c-dp-solution-by-ashu_iitp-4he6
\nclass Solution {\npublic:\n \n int n,m,goal;\n \n int dp[71][4905];\n vector<int> v;\n \n int solve(vector<vector<int>>& mat,int i,int su
ashu_iitp
NORMAL
2022-04-01T19:32:46.066174+00:00
2022-04-01T19:32:46.066213+00:00
403
false
```\nclass Solution {\npublic:\n \n int n,m,goal;\n \n int dp[71][4905];\n vector<int> v;\n \n int solve(vector<vector<int>>& mat,int i,int sum)\n {\n if(i>=n) \n {\n //picking element from each row and taking abs diff of all possible path.\n return abs(sum-go...
2
0
[]
1
minimize-the-difference-between-target-and-chosen-elements
illustrated explanation
illustrated-explanation-by-wilmerkrisp-ww0i
<-- please vote\n\n\n\n def minimizeTheDifference1(self, mat, target):\n nums = {0}\n [nums := {x + sx for x in row for sx in nums} for row in
wilmerkrisp
NORMAL
2022-02-24T18:21:53.184047+00:00
2022-02-24T18:21:53.184075+00:00
159
false
<-- please vote\n\n![image](https://assets.leetcode.com/users/images/1daca118-d4f3-442e-b20a-98cfb90ba368_1645726902.1581254.png)\n\n def minimizeTheDifference1(self, mat, target):\n nums = {0}\n [nums := {x + sx for x in row for sx in nums} for row in mat]\n return min(abs(target - x) for x in ...
2
2
['Math', 'Python']
2
minimize-the-difference-between-target-and-chosen-elements
Easy C++ SOL using DP
easy-c-sol-using-dp-by-rashmimishra20156-xpxr
The question states that we need to pick one element from each row , such that \nthe absolute differnce b/w the sum and target is minimized. In other words, th
rashmiMishra20156
NORMAL
2022-02-08T19:17:03.441757+00:00
2022-02-08T19:17:03.441804+00:00
399
false
The question states that we need to pick one element from each row , such that \nthe absolute differnce b/w the sum and target is minimized. In other words, their sum should be close to target.\n\nSimple approach is try all possible combinatoins /paths, return the minimum value of abs(target-sum).\nHere , our result d...
2
0
['Memoization', 'C']
1
minimize-the-difference-between-target-and-chosen-elements
56 ms, faster than 98.23% of Java DFS + memo solution
56-ms-faster-than-9823-of-java-dfs-memo-h9w40
Time: n(mlogm + m) + 2^(nm)\nSpace: n* target\n\n\nclass Solution {\n public int minimizeTheDifference(int[][] mat, int target) {\n //1. Prepare proc
anquan
NORMAL
2021-09-12T21:20:17.465874+00:00
2021-09-12T21:21:31.713490+00:00
210
false
Time: n*(mlogm + m) + 2^(n*m)\nSpace: n* target\n\n```\nclass Solution {\n public int minimizeTheDifference(int[][] mat, int target) {\n //1. Prepare process\n int remain = target;\n for (int i = 0; i < mat.length; i++) {\n\t\t\tArrays.sort(mat[i]); //1) sort each row\n \n ...
2
0
[]
1
minimize-the-difference-between-target-and-chosen-elements
[Python3] 2 solutions - dp & bitset
python3-2-solutions-dp-bitset-by-ye15-h375
Please check this commit for solutions of weekly 255. \n\nApproach 1 -- dp (9228ms, 8.91%)\n\nclass Solution:\n def minimizeTheDifference(self, mat: List[Lis
ye15
NORMAL
2021-08-24T20:07:24.008487+00:00
2021-08-24T21:52:48.745645+00:00
316
false
Please check this [commit](https://github.com/gaosanyong/leetcode/commit/12942515a0e681f7ed804e66ec8de2b3423cadb0) for solutions of weekly 255. \n\n**Approach 1 -- dp** (9228ms, 8.91%)\n```\nclass Solution:\n def minimizeTheDifference(self, mat: List[List[int]], target: int) -> int:\n m, n = len(mat), len(mat...
2
1
['Python3']
2
minimize-the-difference-between-target-and-chosen-elements
100% Efficient | Simple Recursion + Memoization
100-efficient-simple-recursion-memoizati-iycc
\n\nclass Solution {\npublic:\n int recc(vector<vector<int>>& mat, int sum,int n,vector<vector<int>>&m){\n if(n==0){\n return abs(sum);\n
dsslayer
NORMAL
2021-08-23T13:10:39.479128+00:00
2021-08-23T13:17:53.267629+00:00
426
false
```\n\nclass Solution {\npublic:\n int recc(vector<vector<int>>& mat, int sum,int n,vector<vector<int>>&m){\n if(n==0){\n return abs(sum);\n }\n if(sum<=0){\n return INT_MAX;\n }\n if(m[n][sum]!=-1){\n return m[n][sum];\n }\n int ans...
2
0
['Recursion', 'Memoization', 'C']
1
minimize-the-difference-between-target-and-chosen-elements
C++ 0-1 Knapsack Implementation
c-0-1-knapsack-implementation-by-galexw-hgl4
```\nclass Solution {\npublic:\n int minimizeTheDifference(vector>& mat, int target) {\n int m = mat.size();\n\n int MAX_SUM = 70 * 70;\n vect
galexw
NORMAL
2021-08-22T21:59:36.993663+00:00
2021-08-22T21:59:50.315386+00:00
218
false
```\nclass Solution {\npublic:\n int minimizeTheDifference(vector<vector<int>>& mat, int target) {\n int m = mat.size();\n\n int MAX_SUM = 70 * 70;\n vector<vector<bool>> dp(m, vector<bool>(MAX_SUM + 1));\n\n for (auto num : mat[0]) {\n dp[0][num] = true;\n }\n\n for (int i = 0; ...
2
0
[]
0
minimize-the-difference-between-target-and-chosen-elements
[C++] Simple O(m * n * target) DP solution
c-simple-om-n-target-dp-solution-by-code-2bop
Here is yet another tribute to my idol @lee215\n\nIntuition:\n1. Think of it similar to 0-1 knapsack problem. Here, the difference is that you have to choose on
coderplustester
NORMAL
2021-08-22T11:22:48.737474+00:00
2021-08-22T11:22:48.737507+00:00
148
false
Here is yet another tribute to my idol @lee215\n\nIntuition:\n1. Think of it similar to 0-1 knapsack problem. Here, the difference is that you have to choose one column in every row in this case and you have to choose an element.\n \n```\nclass Solution {\npublic:\n int minimizeTheDifference(vector<vector<int>>& mat...
2
0
[]
1
minimize-the-difference-between-target-and-chosen-elements
Easy Well Commented: Memoization
easy-well-commented-memoization-by-raj_s-exbg
\nclass Solution {\npublic:\n int dp[5001][71];\n int mini = INT_MAX;\n int find(vector<vector<int>>& mat,int i,int sum,int tar)\n {\n\t\tif(dp[sum]
raj_srivastava
NORMAL
2021-08-22T09:09:48.591593+00:00
2022-02-06T08:33:31.489962+00:00
186
false
```\nclass Solution {\npublic:\n int dp[5001][71];\n int mini = INT_MAX;\n int find(vector<vector<int>>& mat,int i,int sum,int tar)\n {\n\t\tif(dp[sum][i]!=-1) return dp[sum][i];\n\t\t//Base Case\n\t\t//When we consume all the rows , update the min answer till now.\n if(i>=mat.size()){\n ...
2
0
['Dynamic Programming', 'C', 'Sorting']
0
minimize-the-difference-between-target-and-chosen-elements
C#
c-by-ve7545-zpri
Runtime: 1417 ms\nMemory Usage: 29.8 MB\n\npublic class Solution {\n public int MinimizeTheDifference(int[][] mat, int target) {\n HashSet<int> crnt =
ve7545
NORMAL
2021-08-22T04:42:31.124962+00:00
2021-08-22T04:42:31.124988+00:00
94
false
Runtime: 1417 ms\nMemory Usage: 29.8 MB\n```\npublic class Solution {\n public int MinimizeTheDifference(int[][] mat, int target) {\n HashSet<int> crnt = new HashSet<int>();\n HashSet<int> prev = new HashSet<int>();\n HashSet<int> temp;\n \n int next = int.MaxValue;\n \n ...
2
1
[]
0
minimize-the-difference-between-target-and-chosen-elements
JAVA || EASY || SOLUTION
java-easy-solution-by-ritwikrst-3m5m
\n private int min; \n private int[][] visited; \n public void solve(int[][] a, int sum, int row, int target){\n if(row>=a.length){\n m
ritwikrst
NORMAL
2021-08-22T04:34:15.742287+00:00
2021-08-22T04:34:15.742378+00:00
349
false
```\n private int min; \n private int[][] visited; \n public void solve(int[][] a, int sum, int row, int target){\n if(row>=a.length){\n min=Math.min(min, Math.abs(target-sum));\n return;\n } \n if(visited[row][sum] == 1) \n return;\n for(int j=...
2
1
['Java']
1
minimize-the-difference-between-target-and-chosen-elements
dp 70*70*800
dp-7070800-by-kira3018-g17a
\nclass Solution {\npublic:\n int prefix[80] = {0};\n int minimizeTheDifference(vector<vector<int>>& mat, int target) \n {\n vector<vector<int>>
kira3018
NORMAL
2021-08-22T04:23:41.808788+00:00
2021-08-22T04:23:41.808824+00:00
141
false
```\nclass Solution {\npublic:\n int prefix[80] = {0};\n int minimizeTheDifference(vector<vector<int>>& mat, int target) \n {\n vector<vector<int>> vec(mat.size(),vector<int>(800,INT_MAX));\n for(int i = mat.size()-1;i >= 0;i--)\n {\n int a = INT_MAX;\n for(int j = 0;...
2
0
[]
0
minimize-the-difference-between-target-and-chosen-elements
C++ Straightforward Hash Set Solution
c-straightforward-hash-set-solution-by-z-2m5i
All possible sums are in the range [1, 4900], so we can pre-calculate them.\nAn optimization is for all sums > 800, only keep the smallest.\nC++\nclass Solution
ztyreg
NORMAL
2021-08-22T04:08:21.872966+00:00
2021-08-22T04:37:22.427175+00:00
162
false
All possible sums are in the range [1, 4900], so we can pre-calculate them.\nAn optimization is for all sums > 800, only keep the smallest.\n```C++\nclass Solution {\npublic:\n \n int minimizeTheDifference(vector<vector<int>>& mat, int target) {\n int m = mat.size();\n int n = mat[0].size();\n ...
2
0
['Ordered Set', 'C++']
0
minimize-the-difference-between-target-and-chosen-elements
[python3] simple dfs solution with optimizations
python3-simple-dfs-solution-with-optimiz-sjly
\nclass Solution:\n def minimizeTheDifference(self, mat: List[List[int]], target: int) -> int:\n ## RC ##\n ## APPROACH: DFS ##\n result
101leetcode
NORMAL
2021-08-22T04:04:12.059477+00:00
2021-08-22T04:51:01.615511+00:00
613
false
```\nclass Solution:\n def minimizeTheDifference(self, mat: List[List[int]], target: int) -> int:\n ## RC ##\n ## APPROACH: DFS ##\n result = float(\'inf\')\n cache = set()\n def dfs(row, res):\n nonlocal result\n \n if (row, res) in cache:\n ...
2
0
['Depth-First Search']
4
minimize-the-difference-between-target-and-chosen-elements
Simple Java Solution | memoization
simple-java-solution-memoization-by-hxgx-thik
Well its simple backtracking with memoization. Comments in the code are self explanatory!\n\nclass Solution {\n\n int abs; //global variabls to keep minimum
hxgxs1
NORMAL
2021-08-22T04:01:18.437912+00:00
2021-08-22T04:08:55.881964+00:00
516
false
Well its simple backtracking with memoization. Comments in the code are self explanatory!\n\nclass Solution {\n\n int abs; //global variabls to keep minimum abs value\n int[][] dp; // whenever we arrive at a particular row we need to see if we had previously arrived on this row with same sum.\n public void re...
2
0
['Dynamic Programming', 'Backtracking']
2
minimize-the-difference-between-target-and-chosen-elements
Tabulation code easy C++
tabulation-code-easy-c-by-sumitdarshanal-h2qs
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
sumitdarshanala
NORMAL
2024-06-11T17:11:53.933598+00:00
2024-06-11T17:11:53.933627+00:00
56
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n O(n*m*(70*70)) ~= cosidered 5000 approx\n\n- Space complexity:\n<!-- Add...
1
0
['Dynamic Programming', 'Memoization', 'C++']
0
minimize-the-difference-between-target-and-chosen-elements
Easy Python Solution
easy-python-solution-by-synch-ab0u
Thought process\n\nQ: What is the brute force method? \n\n-> recursion over each row, try every element in each row within the recursive function, which tracks
synch
NORMAL
2024-05-15T20:43:36.876833+00:00
2024-05-15T20:43:36.876886+00:00
213
false
# Thought process\n\nQ: What is the brute force method? \n\n-> recursion over each row, try every element in each row within the recursive function, which tracks `(idx, current_sum)`.\n\nQ: What is the bottleneck?\n-> For loops. How do we reduce the search space?\n-> 1. Naive idea: remove duplicate in the row. => itera...
1
0
['Python3']
0
minimize-the-difference-between-target-and-chosen-elements
Beats 50%, brute force, O(m*n^m)
beats-50-brute-force-omnm-by-igor0-hjnw
Intuition\nJust create a set for with all possible sums for each row.\n\n# Approach\nAt first create a set with all possible sums for the last row:\n\nvar set =
igor0
NORMAL
2024-02-21T20:23:48.990085+00:00
2024-02-21T20:23:48.990150+00:00
11
false
# Intuition\nJust create a set for with all possible sums for each row.\n\n# Approach\nAt first create a set with all possible sums for the last row:\n```\nvar set = new HashSet<int>(mat[mat.Length - 1]);\n```\nThen go through the all rows and create a set with all possible sums by each iteration:\n```\nfor (int i = ma...
1
0
['C#']
0
minimize-the-difference-between-target-and-chosen-elements
DP(Recursive) + Memoization optimize Solution
dprecursive-memoization-optimize-solutio-n72u
Complexity\n- Time complexity:O(nm)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity:O(nm)\n Add your space complexity here, e.g. O(n) \n\n# Co
Shree_Govind_Jee
NORMAL
2023-12-29T02:15:57.800397+00:00
2023-12-29T02:15:57.800414+00:00
56
false
# Complexity\n- Time complexity:$$O(n*m)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:$$O(n*m)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n// Recursive Solution\n private int solveRec(int[][] mat, int target, int idx, int val){\n ...
1
0
['Array', 'Dynamic Programming', 'Recursion', 'Memoization', 'Matrix', 'Java']
0
minimize-the-difference-between-target-and-chosen-elements
[Kotlin] DP | Fastest
kotlin-dp-fastest-by-user1572h-wt99
```\nclass Solution {\n private val minDiff = mutableMapOf()\n \n fun minimizeTheDifference(mat: Array, target: Int): Int {\n return getMinDiff(
user1572h
NORMAL
2022-09-10T13:53:39.458458+00:00
2022-09-10T13:54:07.331322+00:00
24
false
```\nclass Solution {\n private val minDiff = mutableMapOf<Int, Int>()\n \n fun minimizeTheDifference(mat: Array<IntArray>, target: Int): Int {\n return getMinDiff(mat, 0, target)\n }\n \n fun getMinDiff(mat: Array<IntArray>, firstRow: Int, target: Int): Int {\n var cached = minDiff[firs...
1
0
['Kotlin']
0
minimize-the-difference-between-target-and-chosen-elements
Cleanest || Intuitative Approach
cleanest-intuitative-approach-by-priyans-pu2u
\n int solve(int i, vector<vector<int>>& m, int target, int sum, vector<vector<int>> &dp)\n {\n if(i == m.size())\n return abs(sum-targe
priyanshuHere27
NORMAL
2022-09-02T04:05:35.953738+00:00
2022-09-02T04:05:35.953781+00:00
437
false
```\n int solve(int i, vector<vector<int>>& m, int target, int sum, vector<vector<int>> &dp)\n {\n if(i == m.size())\n return abs(sum-target);\n \n if(dp[i][sum]!=-1)\n return dp[i][sum];\n \n int ans = INT_MAX;\n for(int j=0;j<m[0].size();j++)\n ...
1
0
['Dynamic Programming', 'Memoization', 'C', 'C++']
1
minimize-the-difference-between-target-and-chosen-elements
JAVA|Recursive |dp
javarecursive-dp-by-deysouvik955-4491
\n\n\tint ans=Integer.MAX_VALUE;\n Integer[][] dp=null;\n public int minimizeTheDifference(int[][] mat, int target) {\n dp=new Integer[mat.length][
deysouvik955
NORMAL
2022-08-09T16:01:03.214436+00:00
2022-08-09T16:01:03.214474+00:00
512
false
```\n\n\tint ans=Integer.MAX_VALUE;\n Integer[][] dp=null;\n public int minimizeTheDifference(int[][] mat, int target) {\n dp=new Integer[mat.length][71*71];\n ans=Integer.MAX_VALUE;\n dfs(0,mat,target,0);\n return ans;\n }\n void dfs(int row,int mat[][],int t,int sum){\n ...
1
0
['Dynamic Programming', 'Memoization', 'Java']
1
minimize-the-difference-between-target-and-chosen-elements
85% TC and 84% SC easy python solution
85-tc-and-84-sc-easy-python-solution-by-jo2yj
\ndef minimizeTheDifference(self, mat: List[List[int]], target: int) -> int:\n\tm = len(mat)\n\tfor i in range(m):\n\t\tmat[i].sort()\n\tq = set([0])\n\tfor i i
nitanshritulon
NORMAL
2022-07-28T11:09:43.599710+00:00
2022-07-28T11:10:48.459809+00:00
431
false
```\ndef minimizeTheDifference(self, mat: List[List[int]], target: int) -> int:\n\tm = len(mat)\n\tfor i in range(m):\n\t\tmat[i].sort()\n\tq = set([0])\n\tfor i in range(m):\n\t\ttemp = set()\n\t\tfor num1 in q:\n\t\t\tfor num2 in mat[i]:\n\t\t\t\ttemp.add(num1+num2)\n\t\t\t\tif(num1+num2 > target):\n\t\t\t\t\tbreak\n...
1
0
['Binary Tree', 'Python', 'Python3']
0
minimize-the-difference-between-target-and-chosen-elements
JAVA : RECURSIVE(TLE)+MEMOIZATION(ACCEPTED),EASY
java-recursivetlememoizationacceptedeasy-wjsr
RECURSIVE(TLE)\n\nclass Solution {\n public int minimizeTheDifference(int[][] mat, int target) {\n int min=Integer.MAX_VALUE; \n int n=mat.length;
l_e_s_s_o_r_d_i_nary
NORMAL
2022-07-15T13:04:27.310549+00:00
2022-07-15T13:04:27.310577+00:00
343
false
**RECURSIVE(TLE)**\n```\nclass Solution {\n public int minimizeTheDifference(int[][] mat, int target) {\n int min=Integer.MAX_VALUE; \n int n=mat.length;\n int m=mat[0].length;\n \n for(int i=0;i<m;i++){\n min=Math.min(min,solve(mat,0,0,i,n,m,target));\n // System.out.println...
1
0
['Recursion', 'Memoization', 'Java']
0
minimize-the-difference-between-target-and-chosen-elements
Fully optimized DP Top Down Approach
fully-optimized-dp-top-down-approach-by-kzc5a
\nclass Solution {\npublic:\n \n int ans=INT_MAX;\n int helper(int i,int sum,int &n,vector<vector<int>>&dp,vector<vector<int>>&mat,int &target)\n {\n
dhruvjain04
NORMAL
2022-06-16T03:44:22.297888+00:00
2022-06-16T03:45:22.304583+00:00
281
false
```\nclass Solution {\npublic:\n \n int ans=INT_MAX;\n int helper(int i,int sum,int &n,vector<vector<int>>&dp,vector<vector<int>>&mat,int &target)\n {\n if(i==n)\n {\n ans=min(ans,abs(target-sum));\n return abs(target-sum);\n }\n if(dp[i][sum]!=-1)return dp[i...
1
0
['Dynamic Programming', 'C']
0
minimize-the-difference-between-target-and-chosen-elements
Scala
scala-by-fairgrieve-5puw
\nimport scala.util.chaining.scalaUtilChainingOps\n\nobject Solution {\n def minimizeTheDifference(mat: Array[Array[Int]], target: Int): Int = mat\n .foldLe
fairgrieve
NORMAL
2022-05-03T01:55:37.468779+00:00
2022-05-03T01:55:37.468821+00:00
49
false
```\nimport scala.util.chaining.scalaUtilChainingOps\n\nobject Solution {\n def minimizeTheDifference(mat: Array[Array[Int]], target: Int): Int = mat\n .foldLeft(Set(0), Option.empty[Int]) {\n case lessThanTarget -> noLessThanTarget -> row => lessThanTarget\n .iterator\n .concat(noLessThanTarget)...
1
0
['Scala']
0
minimize-the-difference-between-target-and-chosen-elements
JavaScript Solution - Top Down DP Approach
javascript-solution-top-down-dp-approach-rhei
\nvar minimizeTheDifference = function(mat, target) {\n const MAX = Number.MAX_SAFE_INTEGER;\n \n const m = mat.length;\n const n = mat[0].length;\n
Deadication
NORMAL
2022-04-25T15:57:03.684189+00:00
2022-04-25T15:57:03.684233+00:00
218
false
```\nvar minimizeTheDifference = function(mat, target) {\n const MAX = Number.MAX_SAFE_INTEGER;\n \n const m = mat.length;\n const n = mat[0].length;\n \n const memo = [];\n \n for (let i = 0; i < m; ++i) {\n memo[i] = new Array(4901).fill(MAX);\n }\n \n return topDown(0, 0);\n ...
1
0
['Dynamic Programming', 'JavaScript']
1
minimize-the-difference-between-target-and-chosen-elements
Java || DP || Easy to Understand
java-dp-easy-to-understand-by-itsmohitmk-mkka
```\nclass Solution {\n int[][] dp;\n public int minimizeTheDifference(int[][] mat, int target) {\n int n = mat.length , m = mat[0].length;\n
itsmohitmk99
NORMAL
2022-04-21T11:52:44.178537+00:00
2022-04-21T11:52:44.178568+00:00
376
false
```\nclass Solution {\n int[][] dp;\n public int minimizeTheDifference(int[][] mat, int target) {\n int n = mat.length , m = mat[0].length;\n \n dp = new int[n][5000];\n for(int i = 0 ; i< n ; i++){\n for(int j = 0 ; j < 5000 ; j++){\n dp[i][j] = Integer.MAX_V...
1
0
['Dynamic Programming', 'Java']
0
minimize-the-difference-between-target-and-chosen-elements
dp,memoization
dpmemoization-by-amreshhere-5j6d
class Solution {\npublic:\n int dp[71][5000];\n void funct(vector>& mat,int n,int m,int target,int &ans,int k,int sum)\n {\n if(dp[k][sum]!=-
amreshhere
NORMAL
2022-03-29T08:48:59.561260+00:00
2022-03-29T08:48:59.561284+00:00
173
false
class Solution {\npublic:\n int dp[71][5000];\n void funct(vector<vector<int>>& mat,int n,int m,int target,int &ans,int k,int sum)\n {\n if(dp[k][sum]!=-1)\n return;\n dp[k][sum]=1; \n if(k==n)\n {\n ans=min(ans,abs(target-sum));\n return;\n }\n ...
1
1
['Dynamic Programming', 'Recursion', 'Memoization']
0
minimize-the-difference-between-target-and-chosen-elements
Python (Faster than 93.96%) Little Explanation
python-faster-than-9396-little-explanati-96ck
\nclass Solution:\n def minimizeTheDifference(self, mat: List[List[int]], target: int) -> int:\n\t\t# initialise set of attainable numbers using the first ro
jinyang_deng
NORMAL
2022-03-27T01:26:36.597818+00:00
2022-03-27T01:26:54.819532+00:00
175
false
```\nclass Solution:\n def minimizeTheDifference(self, mat: List[List[int]], target: int) -> int:\n\t\t# initialise set of attainable numbers using the first row\n arr = set(mat[0])\n \n\t\t# from the second row onwards\n\t\t# we add every number in arr with every number in the row\n for row in ...
1
0
[]
0
minimize-the-difference-between-target-and-chosen-elements
2 Methods Easy understanding
2-methods-easy-understanding-by-retardin-11jb
\nclass Solution {\npublic:\n int m, n;\n int dp[72][5000];\n int solve(vector<vector<int>>& mat, int row, int sum, const int& target)\n {\n\t\t// B
retarding
NORMAL
2022-03-14T06:32:14.835377+00:00
2022-03-14T06:32:14.835428+00:00
173
false
```\nclass Solution {\npublic:\n int m, n;\n int dp[72][5000];\n int solve(vector<vector<int>>& mat, int row, int sum, const int& target)\n {\n\t\t// Base condition\n if(row >= m)\n {\n return abs(sum - target);\n }\n \n\t\t// Check if already calculated\n if(dp...
1
1
['Dynamic Programming', 'Memoization']
0
minimize-the-difference-between-target-and-chosen-elements
Simple Java Solution (Recursion + Memoization)
simple-java-solution-recursion-memoizati-csdr
```\nclass Solution {\n public int minimizeTheDifference(int[][] mat, int target) {\n int m=mat.length,n=mat[0].length;\n Integer[][] dp=new In
mokshteng
NORMAL
2022-01-07T12:26:46.317845+00:00
2022-01-07T12:26:46.317885+00:00
179
false
```\nclass Solution {\n public int minimizeTheDifference(int[][] mat, int target) {\n int m=mat.length,n=mat[0].length;\n Integer[][] dp=new Integer[m+1][10000];\n return rec(mat,0,m,n,target,0,dp);\n }\n int rec(int[][] mat,int idx,int m,int n,int target,int sum,Integer dp[][])\n {\n ...
1
1
[]
1
minimize-the-difference-between-target-and-chosen-elements
c++ dp
c-dp-by-dolaamon2-d7yx
\nclass Solution {\npublic:\n int minimizeTheDifference(vector<vector<int>>& mat, int target) {\n int n = mat.size();\n int m = mat[0].size();\
dolaamon2
NORMAL
2021-11-27T01:23:15.800996+00:00
2021-11-27T01:23:15.801021+00:00
225
false
```\nclass Solution {\npublic:\n int minimizeTheDifference(vector<vector<int>>& mat, int target) {\n int n = mat.size();\n int m = mat[0].size();\n bool v[801][2] = {0};\n v[0][0] = 1;\n int mt = 1000000;\n for(int i=n-1; i>=0; i--)\n {\n mt = mt + (*min_el...
1
0
[]
0
minimize-the-difference-between-target-and-chosen-elements
C++| Top-Down with pruning | DP + memoization
c-top-down-with-pruning-dp-memoization-b-jcyn
\n\n\nclass Solution {\npublic:\n int dp[5000][71]; \n int n, m, target; \n vector<vector<int>> mat;\n int res = INT_MAX; \n int memo(int row, in
aditya_trips
NORMAL
2021-10-20T13:35:43.260253+00:00
2021-10-20T13:36:13.301753+00:00
304
false
\n\n```\nclass Solution {\npublic:\n int dp[5000][71]; \n int n, m, target; \n vector<vector<int>> mat;\n int res = INT_MAX; \n int memo(int row, int currSum){\n if(row == n){\n res = min(res , abs(currSum-target)); \n return abs(currSum-target); \n }\n if(dp[cu...
1
0
['Dynamic Programming', 'Memoization', 'C']
0
minimize-the-difference-between-target-and-chosen-elements
Python DP Solution with prunning, beats ~80%
python-dp-solution-with-prunning-beats-8-63ek
At each row, dp[i] is a list of unique sums we can obtain considering rows up to and including i.\ndp[i] = set(d+el for el in row for d in dp[i-1])\n\nNotice th
niteloser
NORMAL
2021-09-03T12:03:52.871385+00:00
2021-09-03T12:03:52.871429+00:00
203
false
At each row, dp[i] is a list of unique sums we can obtain considering rows up to and including i.\n`dp[i] = set(d+el for el in row for d in dp[i-1])`\n\nNotice the following: for each element `el` of row, if we `d+el >= target` for some `d` in `dp[i-1]` , there is no need to consider values of `dp[i-1]` that are greate...
1
0
[]
0
minimize-the-difference-between-target-and-chosen-elements
Two solutions: iterative DP, and dfs with memo
two-solutions-iterative-dp-and-dfs-with-4399t
For iterative dp, at each row we update all possible sum of values we can get. \nFor example, if we at row r, we want to see what sum of values we have at row r
paullyu
NORMAL
2021-09-01T19:45:14.270824+00:00
2021-09-01T19:45:14.270856+00:00
293
false
For iterative dp, at each row we update all possible sum of values we can get. \nFor example, if we at row r, we want to see what sum of values we have at row r-1,\nand we can append mat numbers at row r to sum of values end at row r - 1 to form new\nsum of values\n\ndp[i] means if value i can be formed at current row\...
1
0
[]
1
minimize-the-difference-between-target-and-chosen-elements
My Java Solution using Memoization
my-java-solution-using-memoization-by-vr-gxk5
\nclass Solution {\n public int minimizeTheDifference(int[][] mat, int target) {\n if (mat == null || mat.length == 0 || target < 0)\n retu
vrohith
NORMAL
2021-08-26T18:47:53.049363+00:00
2021-08-26T18:47:53.049434+00:00
443
false
```\nclass Solution {\n public int minimizeTheDifference(int[][] mat, int target) {\n if (mat == null || mat.length == 0 || target < 0)\n return -1;\n int row = mat.length;\n int col = mat[0].length;\n // 70 * 70\n // [index][available sum]\n Integer [][] memo = n...
1
0
['Recursion', 'Memoization', 'Java']
1
minimize-the-difference-between-target-and-chosen-elements
(C++) 1981. Minimize the Difference Between Target and Chosen Elements
c-1981-minimize-the-difference-between-t-d6ea
\n\nclass Solution {\npublic:\n int minimizeTheDifference(vector<vector<int>>& mat, int target) {\n bitset<5000> bits(1); \n for (auto& row : m
qeetcode
NORMAL
2021-08-24T21:41:09.745617+00:00
2021-08-24T21:43:11.257473+00:00
260
false
\n```\nclass Solution {\npublic:\n int minimizeTheDifference(vector<vector<int>>& mat, int target) {\n bitset<5000> bits(1); \n for (auto& row : mat) {\n bitset<5000> temp; \n for (auto& x : row) temp |= (bits << x); \n bits = temp; \n }\n \n for (i...
1
0
['C']
0
minimize-the-difference-between-target-and-chosen-elements
C++ | Recursion + Memoization | Knapsack Variation
c-recursion-memoization-knapsack-variati-qyn4
\nclass Solution {\npublic:\n \n int dp[75][4900+805];\n int cnt=70*70;\n int solve(int i,vector<vector<int>>& mat,int t)\n {\n \n
abhi_code_1
NORMAL
2021-08-24T14:43:08.097195+00:00
2021-08-24T15:05:54.632837+00:00
335
false
```\nclass Solution {\npublic:\n \n int dp[75][4900+805];\n int cnt=70*70;\n int solve(int i,vector<vector<int>>& mat,int t)\n {\n \n if(i==mat.size())\n {\n return abs(t);\n }\n \n if(dp[i][cnt+t]!=-1)return dp[i][cnt+t];\n int mn=1e9;\n ...
1
1
['Recursion', 'Memoization']
1
minimize-the-difference-between-target-and-chosen-elements
C++ / DP / Memoization
c-dp-memoization-by-hitesh2494-ybmh
c++\nclass Solution {\npublic:\n /*\n Over here we are given that the size of the input array won\'t exceed 70. We want to get the sum using each posiible
hitesh2494
NORMAL
2021-08-23T07:53:14.810705+00:00
2021-08-23T07:53:14.810753+00:00
74
false
```c++\nclass Solution {\npublic:\n /*\n Over here we are given that the size of the input array won\'t exceed 70. We want to get the sum using each posiible combination. But we won\'t calculate the sum that is already compyed for a give row. \n */\n //this is how we initialize an array in a range with some...
1
0
[]
0
minimize-the-difference-between-target-and-chosen-elements
[C++] bitset with explanations, 76ms, faster than 100%,
c-bitset-with-explanations-76ms-faster-t-jg4s
Bitset could be a good choic to this question:\n1. the sums are in a relative small range (<5000);\n2. use the index of the bitset to represent the possible sum
dingdang2014
NORMAL
2021-08-22T22:53:11.716326+00:00
2021-08-22T23:36:09.935351+00:00
134
false
Bitset could be a good choic to this question:\n1. the sums are in a relative small range (<5000);\n2. use the index of the bitset to represent the possible sum;\n3. use the bit shift operation to greatly reduce the time complexity. Only one shift operation can be equivalent to add a number to all the sums stored in th...
1
0
['Bit Manipulation']
1
minimize-the-difference-between-target-and-chosen-elements
[Python] Optimized brute force in O(target * n * n) time and O(target) space, explained
python-optimized-brute-force-in-otarget-5jxr3
If you try to solve this with Brute-force, the run time would be O(n^n), and you would always get TLE.\n\nMy optimization is still to add the nums row by row, h
h241668288
NORMAL
2021-08-22T15:33:40.670717+00:00
2021-08-22T15:49:58.693043+00:00
156
false
If you try to solve this with Brute-force, the run time would be O(n^n), and you would always get TLE.\n\nMy optimization is still to add the nums row by row, however, use a set call "to_visit" to track the "valid" numbers that can be brought to the next row. The valid number will be **all nums smaller than the target*...
1
0
['Ordered Set', 'Python']
0
minimize-the-difference-between-target-and-chosen-elements
C++ DP 100% efficient
c-dp-100-efficient-by-verdict_ac-l0z4
\nclass Solution {\npublic:\n int minimizeTheDifference(vector<vector<int>>& mat, int target) \n {\n int m=mat.size(),n=mat[0].size();\n int
Verdict_AC
NORMAL
2021-08-22T09:26:44.587924+00:00
2021-08-22T09:26:44.587952+00:00
137
false
```\nclass Solution {\npublic:\n int minimizeTheDifference(vector<vector<int>>& mat, int target) \n {\n int m=mat.size(),n=mat[0].size();\n int dp[m][target+2];\n for(int i=0;i<m;i++)\n for(int j=0;j<=target;j++)\n dp[i][j]=-1;\n for(int i=0;i<m;i++)dp[i][targ...
1
0
['Dynamic Programming']
0
minimize-the-difference-between-target-and-chosen-elements
[C++] DP solution with explanation
c-dp-solution-with-explanation-by-manish-f9i9
Actually, we need to find a range of sum on which we can apply DP.\n So find the sum of smallest element(let say s) from each row. If this sum is greater than o
manishbishnoi897
NORMAL
2021-08-22T08:29:46.980969+00:00
2021-08-22T08:39:25.329824+00:00
189
false
* Actually, we need to find a range of sum on which we can apply DP.\n* So find the sum of smallest element(let say s) from each row. If this sum is greater than or equal to the target`(s>=target)`, then no other better solution exists and `abs(target-s)` will be our answer.\n\tFor eg. Let the sum we got from smaller e...
1
0
['Dynamic Programming', 'C']
0
minimize-the-difference-between-target-and-chosen-elements
[Java] Use HashSet at every row to find every possible value
java-use-hashset-at-every-row-to-find-ev-gb3x
\n\nclass Solution {\n public int minimizeTheDifference(int[][] mat, int target) {\n HashSet<Integer>set=new HashSet();\n int m=mat.length, n=m
bryantt23
NORMAL
2021-08-22T07:15:20.659813+00:00
2021-08-22T07:15:20.659845+00:00
69
false
```\n\nclass Solution {\n public int minimizeTheDifference(int[][] mat, int target) {\n HashSet<Integer>set=new HashSet();\n int m=mat.length, n=mat[0].length;\n \n // get every value in the row\n for(int j=0; j<n; j++){\n set.add(mat[0][j]);\n }\n \n ...
1
0
[]
1
minimize-the-difference-between-target-and-chosen-elements
C++ bitset 100%
c-bitset-100-by-colinyoyo26-fb5m
\nclass Solution {\npublic:\n int minimizeTheDifference(vector<vector<int>>& mat, int target) {\n bitset<5000> b, b1; \n b.set(0);\n for
colinyoyo26
NORMAL
2021-08-22T07:04:16.530024+00:00
2021-08-22T07:04:16.530065+00:00
94
false
```\nclass Solution {\npublic:\n int minimizeTheDifference(vector<vector<int>>& mat, int target) {\n bitset<5000> b, b1; \n b.set(0);\n for (auto &v : mat) {\n for (auto a : v)\n b1 |= b << a;\n swap(b, b1);\n b1.reset();\n }\n int an...
1
0
[]
0
minimize-the-difference-between-target-and-chosen-elements
[Java] DP solution with greedy.
java-dp-solution-with-greedy-by-lincansh-jmp4
\nclass Solution {\n public int minimizeTheDifference(int[][] mat, int target) {\n int min_sum = 0;\n for (int[] row : mat) {\n int
lincanshu
NORMAL
2021-08-22T04:26:00.683425+00:00
2021-08-22T04:26:08.115026+00:00
182
false
```\nclass Solution {\n public int minimizeTheDifference(int[][] mat, int target) {\n int min_sum = 0;\n for (int[] row : mat) {\n int min = Integer.MAX_VALUE;\n for (int num : row) {\n min = Math.min(num, min);\n }\n min_sum += min;\n }...
1
0
['Java']
0
minimize-the-difference-between-target-and-chosen-elements
Answer Giving TLE please tell the error like I am unable to figure it out
answer-giving-tle-please-tell-the-error-4gby2
typedef long long int ll;\nclass Solution {\npublic:\n ll minimizeTheDifference(vector>& mat, ll target) \n\t{\n\t\tll n=mat.size(),m=mat[0].size();\n
patanahikaunheye
NORMAL
2021-08-22T04:22:48.634067+00:00
2021-08-22T04:22:48.634096+00:00
149
false
typedef long long int ll;\nclass Solution {\npublic:\n ll minimizeTheDifference(vector<vector<int>>& mat, ll target) \n\t{\n\t\tll n=mat.size(),m=mat[0].size();\n if(n==0||m==0)\n {\n return target;\n }\n vector<vector<ll>>dp(n+5,vector<ll>(5000,0));\n for(ll i=0;i<=n;i+...
1
0
[]
0
minimize-the-difference-between-target-and-chosen-elements
[JavaScript] Dynamic Programming
javascript-dynamic-programming-by-steven-2ns0
javascript\n/**\n * Collect all the possible sums but keep only one sum that is greater than the target\n */\nvar minimizeTheDifference = function(mat, target)
stevenkinouye
NORMAL
2021-08-22T04:02:31.773852+00:00
2021-08-30T00:22:19.064116+00:00
348
false
```javascript\n/**\n * Collect all the possible sums but keep only one sum that is greater than the target\n */\nvar minimizeTheDifference = function(mat, target) {\n let possibleSums = new Set(mat[0]);\n for (let row = 1; row < mat.length; row++) {\n const nextPossibleSums = new Set();\n let min = ...
1
0
['JavaScript']
0
minimize-the-difference-between-target-and-chosen-elements
c++ bitset
c-bitset-by-harshitata404-p651
IntuitionWe just need to maintain the total unique sums obtained in prev rowApproachSince constraints are small, we are able to use a bitset hereCode
harshitata404
NORMAL
2025-04-07T17:21:26.637665+00:00
2025-04-07T17:21:26.637665+00:00
2
false
# Intuition We just need to maintain the total unique sums obtained in prev row # Approach Since constraints are small, we are able to use a bitset here # Code ```cpp [] class Solution { public: int minimizeTheDifference(vector<vector<int>>& mat, int target) { bitset<10001>prev(1); for(int i=0;i...
0
0
['C++']
0
minimize-the-difference-between-target-and-chosen-elements
Python | C++ DP appropriate knapsack
python-c-dp-appropriate-knapsack-by-kalu-ujqz
Complexity Time complexity: O(NM|K|) Space complexity: |K| Code
kaluginpeter
NORMAL
2025-04-07T09:01:53.268901+00:00
2025-04-07T09:01:53.268901+00:00
12
false
# Complexity - Time complexity: O(NM|K|) - Space complexity: |K| # Code ```python3 [] class Solution: def minimizeTheDifference(self, mat: List[List[int]], target: int) -> int: sums: set[int] = {0} for row in mat: sums = {prev_sum + num for num in row for prev_sum in sums} re...
0
0
['Array', 'Dynamic Programming', 'Matrix', 'C++', 'Python3']
0
minimize-the-difference-between-target-and-chosen-elements
This shouldn't have worked but it does...
this-shouldnt-have-worked-but-it-does-by-gh5v
Code
Erohblak
NORMAL
2025-04-07T07:19:04.085757+00:00
2025-04-07T07:29:15.794927+00:00
7
false
# Code ```cpp [] class Solution { public: int minimizeTheDifference(vector<vector<int>>& mat, int target) { int n = mat.size(); int m = mat[0].size(); int ans = INT_MAX; int minSum = 0, maxSum = 0; for (int i = 0; i < mat.size(); i++) { minSum += *min_element(ma...
0
0
['C++']
0
minimize-the-difference-between-target-and-chosen-elements
Moving Sum Set
moving-sum-set-by-lilongxue-up95
IntuitionUse a moving sum set.Approach The sum set initially is the first row. Go down row by row, and for each row, we update the sum set. In the final sum set
lilongxue
NORMAL
2025-04-02T03:29:16.669743+00:00
2025-04-02T03:29:16.669743+00:00
1
false
# Intuition Use a moving sum set. # Approach 1. The sum set initially is the first row. 2. Go down row by row, and for each row, we update the sum set. 3. In the final sum set, find the closet sum along with its difference. # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space c...
0
0
['JavaScript']
0
minimize-the-difference-between-target-and-chosen-elements
Simple bitmask solution
simple-bitmask-solution-by-lilysunstride-mte1
IntuitionBitmask is good at storing all possible values within a small rangeApproach loop through the matrix to record all possible values find the distance bet
lilysunstrider
NORMAL
2025-03-10T17:57:02.566548+00:00
2025-03-10T17:57:02.566548+00:00
4
false
# Intuition Bitmask is good at storing all possible values within a small range # Approach - loop through the matrix to record all possible values - find the distance between target and possible values <!-- Describe your approach to solving the problem. --> # Code ```typescript [] function minimizeTheDifference(mat: ...
0
0
['TypeScript']
0
minimize-the-difference-between-target-and-chosen-elements
Most Optimal Time (O(N*M*M) and Space Complexity (2*O(M*M))
most-optimal-time-onmm-and-space-complex-6rsr
ApproachAssume that N = num of rows, and M = num of colsWe start with inserting our base case into our current Set, since if we assume the matrix is of zero row
varunadit2
NORMAL
2025-03-08T19:48:30.200325+00:00
2025-03-08T19:48:30.200325+00:00
3
false
# Approach <!-- Describe your approach to solving the problem. --> Assume that N = num of rows, and M = num of cols We start with inserting our base case into our current Set, since if we assume the matrix is of zero rows and zero columns, we would be able to achieve only one sum that is Zero. Building upon our base ...
0
0
['Dynamic Programming', 'Swift']
0
minimize-the-difference-between-target-and-chosen-elements
C++ Solution by dp and bitset with clear explanation
c-solution-by-dp-and-bitset-with-clear-e-ns26
Complexity Time Complexity: O(n∗m) Space Complexity: O(1) IdeaWe use the bitset seen to record the sum that we have seen, seen[i]=1 if and only if we have seen
tan5166
NORMAL
2025-01-27T04:03:57.850935+00:00
2025-01-27T04:05:30.313443+00:00
11
false
## Complexity - Time Complexity: $O(n * m)$ - Space Complexity: $O(1)$ ## Idea We use the bitset `seen` to record the sum that we have seen, $seen[i] = 1$ if and only if we have seen the sum $i$. By this idea, we first set $seen[0]= 1$. When there is a new number, say $num$, and we want to find all of its possibl...
0
0
['C++']
0
minimize-the-difference-between-target-and-chosen-elements
DFS + Optimisation / Pruning
dfs-optimisation-pruning-by-matt_scott-srls
IntuitionGiven the constraints of this problem, it's not unrealistic to expect that we can use DP / recursion to (intelligently) brute-force the solution. Recur
matt_scott
NORMAL
2025-01-24T15:40:36.091646+00:00
2025-01-24T15:40:36.091646+00:00
11
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> Given the constraints of this problem, it's not unrealistic to expect that we can use DP / recursion to (intelligently) brute-force the solution. Recursively, we can take a number in each row, and ask for the reduced problem of `target - ma...
0
0
['Python3']
0
minimize-the-difference-between-target-and-chosen-elements
Backtrack + DP with time/space complexity explanation
backtrack-dp-with-timespace-complexity-e-819p
IntuitionBacktracking along with Dynamic Programming+Memorization.ApproachDo DFS. For each element of the row, explore all the elements of next row. Accumulate
sam506
NORMAL
2025-01-18T06:58:27.702107+00:00
2025-01-18T06:58:27.702107+00:00
14
false
# Intuition Backtracking along with Dynamic Programming+Memorization. # Approach Do DFS. For each element of the row, explore all the elements of next row. Accumulate the sum and capture the min results if the current row is the last row. Optimization step: If the sum has gone bigger than the target, and the diff is...
0
0
['C++']
0
minimize-the-difference-between-target-and-chosen-elements
Java DP solution
java-dp-solution-by-priyanshiigoyal-6kso
IntuitionApproachThe problem depends on two factors the sum and the row number so using this dpComplexity Time complexity: Space complexity: O(m*S) whre S is
priyanshiigoyal
NORMAL
2025-01-09T15:00:36.519290+00:00
2025-01-09T15:00:36.519290+00:00
12
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach The problem depends on two factors the sum and the row number so using this dp # Complexity - Time complexity: - Space complexity: O(m*S) whre S is sum <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```java [...
0
0
['Dynamic Programming', 'Java']
0
minimize-the-difference-between-target-and-chosen-elements
Simple DP + Memoization solution || c++ || beats 90%
simple-dp-memoization-solution-c-beats-9-7f4q
IntuitionApproachComplexity Time complexity: Space complexity: Code
vikash_kumar_dsa2
NORMAL
2025-01-05T17:09:23.278846+00:00
2025-01-05T17:09:23.278846+00:00
10
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code `...
0
0
['Dynamic Programming', 'Memoization', 'C++']
0
minimize-the-difference-between-target-and-chosen-elements
Python int as bitset in two lines ~15ms
python-int-as-bitset-by-edward-ji-pt0w
ApproachUse a single integer seen as a hash set for the sum we've seen for far. The i-th least significant bit represents whether the sum i is possible. Initial
edward-ji
NORMAL
2025-01-03T16:12:33.871606+00:00
2025-01-04T02:52:33.681044+00:00
13
false
# Approach Use a single integer `seen` as a hash set for the sum we've seen for far. The `i`-th least significant bit represents whether the sum `i` is possible. Initially, `seen = 1` represents that the sum `0` is possible by not taking any number. For each `num` in each row, we compute the new possible sums by left...
0
0
['Dynamic Programming', 'Bit Manipulation', 'Python3']
0
minimize-the-difference-between-target-and-chosen-elements
Bruteforce
bruteforce-by-tailtit-x80k
Code
tailtit
NORMAL
2024-12-30T19:22:17.684910+00:00
2024-12-30T19:22:17.684910+00:00
6
false
# Code ```python3 [] class Solution: def minimizeTheDifference(self, mat: List[List[int]], target: int) -> int: p_min = sum(min(i) for i in mat) if p_min > target: return p_min - target d = {0} for row in mat: d = {x + i for x in row for i in d if x + i <= (2...
0
0
['Array', 'Python3']
0
minimize-the-difference-between-target-and-chosen-elements
minimize the difference
minimize-the-difference-by-balaganeshkan-t81u
IntuitionApproachComplexity Time complexity: Space complexity: Code
BALAGANESHKANIKICHARLA
NORMAL
2024-12-21T16:12:28.194994+00:00
2024-12-21T16:12:28.194994+00:00
5
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code `...
0
0
['C++']
0
minimize-the-difference-between-target-and-chosen-elements
Minimize the Difference Between Target and Choosen Element
minimize-the-difference-between-target-a-y7s5
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
Naeem_ABD
NORMAL
2024-12-08T10:43:08.795379+00:00
2024-12-08T10:43:08.795419+00:00
6
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
0
0
['Go']
0
minimize-the-difference-between-target-and-chosen-elements
Python, knapsack dp solution with explanation
python-knapsack-dp-solution-with-explana-quwp
We must select a number from each row to make sum of these number be as close as possible to target, it is similar to knapsack problem.\n\ndp[i][j] is whether w
shun6096tw
NORMAL
2024-11-28T05:47:45.539721+00:00
2024-11-28T05:47:45.539758+00:00
4
false
We must select a number from each row to make sum of these number be as close as possible to ```target```, it is similar to knapsack problem.\n\ndp[i][j] is whether we make ```j``` from row 0 to ```i```.\nmax of ```j``` is target, and when the sum is over target, keep track of it.\n\ndp[i][j] = any(dp[i-1][j - x] for x...
0
0
['Dynamic Programming', 'Python']
0
minimize-the-difference-between-target-and-chosen-elements
scala recursion, memoization, pruning
scala-recursion-memoization-pruning-by-v-aeus
scala []\nobject Solution {\n import scala.util.chaining._\n val mxVal = 70\n val cache = collection.mutable.Map.empty[(Int,Int),Int]\n def minimizeTheDiffe
vititov
NORMAL
2024-11-19T17:42:54.764716+00:00
2024-11-19T17:42:54.764732+00:00
0
false
```scala []\nobject Solution {\n import scala.util.chaining._\n val mxVal = 70\n val cache = collection.mutable.Map.empty[(Int,Int),Int]\n def minimizeTheDifference(_mat: Array[Array[Int]], target: Int): Int = {\n val mat = _mat.map(_.distinct.sorted)\n cache.clear()\n var best = Int.MaxValue\n def f(i:...
0
0
['Array', 'Dynamic Programming', 'Matrix', 'Scala']
0
count-valid-paths-in-a-tree
DFS/Tree-DP in C++, Java & Python
dfstree-dp-in-c-java-python-by-cpcs-rdst
Intuition\nLinear sieve to get all the prime numbers that are no larger than n.\nThen do a tree DP (or simply consider it as DFS) to calculate the result.\n\n#
cpcs
NORMAL
2023-09-24T04:01:10.554714+00:00
2023-09-25T03:01:44.541049+00:00
5,225
false
# Intuition\nLinear sieve to get all the prime numbers that are no larger than n.\nThen do a tree DP (or simply consider it as DFS) to calculate the result.\n\n# Approach\nUse linear sieve to get all prime numbers, other sieve algorithms that have higher time complexity are also acceptable.\n\nIn DFS/Tree DP, return 2 ...
41
3
['Dynamic Programming', 'C++', 'Java', 'Python3']
10
count-valid-paths-in-a-tree
[Python3] DSU
python3-dsu-by-awice-r3ss
Union all the composite nodes, now we just have to count from a bipartite tree.\n\nThis is easy, as all the path lengths are at most 3.\n\n# Code\n\nMX = 100001
awice
NORMAL
2023-09-24T04:02:05.304359+00:00
2023-09-24T04:02:05.304387+00:00
1,511
false
Union all the composite nodes, now we just have to count from a bipartite tree.\n\nThis is easy, as all the path lengths are at most 3.\n\n# Code\n```\nMX = 100001\nlpf = [0] * MX\nfor i in range(2, MX):\n if lpf[i] == 0:\n for j in range(i, MX, i):\n lpf[j] = i\n\nclass DSU:\n def __init__(self...
30
0
['Python3']
6