From 71952115f621d5b16417aee516ed79ab35e12191 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 24 Sep 2023 18:26:23 +0530 Subject: [PATCH 0001/1399] Create README - LeetHub --- Find duplicates in an array - GFG/README.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 Find duplicates in an array - GFG/README.md diff --git a/Find duplicates in an array - GFG/README.md b/Find duplicates in an array - GFG/README.md new file mode 100644 index 00000000..81f21dc6 --- /dev/null +++ b/Find duplicates in an array - GFG/README.md @@ -0,0 +1,19 @@ +# Find duplicates in an array +## Easy +

Given an array a of size N which contains elements from 0 to N-1, you need to find all the elements occurring more than once in the given array. Return the answer in ascending order. If no such element is found, return list containing [-1]

+

Note: The extra space is only for the array to be returned. Try and perform all operations within the provided array. 

+

Example 1:

+
Input:
+N = 4
+a[] = {0,3,1,2}
+Output: 
-1 +Explanation:
There is no repeating element in the array. Therefore output is -1.
+

Example 2:

+
Input:
+N = 5
+a[] = {2,3,1,2,3}
+Output: 
2 3  +Explanation:
2 and 3 occur more than once in the given array.
+

Your Task:
Complete the function duplicates() which takes array a[] and n as input as parameters and returns a list of elements that occur more than once in the given array in a sorted manner. 

+

Expected Time Complexity: O(n).
Expected Auxiliary Space: O(n).

+

Constraints:
1 <= N <= 105
0 <= A[i] <= N-1, for each valid i

\ No newline at end of file From 864fcf09f23547120fe3c258fcc0a74fcd8ee521 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 24 Sep 2023 18:26:24 +0530 Subject: [PATCH 0002/1399] Added solution - LeetHub --- .../find-duplicates-in-an-array.java | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 Find duplicates in an array - GFG/find-duplicates-in-an-array.java diff --git a/Find duplicates in an array - GFG/find-duplicates-in-an-array.java b/Find duplicates in an array - GFG/find-duplicates-in-an-array.java new file mode 100644 index 00000000..e60293f5 --- /dev/null +++ b/Find duplicates in an array - GFG/find-duplicates-in-an-array.java @@ -0,0 +1,54 @@ +//{ Driver Code Starts +import java.io.*; +import java.util.*; +import java.util.Map.Entry; + +class GFG { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int t = sc.nextInt(); + while (t-- > 0) { + int n = sc.nextInt(); + int[] a = new int[n]; + for (int i = 0; i < n; i++) a[i] = sc.nextInt(); + Solution g = new Solution(); + ArrayList ans = g.duplicates(a, n); + for (Integer val : ans) System.out.print(val + " "); + System.out.println(); + } + } +} + +// } Driver Code Ends + + +class Solution { + public static ArrayList duplicates(int arr[], int n) { + // code here + + Map mp = new TreeMap(); + + for(int i = 0; i < n; ++i) + { + if(mp.containsKey(arr[i])) + { + mp.put(arr[i], mp.get(arr[i]) + 1); + } + else + mp.put(arr[i], 1); + } + + ArrayList ans = new ArrayList(); + + for(Map.Entry ele : mp.entrySet()) + { + if(ele.getValue() > 1) + ans.add(ele.getKey()); + } + + if(ans.isEmpty()) + ans.add(-1); + + return ans; + } +} From 758e030c8f530be00cd1461fa58f80c4ae36059b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 24 Sep 2023 23:06:58 +0530 Subject: [PATCH 0003/1399] Create README - LeetHub --- .../README.md | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divuber-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div799-champagne-tower/README.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divuber-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div799-champagne-tower/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divuber-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div799-champagne-tower/README.md new file mode 100644 index 00000000..11954b9d --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divuber-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div799-champagne-tower/README.md @@ -0,0 +1,38 @@ +

Uber
2
Amazon
2
Adobe
2
799. Champagne Tower

Medium


We stack glasses in a pyramid, where the first row has 1 glass, the second row has 2 glasses, and so on until the 100th row.  Each glass holds one cup of champagne.

+ +

Then, some champagne is poured into the first glass at the top.  When the topmost glass is full, any excess liquid poured will fall equally to the glass immediately to the left and right of it.  When those glasses become full, any excess champagne will fall equally to the left and right of those glasses, and so on.  (A glass at the bottom row has its excess champagne fall on the floor.)

+ +

For example, after one cup of champagne is poured, the top most glass is full.  After two cups of champagne are poured, the two glasses on the second row are half full.  After three cups of champagne are poured, those two cups become full - there are 3 full glasses total now.  After four cups of champagne are poured, the third row has the middle glass half full, and the two outside glasses are a quarter full, as pictured below.

+ +

+ +

Now after pouring some non-negative integer cups of champagne, return how full the jth glass in the ith row is (both i and j are 0-indexed.)

+ +

 

+

Example 1:

+ +
Input: poured = 1, query_row = 1, query_glass = 1
+Output: 0.00000
+Explanation: We poured 1 cup of champange to the top glass of the tower (which is indexed as (0, 0)). There will be no excess liquid so all the glasses under the top glass will remain empty.
+
+ +

Example 2:

+ +
Input: poured = 2, query_row = 1, query_glass = 1
+Output: 0.50000
+Explanation: We poured 2 cups of champange to the top glass of the tower (which is indexed as (0, 0)). There is one cup of excess liquid. The glass indexed as (1, 0) and the glass indexed as (1, 1) will share the excess liquid equally, and each will get half cup of champange.
+
+ +

Example 3:

+ +
Input: poured = 100000009, query_row = 33, query_glass = 17
+Output: 1.00000
+
+ +

 

+

Constraints:

+ +
    +
  • 0 <= poured <= 109
  • +
  • 0 <= query_glass <= query_row < 100
  • +
\ No newline at end of file From 22e3416af8826d92f429b422f3d9407b57d4717a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 24 Sep 2023 23:07:01 +0530 Subject: [PATCH 0004/1399] Time: 5 ms (70.70%), Space: 6.4 MB (76.54%) - LeetHub --- ...svg-div-div-div-div799-champagne-tower.cpp | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divuber-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div799-champagne-tower/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divuber-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div799-champagne-tower.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divuber-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div799-champagne-tower/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divuber-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div799-champagne-tower.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divuber-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div799-champagne-tower/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divuber-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div799-champagne-tower.cpp new file mode 100644 index 00000000..f2a40390 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divuber-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div799-champagne-tower/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divuber-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div799-champagne-tower.cpp @@ -0,0 +1,23 @@ +class Solution { +public: +double champagneTower(int poured, int query_row, int query_glass) { + + double result[101][101] = {0.0}; + + result[0][0] = poured; + + for (int i = 0; i < 100; i++) + { + for (int j = 0; j <= i; j++) + { + if (result[i][j] >= 1) + { + result[i + 1][j] += (result[i][j] - 1) / 2.0; + result[i + 1][j + 1] += (result[i][j] - 1) / 2.0; + result[i][j] = 1; + } + } + } + return result[query_row][query_glass]; + } +}; \ No newline at end of file From efd7708b9d21f3196d0d93f70d9fd5eceb5f361a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 25 Sep 2023 08:08:27 +0530 Subject: [PATCH 0005/1399] Create README - LeetHub --- .../README.md | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div389-find-the-difference/README.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div389-find-the-difference/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div389-find-the-difference/README.md new file mode 100644 index 00000000..34fec164 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div389-find-the-difference/README.md @@ -0,0 +1,29 @@ +

Google
6
Amazon
3
Apple
2
389. Find the Difference

Easy


You are given two strings s and t.

+ +

String t is generated by random shuffling string s and then add one more letter at a random position.

+ +

Return the letter that was added to t.

+ +

 

+

Example 1:

+ +
Input: s = "abcd", t = "abcde"
+Output: "e"
+Explanation: 'e' is the letter that was added.
+
+ +

Example 2:

+ +
Input: s = "", t = "y"
+Output: "y"
+
+ +

 

+

Constraints:

+ +
    +
  • 0 <= s.length <= 1000
  • +
  • t.length == s.length + 1
  • +
  • s and t consist of lowercase English letters.
  • +
+
\ No newline at end of file From fbbb555a3b8022329a8fa03f0c8470bb53973470 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 25 Sep 2023 08:08:31 +0530 Subject: [PATCH 0006/1399] Time: 6 ms (19.69%), Space: 7.1 MB (8.68%) - LeetHub --- ...div-div-div-div389-find-the-difference.cpp | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div389-find-the-difference/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div389-find-the-difference.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div389-find-the-difference/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div389-find-the-difference.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div389-find-the-difference/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div389-find-the-difference.cpp new file mode 100644 index 00000000..02b72711 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div389-find-the-difference/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div389-find-the-difference.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + char findTheDifference(string s, string t) { + + sort(t.begin(), t.end()); + sort(s.begin(), s.end()); + + int k = 0; + + for(int i = 0; i < s.size(); ++i) + { + if(s[i] == t[k]) + ++k; + else + return t[k]; + } + + return t.back(); + } +}; \ No newline at end of file From 8b18c64b5e33a165c40c470ca388c2f5107980e9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 25 Sep 2023 08:12:56 +0530 Subject: [PATCH 0007/1399] Create README - LeetHub From bd067b27a79386c71ca3da47b454ad663557eba7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 25 Sep 2023 08:12:57 +0530 Subject: [PATCH 0008/1399] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div389-find-the-difference/NOTES.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div389-find-the-difference/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div389-find-the-difference/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div389-find-the-difference/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 0eae5eac3e1b24c342efcac1acb2755a35172e69 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 25 Sep 2023 08:13:31 +0530 Subject: [PATCH 0009/1399] Create README - LeetHub --- 0389-find-the-difference/README.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 0389-find-the-difference/README.md diff --git a/0389-find-the-difference/README.md b/0389-find-the-difference/README.md new file mode 100644 index 00000000..0663dd96 --- /dev/null +++ b/0389-find-the-difference/README.md @@ -0,0 +1,29 @@ +

389. Find the Difference

Easy


You are given two strings s and t.

+ +

String t is generated by random shuffling string s and then add one more letter at a random position.

+ +

Return the letter that was added to t.

+ +

 

+

Example 1:

+ +
Input: s = "abcd", t = "abcde"
+Output: "e"
+Explanation: 'e' is the letter that was added.
+
+ +

Example 2:

+ +
Input: s = "", t = "y"
+Output: "y"
+
+ +

 

+

Constraints:

+ +
    +
  • 0 <= s.length <= 1000
  • +
  • t.length == s.length + 1
  • +
  • s and t consist of lowercase English letters.
  • +
+
\ No newline at end of file From 8519ee115d6071ef0d53687d6008d396d50ba926 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 25 Sep 2023 08:13:31 +0530 Subject: [PATCH 0010/1399] Attach NOTES - LeetHub --- 0389-find-the-difference/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0389-find-the-difference/NOTES.md diff --git a/0389-find-the-difference/NOTES.md b/0389-find-the-difference/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0389-find-the-difference/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From cd4e7dcd0d4a42671f829b5a1c5d754344f566f5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 25 Sep 2023 08:13:35 +0530 Subject: [PATCH 0011/1399] Time: 3 ms (45.31%), Space: 41.1 MB (20.11%) - LeetHub --- .../0389-find-the-difference.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 0389-find-the-difference/0389-find-the-difference.java diff --git a/0389-find-the-difference/0389-find-the-difference.java b/0389-find-the-difference/0389-find-the-difference.java new file mode 100644 index 00000000..ba8c7d44 --- /dev/null +++ b/0389-find-the-difference/0389-find-the-difference.java @@ -0,0 +1,19 @@ +class Solution { + public char findTheDifference(String s, String t) { + + char[] sChar = s.toCharArray(); + char[] tChar = t.toCharArray(); + + Arrays.sort(sChar); + Arrays.sort(tChar); + + for(int i = 0; i < sChar.length; ++i) + { + if(sChar[i] != tChar[i]) + return tChar[i]; + } + + return tChar[tChar.length-1]; + + } +} \ No newline at end of file From 449986e1ddf73d22195533c03e914a8a8b61647f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 25 Sep 2023 08:15:01 +0530 Subject: [PATCH 0012/1399] Attach NOTES - LeetHub From 3614e7209b2a452f8e899128c12183774cdff766 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 25 Sep 2023 08:15:04 +0530 Subject: [PATCH 0013/1399] Time: 3 ms (45.31%), Space: 41.1 MB (20.11%) - LeetHub From 5d8f314d5d7879d3df7a8df28bf3fed07acf0a17 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 25 Sep 2023 08:16:07 +0530 Subject: [PATCH 0014/1399] Attach NOTES - LeetHub From c4ce72bc3321a609d7265df172214a2ea4bd3311 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 25 Sep 2023 08:16:13 +0530 Subject: [PATCH 0015/1399] Attach NOTES - LeetHub From d8ee75617d9eaf6f9af4ff50087ce70476e1f8a0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 25 Sep 2023 08:16:17 +0530 Subject: [PATCH 0016/1399] Time: 3 ms (45.31%), Space: 41.1 MB (20.11%) - LeetHub From baa076d7433c971a9148fd82d09f3a6199164a35 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 25 Sep 2023 08:16:50 +0530 Subject: [PATCH 0017/1399] Attach NOTES - LeetHub From c2b00bea7ee3f11ed4aaa96efadb864cb2410d26 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 25 Sep 2023 08:16:54 +0530 Subject: [PATCH 0018/1399] Time: 1 ms (100.00%), Space: 40.9 MB (42.11%) - LeetHub --- .../0389-find-the-difference.java | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/0389-find-the-difference/0389-find-the-difference.java b/0389-find-the-difference/0389-find-the-difference.java index ba8c7d44..e0564259 100644 --- a/0389-find-the-difference/0389-find-the-difference.java +++ b/0389-find-the-difference/0389-find-the-difference.java @@ -1,19 +1,14 @@ class Solution { public char findTheDifference(String s, String t) { - char[] sChar = s.toCharArray(); - char[] tChar = t.toCharArray(); + char ch = 0; - Arrays.sort(sChar); - Arrays.sort(tChar); + for(char c : s.toCharArray()) + ch ^= c; - for(int i = 0; i < sChar.length; ++i) - { - if(sChar[i] != tChar[i]) - return tChar[i]; - } - - return tChar[tChar.length-1]; + for(char c : t.toCharArray()) + ch ^= c; + return ch; } } \ No newline at end of file From a3a943328e5ca9a8d5372150506dd4c18489c6df Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 25 Sep 2023 08:17:14 +0530 Subject: [PATCH 0019/1399] Attach NOTES - LeetHub From 4cfa9db1553ead8a1f2e3ddbc1b600626c8d6e77 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 25 Sep 2023 08:17:17 +0530 Subject: [PATCH 0020/1399] Time: 1 ms (100.00%), Space: 40.9 MB (42.11%) - LeetHub From 8cd595373c7b53aa1bc5f2dfcbed119de29997e9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 25 Sep 2023 08:17:25 +0530 Subject: [PATCH 0021/1399] Attach NOTES - LeetHub From 94d13215035d564ac3b56386d0ad5982f3cf0b61 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 25 Sep 2023 22:53:41 +0530 Subject: [PATCH 0022/1399] Create README - LeetHub --- Maximum Sum Combination - GFG/README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 Maximum Sum Combination - GFG/README.md diff --git a/Maximum Sum Combination - GFG/README.md b/Maximum Sum Combination - GFG/README.md new file mode 100644 index 00000000..05474e85 --- /dev/null +++ b/Maximum Sum Combination - GFG/README.md @@ -0,0 +1,11 @@ +# Maximum Sum Combination +## Medium +

Thank you for consistently taking part in the POTD.
Feel free to share your feedback and suggestions by filling up the Form below.
https://forms.gle/Ga6fVJBzEtDSvEop9
____________________________________________________________________________________________________________________________________________

Given two integer array A and B of size N each.
A sum combination is made by adding one element from array A and another element of array B.
Return the maximum K valid sum combinations from all the possible sum combinations.

+

Note : Output array must be sorted in non-increasing order.

+

Example 1:

+
Input:
N = 2
K = 2
A [ ] = {3, 2}
B [ ] = {1, 4}
Output: {7, 6}
Explanation: 
7 -> (A : 3) + (B : 4)
6 -> (A : 2) + (B : 4)
+

Example 2:

+
Input:
N = 4
K = 3
A [ ] = {1, 4, 2, 3}
B [ ] = {2, 5, 1, 6}
Output: {10, 9, 9}
Explanation: 
10 -> (A : 4) + (B : 6)
9 -> (A : 4) + (B : 5)
9 -> (A : 3) + (B : 6)
+

Your Task:
You don't need to read input or print anything. Your task is to complete the function maxCombinations() which takes the interger N,integer K and two integer arrays A [ ] and B [ ] as parameters and returns the maximum K valid distinct sum combinations .

+

Expected Time Complexity: O(Nlog(N))
Expected Auxiliary Space: O(N)

+

Constraints:
1 ≤ N ≤  105
1 ≤ K ≤  N
1 ≤ A [ i ] , B [ i ] ≤ 104

\ No newline at end of file From 0bc7561231d62ce0fa6a1cfa0f69b7624dc20517 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 25 Sep 2023 22:53:42 +0530 Subject: [PATCH 0023/1399] Added solution - LeetHub --- .../maximum-sum-combination.cpp | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 Maximum Sum Combination - GFG/maximum-sum-combination.cpp diff --git a/Maximum Sum Combination - GFG/maximum-sum-combination.cpp b/Maximum Sum Combination - GFG/maximum-sum-combination.cpp new file mode 100644 index 00000000..a13755ac --- /dev/null +++ b/Maximum Sum Combination - GFG/maximum-sum-combination.cpp @@ -0,0 +1,77 @@ +//{ Driver Code Starts +#include +using namespace std; + + +// } Driver Code Ends +class Solution { + public: + vector maxCombinations(int N, int K, vector &A, vector &B) { + // code here + + priority_queue, greater > pq; + + sort(A.rbegin(), A.rend()); + sort(B.rbegin(), B.rend()); + + for(int i = 0; i < K; ++i) + { + for(int j = 0; j < K; ++j) + { + int sum = A[i] + B[j]; + + if(pq.size() < K) + pq.push(sum); + else + { + int curr = pq.top(); + + if(curr < sum) + { + pq.pop(); + pq.push(sum); + } + else + break; + } + } + } + + vector ans; + + while(!pq.empty()) + { + ans.push_back(pq.top()); + pq.pop(); + } + + reverse(ans.begin(), ans.end()); + + return ans; + } +}; + +//{ Driver Code Starts. + +int main() { + int t; + cin >> t; + while (t--) { + int N, K; + cin >> N >> K; + + vector A(N), B(N); + for (int i = 0; i < N; i++) { + cin >> A[i]; + } + for (int i = 0; i < N; i++) { + cin >> B[i]; + } + Solution obj; + vector ans = obj.maxCombinations(N, K, A, B); + for (auto &it : ans) cout << it << ' '; + cout << endl; + } + return 0; +} +// } Driver Code Ends \ No newline at end of file From 14f00c41cfcb4e66ec302919b5e9fc7c9b758cba Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 27 Sep 2023 00:13:39 +0530 Subject: [PATCH 0024/1399] Create README - LeetHub --- .../README.md | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence15-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divroblox-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div792-number-of-matching-subsequences/README.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence15-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divroblox-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div792-number-of-matching-subsequences/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence15-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divroblox-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div792-number-of-matching-subsequences/README.md new file mode 100644 index 00000000..cb2c131c --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence15-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divroblox-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div792-number-of-matching-subsequences/README.md @@ -0,0 +1,32 @@ +

Google
15
Roblox
2
792. Number of Matching Subsequences

Medium


Given a string s and an array of strings words, return the number of words[i] that is a subsequence of s.

+ +

A subsequence of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters.

+ +
    +
  • For example, "ace" is a subsequence of "abcde".
  • +
+ +

 

+

Example 1:

+ +
Input: s = "abcde", words = ["a","bb","acd","ace"]
+Output: 3
+Explanation: There are three strings in words that are a subsequence of s: "a", "acd", "ace".
+
+ +

Example 2:

+ +
Input: s = "dsahjpjauf", words = ["ahjpjau","ja","ahbwzgqnuk","tnmlanowax"]
+Output: 2
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 5 * 104
  • +
  • 1 <= words.length <= 5000
  • +
  • 1 <= words[i].length <= 50
  • +
  • s and words[i] consist of only lowercase English letters.
  • +
+
\ No newline at end of file From 7bc3d1324e0c70779fc8f7d7dda9e98f04af64ed Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 27 Sep 2023 00:35:11 +0530 Subject: [PATCH 0025/1399] Create README - LeetHub --- .../README.md | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div316-remove-duplicate-letters/README.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div316-remove-duplicate-letters/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div316-remove-duplicate-letters/README.md new file mode 100644 index 00000000..86a62d23 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div316-remove-duplicate-letters/README.md @@ -0,0 +1,26 @@ +

Apple
4
Facebook
2
Amazon
2
Salesforce
2
316. Remove Duplicate Letters

Medium


Given a string s, remove duplicate letters so that every letter appears once and only once. You must make sure your result is the smallest in lexicographical order among all possible results.

+ +

 

+

Example 1:

+ +
Input: s = "bcabc"
+Output: "abc"
+
+ +

Example 2:

+ +
Input: s = "cbacdcbc"
+Output: "acdb"
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 104
  • +
  • s consists of lowercase English letters.
  • +
+ +

 

+

Note: This question is the same as 1081: https://leetcode.com/problems/smallest-subsequence-of-distinct-characters/

+
\ No newline at end of file From d7796a15e0392fa8c77f5e347870254af01b23e0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 27 Sep 2023 00:35:15 +0530 Subject: [PATCH 0026/1399] Time: 0 ms (100.00%), Space: 7 MB (17.68%) - LeetHub --- ...iv-div-div316-remove-duplicate-letters.cpp | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div316-remove-duplicate-letters/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div316-remove-duplicate-letters.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div316-remove-duplicate-letters/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div316-remove-duplicate-letters.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div316-remove-duplicate-letters/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div316-remove-duplicate-letters.cpp new file mode 100644 index 00000000..9f335b1a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div316-remove-duplicate-letters/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div316-remove-duplicate-letters.cpp @@ -0,0 +1,37 @@ +class Solution { +public: + string removeDuplicateLetters(string s) { + + int n = s.size(); + + vector freq(26, 0); + + vector visited(26, 0); + + string ans; + + for(int i = 0; i < n; ++i) + { + ++freq[s[i] - 'a']; + } + + for(int i = 0; i < n; ++i) + { + while(!ans.empty() and !visited[s[i] - 'a'] and s[i] < ans.back() and freq[ans.back()-'a']) + { + visited[ans.back() - 'a'] = false; + ans.pop_back(); + } + + if(!visited[s[i] - 'a']) + { + ans += s[i]; + visited[s[i] - 'a'] = 1; + } + + --freq[s[i] - 'a']; + } + + return ans; + } +}; \ No newline at end of file From 67a12a6a55997f99fa8750e11aa5e8675fb2ceb8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 27 Sep 2023 01:44:46 +0530 Subject: [PATCH 0027/1399] Create README - LeetHub --- .../README.md | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1081-smallest-subsequence-of-distinct-characters/README.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1081-smallest-subsequence-of-distinct-characters/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1081-smallest-subsequence-of-distinct-characters/README.md new file mode 100644 index 00000000..1e0f5f90 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1081-smallest-subsequence-of-distinct-characters/README.md @@ -0,0 +1,25 @@ +

No companies found for this problem
1081. Smallest Subsequence of Distinct Characters

Medium


Given a string s, return the lexicographically smallest subsequence of s that contains all the distinct characters of s exactly once.

+ +

 

+

Example 1:

+ +
Input: s = "bcabc"
+Output: "abc"
+
+ +

Example 2:

+ +
Input: s = "cbacdcbc"
+Output: "acdb"
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 1000
  • +
  • s consists of lowercase English letters.
  • +
+ +

 

+Note: This question is the same as 316: https://leetcode.com/problems/remove-duplicate-letters/
\ No newline at end of file From b9173143f0468b7b4efdb2523d442eeb556f4c6a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 27 Sep 2023 01:44:51 +0530 Subject: [PATCH 0028/1399] Time: 0 ms (100.00%), Space: 6.6 MB (28.42%) - LeetHub --- ...est-subsequence-of-distinct-characters.cpp | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1081-smallest-subsequence-of-distinct-characters/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1081-smallest-subsequence-of-distinct-characters.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1081-smallest-subsequence-of-distinct-characters/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1081-smallest-subsequence-of-distinct-characters.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1081-smallest-subsequence-of-distinct-characters/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1081-smallest-subsequence-of-distinct-characters.cpp new file mode 100644 index 00000000..b9602471 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1081-smallest-subsequence-of-distinct-characters/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1081-smallest-subsequence-of-distinct-characters.cpp @@ -0,0 +1,34 @@ +class Solution { +public: + string smallestSubsequence(string s) { + + int n = s.size(); + + vector lastOccurence(26, -1); + + vector visited(26, false); + + for(int i = 0; i < n; ++i) + lastOccurence[s[i] - 'a'] = i; + + string ans; + + for(int i = 0; i < n; ++i) + { + while(!ans.empty() and !visited[s[i] - 'a'] and s[i] < ans.back() and lastOccurence[ans.back() - 'a'] > i) + { + visited[ans.back() - 'a'] = false; + + ans.pop_back(); + } + + if(!visited[s[i] - 'a']) + { + ans += s[i]; + visited[s[i] - 'a'] = true; + } + } + + return ans; + } +}; \ No newline at end of file From f3a960f54ac4ac8d464e22eb17bb477bc76e54a6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 27 Sep 2023 01:45:02 +0530 Subject: [PATCH 0029/1399] Create README - LeetHub --- .../README.md | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 1081-smallest-subsequence-of-distinct-characters/README.md diff --git a/1081-smallest-subsequence-of-distinct-characters/README.md b/1081-smallest-subsequence-of-distinct-characters/README.md new file mode 100644 index 00000000..27cd917c --- /dev/null +++ b/1081-smallest-subsequence-of-distinct-characters/README.md @@ -0,0 +1,25 @@ +

1081. Smallest Subsequence of Distinct Characters

Medium


Given a string s, return the lexicographically smallest subsequence of s that contains all the distinct characters of s exactly once.

+ +

 

+

Example 1:

+ +
Input: s = "bcabc"
+Output: "abc"
+
+ +

Example 2:

+ +
Input: s = "cbacdcbc"
+Output: "acdb"
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 1000
  • +
  • s consists of lowercase English letters.
  • +
+ +

 

+Note: This question is the same as 316: https://leetcode.com/problems/remove-duplicate-letters/
\ No newline at end of file From 71196480b14969080ab2d5e38ecfda1cd65fe966 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 27 Sep 2023 01:45:03 +0530 Subject: [PATCH 0030/1399] Attach NOTES - LeetHub --- 1081-smallest-subsequence-of-distinct-characters/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1081-smallest-subsequence-of-distinct-characters/NOTES.md diff --git a/1081-smallest-subsequence-of-distinct-characters/NOTES.md b/1081-smallest-subsequence-of-distinct-characters/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1081-smallest-subsequence-of-distinct-characters/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From b3b9d4fe9c9061f3fd684e9f538e60c6c8d7ffec Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 27 Sep 2023 01:45:06 +0530 Subject: [PATCH 0031/1399] Time: 0 ms (100.00%), Space: 6.6 MB (28.42%) - LeetHub --- ...est-subsequence-of-distinct-characters.cpp | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 1081-smallest-subsequence-of-distinct-characters/1081-smallest-subsequence-of-distinct-characters.cpp diff --git a/1081-smallest-subsequence-of-distinct-characters/1081-smallest-subsequence-of-distinct-characters.cpp b/1081-smallest-subsequence-of-distinct-characters/1081-smallest-subsequence-of-distinct-characters.cpp new file mode 100644 index 00000000..b9602471 --- /dev/null +++ b/1081-smallest-subsequence-of-distinct-characters/1081-smallest-subsequence-of-distinct-characters.cpp @@ -0,0 +1,34 @@ +class Solution { +public: + string smallestSubsequence(string s) { + + int n = s.size(); + + vector lastOccurence(26, -1); + + vector visited(26, false); + + for(int i = 0; i < n; ++i) + lastOccurence[s[i] - 'a'] = i; + + string ans; + + for(int i = 0; i < n; ++i) + { + while(!ans.empty() and !visited[s[i] - 'a'] and s[i] < ans.back() and lastOccurence[ans.back() - 'a'] > i) + { + visited[ans.back() - 'a'] = false; + + ans.pop_back(); + } + + if(!visited[s[i] - 'a']) + { + ans += s[i]; + visited[s[i] - 'a'] = true; + } + } + + return ans; + } +}; \ No newline at end of file From 2d2b17da8401ed391d938ef8e900a8b4bca2ca09 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 27 Sep 2023 22:41:04 +0530 Subject: [PATCH 0032/1399] Create README - LeetHub --- 0880-decoded-string-at-index/README.md | 46 ++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 0880-decoded-string-at-index/README.md diff --git a/0880-decoded-string-at-index/README.md b/0880-decoded-string-at-index/README.md new file mode 100644 index 00000000..2831eaa7 --- /dev/null +++ b/0880-decoded-string-at-index/README.md @@ -0,0 +1,46 @@ +

880. Decoded String at Index

Medium


You are given an encoded string s. To decode the string to a tape, the encoded string is read one character at a time and the following steps are taken:

+ +
    +
  • If the character read is a letter, that letter is written onto the tape.
  • +
  • If the character read is a digit d, the entire current tape is repeatedly written d - 1 more times in total.
  • +
+ +

Given an integer k, return the kth letter (1-indexed) in the decoded string.

+ +

 

+

Example 1:

+ +
Input: s = "leet2code3", k = 10
+Output: "o"
+Explanation: The decoded string is "leetleetcodeleetleetcodeleetleetcode".
+The 10th letter in the string is "o".
+
+ +

Example 2:

+ +
Input: s = "ha22", k = 5
+Output: "h"
+Explanation: The decoded string is "hahahaha".
+The 5th letter is "h".
+
+ +

Example 3:

+ +
Input: s = "a2345678999999999999999", k = 1
+Output: "a"
+Explanation: The decoded string is "a" repeated 8301530446056247680 times.
+The 1st letter is "a".
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= s.length <= 100
  • +
  • s consists of lowercase English letters and digits 2 through 9.
  • +
  • s starts with a letter.
  • +
  • 1 <= k <= 109
  • +
  • It is guaranteed that k is less than or equal to the length of the decoded string.
  • +
  • The decoded string is guaranteed to have less than 263 letters.
  • +
+
\ No newline at end of file From eaed5b1f5b320ccbe3d73fcdb0aabe206ef9b363 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 27 Sep 2023 22:41:07 +0530 Subject: [PATCH 0033/1399] Time: 2 ms (45.12%), Space: 6.5 MB (7.16%) - LeetHub --- .../0880-decoded-string-at-index.cpp | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 0880-decoded-string-at-index/0880-decoded-string-at-index.cpp diff --git a/0880-decoded-string-at-index/0880-decoded-string-at-index.cpp b/0880-decoded-string-at-index/0880-decoded-string-at-index.cpp new file mode 100644 index 00000000..09875315 --- /dev/null +++ b/0880-decoded-string-at-index/0880-decoded-string-at-index.cpp @@ -0,0 +1,30 @@ +class Solution { +public: + string decodeAtIndex(string s, int k) { + + int n = s.size(); + + long long totalLength = 0; + + for(int i = 0; i < n; ++i) + { + if(isalpha(s[i])) + ++totalLength; + else + totalLength = totalLength * (s[i] - '0'); + } + + for(int i = n-1; i >= 0; --i) + { + k = k % totalLength; + if(k == 0 and isalpha(s[i])) + return string(1, s[i]); + if(isalpha(s[i])) + totalLength -= 1; + else + totalLength /= (s[i] - '0'); + } + + return ""; + } +}; \ No newline at end of file From 4132ea9eaf7c842eb1fb219f11d15e64fe6557c1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 28 Sep 2023 07:57:33 +0530 Subject: [PATCH 0034/1399] Create README - LeetHub --- Wave Array - GFG/README.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Wave Array - GFG/README.md diff --git a/Wave Array - GFG/README.md b/Wave Array - GFG/README.md new file mode 100644 index 00000000..dc773438 --- /dev/null +++ b/Wave Array - GFG/README.md @@ -0,0 +1,24 @@ +# Wave Array +## Easy +

Given a sorted array arr[] of distinct integers. Sort the array into a wave-like array(In Place).
In other words, arrange the elements into a sequence such that arr[1] >= arr[2] <= arr[3] >= arr[4] <= arr[5].....

+

If there are multiple solutions, find the lexicographically smallest one.

+

Note:The given array is sorted in ascending order, and you don't need to return anything to make changes in the original array itself.

+

Example 1:

+
Input:
+n = 5
+arr[] = {1,2,3,4,5}
+Output: 2 1 4 3 5
+Explanation: Array elements after 
+sorting it in wave form are 
+2 1 4 3 5.
+

Example 2:

+
Input:
+n = 6
+arr[] = {2,4,7,8,9,10}
+Output: 4 2 8 7 10 9
+Explanation: Array elements after 
+sorting it in wave form are 
+4 2 8 7 10 9.
+

Your Task:
The task is to complete the function convertToWave(), which converts the given array to a wave array.

+

Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).

+

Constraints:
1 ≤ n ≤ 106
0 ≤ arr[i] ≤107

\ No newline at end of file From c2a253fb0d21de4b68466d4972eb044f9ff3adfc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 28 Sep 2023 07:57:33 +0530 Subject: [PATCH 0035/1399] Added solution - LeetHub --- Wave Array - GFG/wave-array.cpp | 47 +++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 Wave Array - GFG/wave-array.cpp diff --git a/Wave Array - GFG/wave-array.cpp b/Wave Array - GFG/wave-array.cpp new file mode 100644 index 00000000..21ee8fe1 --- /dev/null +++ b/Wave Array - GFG/wave-array.cpp @@ -0,0 +1,47 @@ +//{ Driver Code Starts +#include +using namespace std; +// #include + + +// } Driver Code Ends +class Solution{ + public: + // arr: input array + // n: size of array + //Function to sort the array into a wave-like array. + void convertToWave(int n, vector& arr){ + + // Your code here + + for(int i = 0; i < n-1; i += 2) + { + swap(arr[i], arr[i+1]); + } + + } +}; + +//{ Driver Code Starts. + +int main() +{ + int t,n; + cin>>t; //Input testcases + while(t--) //While testcases exist + { + cin>>n; //input size of array + vector a(n); //declare vector of size n + for(int i=0;i>a[i]; //input elements of array + sort(a.begin(),a.end()); + Solution ob; + ob.convertToWave(n, a); + + for(int i=0;i Date: Thu, 28 Sep 2023 08:26:47 +0530 Subject: [PATCH 0036/1399] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div905-sort-array-by-parity/NOTES.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div905-sort-array-by-parity/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div905-sort-array-by-parity/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div905-sort-array-by-parity/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 6ba15dee5e178e42c3a18848a3ecc9145e917de3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 28 Sep 2023 08:26:50 +0530 Subject: [PATCH 0037/1399] Time: 14 ms (13.55%), Space: 16.6 MB (18.22%) - LeetHub --- ...iv-div-div-div905-sort-array-by-parity.cpp | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div905-sort-array-by-parity/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div905-sort-array-by-parity.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div905-sort-array-by-parity/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div905-sort-array-by-parity.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div905-sort-array-by-parity/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div905-sort-array-by-parity.cpp new file mode 100644 index 00000000..6455ee55 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div905-sort-array-by-parity/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div905-sort-array-by-parity.cpp @@ -0,0 +1,22 @@ +class Solution { +public: + vector sortArrayByParity(vector& nums) { + + int n = nums.size(); + + int start = 0, end = n-1; + + while(start < end) + { + if(nums[start] & 1) + { + swap(nums[start], nums[end--]); + } + else + ++start; + } + + return nums; + + } +}; \ No newline at end of file From 19d81384446e35799d1a6c55381a554ac5191f91 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 29 Sep 2023 07:17:26 +0530 Subject: [PATCH 0038/1399] Create README - LeetHub --- .../README.md | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div896-monotonic-array/README.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div896-monotonic-array/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div896-monotonic-array/README.md new file mode 100644 index 00000000..cb6f2c41 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div896-monotonic-array/README.md @@ -0,0 +1,33 @@ +

Facebook
12
Amazon
2
896. Monotonic Array

Easy


An array is monotonic if it is either monotone increasing or monotone decreasing.

+ +

An array nums is monotone increasing if for all i <= j, nums[i] <= nums[j]. An array nums is monotone decreasing if for all i <= j, nums[i] >= nums[j].

+ +

Given an integer array nums, return true if the given array is monotonic, or false otherwise.

+ +

 

+

Example 1:

+ +
Input: nums = [1,2,2,3]
+Output: true
+
+ +

Example 2:

+ +
Input: nums = [6,5,4,4]
+Output: true
+
+ +

Example 3:

+ +
Input: nums = [1,3,2]
+Output: false
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 105
  • +
  • -105 <= nums[i] <= 105
  • +
+
\ No newline at end of file From feaf03ecc319647717ac364ee8305a296ee55f2f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 29 Sep 2023 07:17:30 +0530 Subject: [PATCH 0039/1399] Time: 129 ms (35.19%), Space: 96.8 MB (22.05%) - LeetHub --- ...t-svg-div-div-div-div896-monotonic-array.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div896-monotonic-array/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div896-monotonic-array.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div896-monotonic-array/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div896-monotonic-array.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div896-monotonic-array/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div896-monotonic-array.cpp new file mode 100644 index 00000000..ffa18222 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div896-monotonic-array/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div896-monotonic-array.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + bool isMonotonic(vector& nums) { + + int n = nums.size(); + + bool incr = true, decr = true; + + for(int i = 1; i < n; ++i) + { + incr &= (nums[i-1] <= nums[i]); + decr &= (nums[i-1] >= nums[i]); + } + + return incr or decr; + } +}; \ No newline at end of file From 717547cb59cb35587d7fb130a3cbdca387cbcd85 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 29 Sep 2023 07:41:18 +0530 Subject: [PATCH 0040/1399] Create README - LeetHub --- Number Of Enclaves - GFG/README.md | 45 ++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 Number Of Enclaves - GFG/README.md diff --git a/Number Of Enclaves - GFG/README.md b/Number Of Enclaves - GFG/README.md new file mode 100644 index 00000000..37711e95 --- /dev/null +++ b/Number Of Enclaves - GFG/README.md @@ -0,0 +1,45 @@ +# Number Of Enclaves +## Medium +

You are given an n x m binary matrix grid, where 0 represents a sea cell and 1 represents a land cell.

+

A move consists of walking from one land cell to another adjacent (4-directionally) land cell or walking off the boundary of the grid.

+

Find the number of land cells in grid for which we cannot walk off the boundary of the grid in any number of moves.

+

Example 1:

+
Input:
+grid[][] = {{0, 0, 0, 0},
+            {1, 0, 1, 0},
+            {0, 1, 1, 0},
+            {0, 0, 0, 0}}
+Output:
+3
+Explanation:
+0 0 0 0
+1 0 1 0
+0 1 1 0
+0 0 0 0
+The highlighted cells represents the land cells.
+
+

Example 2:

+
Input:
+grid[][] = {{0, 0, 0, 1},
+            {0, 1, 1, 0},
+            {0, 1, 1, 0},
+            {0, 0, 0, 1},
+            {0, 1, 1, 0}}
+Output:
+4
+Explanation:
+0 0 0 1
+0 1 1 0
+0 1 1 0
+0 0 0 1
+0 1 1 0
+The highlighted cells represents the land cells.
+

Your Task:

+

You don't need to print or input anything. Complete the function numberOfEnclaves() which takes a 2D integer matrix grid as the input parameter and returns an integer, denoting the number of land cells.

+

Expected Time Complexity: O(n * m)

+

Expected Space Complexity: O(n * m)

+

Constraints:

+
    +
  • 1 <= n, m <= 500
  • +
  • grid[i][j] == 0 or 1
  • +
\ No newline at end of file From ed12b7095ef9c58ea7807a7f1eeaf662a640c0ab Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 29 Sep 2023 07:41:19 +0530 Subject: [PATCH 0041/1399] Added solution - LeetHub --- .../number-of-enclaves.cpp | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 Number Of Enclaves - GFG/number-of-enclaves.cpp diff --git a/Number Of Enclaves - GFG/number-of-enclaves.cpp b/Number Of Enclaves - GFG/number-of-enclaves.cpp new file mode 100644 index 00000000..e61366c8 --- /dev/null +++ b/Number Of Enclaves - GFG/number-of-enclaves.cpp @@ -0,0 +1,86 @@ +//{ Driver Code Starts +// Initial Template for C++ + +#include +using namespace std; + + +// } Driver Code Ends +// User function Template for C++ + +class Solution { + public: + int numberOfEnclaves(vector> &grid) { + // Code here + + int n = grid.size(); + int m = grid[0].size(); + + int landCell = 0; + + vector dx = {0, +1, 0, -1}; + vector dy = {+1, 0, -1, 0}; + + vector> visited(n, vector(m, false)); + + function dfs = [&](int i, int j){ + + visited[i][j] = true; + + for(int x = 0; x < 4; ++x) + { + int newX = dx[x] + i; + int newY = dy[x] + j; + + if(newX >= 0 and newY >= 0 and newX < n and newY < m and !visited[newX][newY] and grid[newX][newY] == 1) + { + dfs(newX, newY); + } + } + + }; + + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < m; ++j) + { + if((i == 0 or j == 0 or i == n-1 or j == m-1) and (grid[i][j] == 1 and !visited[i][j])) + { + dfs(i,j); + } + } + } + + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < m; ++j) + { + if(!visited[i][j] and grid[i][j] == 1) + ++landCell; + } + } + + return landCell; + } +}; + + +//{ Driver Code Starts. + +int main() { + int t; + cin >> t; + while (t--) { + int n, m; + cin >> n >> m; + vector> grid(n, vector(m)); + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + cin >> grid[i][j]; + } + } + Solution obj; + cout << obj.numberOfEnclaves(grid) << endl; + } +} +// } Driver Code Ends \ No newline at end of file From f226916b09703f6a5a0292dd4dcbac4218cafd75 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 30 Sep 2023 18:14:38 +0530 Subject: [PATCH 0042/1399] Create README - LeetHub --- .../README.md | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence4-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div456-132-pattern/README.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence4-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div456-132-pattern/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence4-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div456-132-pattern/README.md new file mode 100644 index 00000000..c0815542 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence4-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div456-132-pattern/README.md @@ -0,0 +1,35 @@ +

Amazon
4
456. 132 Pattern

Medium


Given an array of n integers nums, a 132 pattern is a subsequence of three integers nums[i], nums[j] and nums[k] such that i < j < k and nums[i] < nums[k] < nums[j].

+ +

Return true if there is a 132 pattern in nums, otherwise, return false.

+ +

 

+

Example 1:

+ +
Input: nums = [1,2,3,4]
+Output: false
+Explanation: There is no 132 pattern in the sequence.
+
+ +

Example 2:

+ +
Input: nums = [3,1,4,2]
+Output: true
+Explanation: There is a 132 pattern in the sequence: [1, 4, 2].
+
+ +

Example 3:

+ +
Input: nums = [-1,3,2,0]
+Output: true
+Explanation: There are three 132 patterns in the sequence: [-1, 3, 2], [-1, 3, 0] and [-1, 2, 0].
+
+ +

 

+

Constraints:

+ +
    +
  • n == nums.length
  • +
  • 1 <= n <= 2 * 105
  • +
  • -109 <= nums[i] <= 109
  • +
+
\ No newline at end of file From a236469e76504ef15db5e85dcca6b6960d87839b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 30 Sep 2023 18:14:41 +0530 Subject: [PATCH 0043/1399] Time: 68 ms (58.88%), Space: 49.7 MB (22.77%) - LeetHub --- ...ect-svg-div-div-div-div456-132-pattern.cpp | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence4-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div456-132-pattern/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence4-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div456-132-pattern.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence4-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div456-132-pattern/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence4-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div456-132-pattern.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence4-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div456-132-pattern/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence4-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div456-132-pattern.cpp new file mode 100644 index 00000000..ead08dec --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence4-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div456-132-pattern/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence4-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div456-132-pattern.cpp @@ -0,0 +1,31 @@ +class Solution { +public: + bool find132pattern(vector& nums) { + + int n = nums.size(); + + vector prefMin(n); + + prefMin[0] = nums[0]; + + for(int i = 1; i < n; ++i) + prefMin[i] = min(nums[i], prefMin[i-1]); + + stack st; + + for(int i = n-1; i >= 0; --i) + { + if(nums[i] > prefMin[i]) + { + while(!st.empty() and st.top() <= prefMin[i]) + st.pop(); + if(!st.empty() and nums[i] > st.top()) + return true; + st.push(nums[i]); + } + } + + return false; + + } +}; \ No newline at end of file From 1f34488e1adb7989eec688f8a73b5bb61d6cfcd8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 30 Sep 2023 18:18:32 +0530 Subject: [PATCH 0044/1399] Create README - LeetHub --- 0456-132-pattern/README.md | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 0456-132-pattern/README.md diff --git a/0456-132-pattern/README.md b/0456-132-pattern/README.md new file mode 100644 index 00000000..049ce47c --- /dev/null +++ b/0456-132-pattern/README.md @@ -0,0 +1,35 @@ +

456. 132 Pattern

Medium


Given an array of n integers nums, a 132 pattern is a subsequence of three integers nums[i], nums[j] and nums[k] such that i < j < k and nums[i] < nums[k] < nums[j].

+ +

Return true if there is a 132 pattern in nums, otherwise, return false.

+ +

 

+

Example 1:

+ +
Input: nums = [1,2,3,4]
+Output: false
+Explanation: There is no 132 pattern in the sequence.
+
+ +

Example 2:

+ +
Input: nums = [3,1,4,2]
+Output: true
+Explanation: There is a 132 pattern in the sequence: [1, 4, 2].
+
+ +

Example 3:

+ +
Input: nums = [-1,3,2,0]
+Output: true
+Explanation: There are three 132 patterns in the sequence: [-1, 3, 2], [-1, 3, 0] and [-1, 2, 0].
+
+ +

 

+

Constraints:

+ +
    +
  • n == nums.length
  • +
  • 1 <= n <= 2 * 105
  • +
  • -109 <= nums[i] <= 109
  • +
+
\ No newline at end of file From 2495ac029029ed5c1bca5773ec506e54d9e86435 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 30 Sep 2023 18:18:32 +0530 Subject: [PATCH 0045/1399] Attach NOTES - LeetHub --- 0456-132-pattern/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0456-132-pattern/NOTES.md diff --git a/0456-132-pattern/NOTES.md b/0456-132-pattern/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0456-132-pattern/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 8cbf6b0af886473e992dde6352ab21d6ae60ab16 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 30 Sep 2023 18:18:35 +0530 Subject: [PATCH 0046/1399] Time: 68 ms (58.88%), Space: 49.7 MB (22.77%) - LeetHub --- 0456-132-pattern/0456-132-pattern.cpp | 31 +++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 0456-132-pattern/0456-132-pattern.cpp diff --git a/0456-132-pattern/0456-132-pattern.cpp b/0456-132-pattern/0456-132-pattern.cpp new file mode 100644 index 00000000..ead08dec --- /dev/null +++ b/0456-132-pattern/0456-132-pattern.cpp @@ -0,0 +1,31 @@ +class Solution { +public: + bool find132pattern(vector& nums) { + + int n = nums.size(); + + vector prefMin(n); + + prefMin[0] = nums[0]; + + for(int i = 1; i < n; ++i) + prefMin[i] = min(nums[i], prefMin[i-1]); + + stack st; + + for(int i = n-1; i >= 0; --i) + { + if(nums[i] > prefMin[i]) + { + while(!st.empty() and st.top() <= prefMin[i]) + st.pop(); + if(!st.empty() and nums[i] > st.top()) + return true; + st.push(nums[i]); + } + } + + return false; + + } +}; \ No newline at end of file From 58be5760861e604002c6c5bed293578bf1694052 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 30 Sep 2023 19:33:31 +0530 Subject: [PATCH 0047/1399] Create README - LeetHub --- Boolean Matrix - GFG/README.md | 35 ++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 Boolean Matrix - GFG/README.md diff --git a/Boolean Matrix - GFG/README.md b/Boolean Matrix - GFG/README.md new file mode 100644 index 00000000..5c0cc0a5 --- /dev/null +++ b/Boolean Matrix - GFG/README.md @@ -0,0 +1,35 @@ +# Boolean Matrix +## Medium +

Given a boolean matrix of size RxC where each cell contains either 0 or 1, modify it such that if a matrix cell matrix[i][j] is 1 then all the cells in its ith row and jth column will become 1.

+

Example 1:

+
Input:
+R = 2, C = 2
+matrix[][] = {{1, 0},
+              {0, 0}}
+Output: 
+1 1
+1 0 
+Explanation:
+Only cell that has 1 is at (0,0) so all 
+cells in row 0 are modified to 1 and all 
+cells in column 0 are modified to 1.
+


Example 2:

+
Input:
+R = 4, C = 3
+matrix[][] = {{ 1, 0, 0},
+              { 1, 0, 0},
+              { 1, 0, 0},
+              { 0, 0, 0}}
+Output: 
+1 1 1
+1 1 1
+1 1 1
+1 0 0 
+Explanation:
+The position of cells that have 1 in
+the original matrix are (0,0), (1,0)
+and (2,0). Therefore, all cells in row
+0,1,2 are and column 0 are modified to 1. 
+

Your Task:
You dont need to read input or print anything. Complete the function booleanMatrix() that takes the matrix as input parameter and modifies it in-place.

+

Expected Time Complexity: O(R * C)
Expected Auxiliary Space: O(R + C) 

+

Constraints:
1 ≤ R, C ≤ 1000
0 ≤ matrix[i][j] ≤ 1

\ No newline at end of file From bcc7153d7f77dc02444e1050f0e577bbb2d98e52 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 30 Sep 2023 19:33:32 +0530 Subject: [PATCH 0048/1399] Added solution - LeetHub --- Boolean Matrix - GFG/boolean-matrix.cpp | 81 +++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 Boolean Matrix - GFG/boolean-matrix.cpp diff --git a/Boolean Matrix - GFG/boolean-matrix.cpp b/Boolean Matrix - GFG/boolean-matrix.cpp new file mode 100644 index 00000000..fd1cca9b --- /dev/null +++ b/Boolean Matrix - GFG/boolean-matrix.cpp @@ -0,0 +1,81 @@ +//{ Driver Code Starts +#include +using namespace std; + +// } Driver Code Ends + + +class Solution +{ + public: + //Function to modify the matrix such that if a matrix cell matrix[i][j] + //is 1 then all the cells in its ith row and jth column will become 1. + void booleanMatrix(vector > &matrix) + { + // code here + + int n = matrix.size(); + int m = matrix[0].size(); + + vector row(n, 0), col(m, 0); + + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < m; ++j) + { + if(matrix[i][j] == 1) + { + row[i] = col[j] = 1; + } + } + } + + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < m; ++j) + { + if(row[i] or col[j]) + matrix[i][j] = 1; + } + } + } +}; + + +//{ Driver Code Starts. +int main() { + int t; + cin>>t; + while(t--) + { + int row, col; + cin>> row>> col; + vector > matrix(row); + for(int i=0; i>matrix[i][j]; + } + } + + Solution ob; + ob.booleanMatrix(matrix); + + + for (int i = 0; i < row; ++i) + { + for (int j = 0; j < col; ++j) + { + cout< Date: Sat, 30 Sep 2023 23:47:46 +0530 Subject: [PATCH 0049/1399] Attach NOTES - LeetHub From a1d4cf6e0af96b9255ac9567e56a6203068a33da Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 30 Sep 2023 23:47:50 +0530 Subject: [PATCH 0050/1399] Time: 58 ms (92.94%), Space: 48.9 MB (37.36%) - LeetHub --- 0456-132-pattern/0456-132-pattern.cpp | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/0456-132-pattern/0456-132-pattern.cpp b/0456-132-pattern/0456-132-pattern.cpp index ead08dec..a1973605 100644 --- a/0456-132-pattern/0456-132-pattern.cpp +++ b/0456-132-pattern/0456-132-pattern.cpp @@ -4,25 +4,22 @@ class Solution { int n = nums.size(); - vector prefMin(n); - - prefMin[0] = nums[0]; - - for(int i = 1; i < n; ++i) - prefMin[i] = min(nums[i], prefMin[i-1]); + int s3 = INT_MIN; stack st; for(int i = n-1; i >= 0; --i) { - if(nums[i] > prefMin[i]) + if(nums[i] < s3) + return true; + + while(!st.empty() and st.top() < nums[i]) { - while(!st.empty() and st.top() <= prefMin[i]) - st.pop(); - if(!st.empty() and nums[i] > st.top()) - return true; - st.push(nums[i]); + s3 = st.top(); + st.pop(); } + + st.push(nums[i]); } return false; From eebd7750cc515225ac03a6de2bab483daba97efa Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 1 Oct 2023 11:06:30 +0530 Subject: [PATCH 0051/1399] Create README - LeetHub --- .../README.md | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbolt-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divyandex-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divpaytm-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div557-reverse-words-in-a-string-iii/README.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbolt-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divyandex-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divpaytm-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div557-reverse-words-in-a-string-iii/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbolt-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divyandex-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divpaytm-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div557-reverse-words-in-a-string-iii/README.md new file mode 100644 index 00000000..b13bbc68 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbolt-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divyandex-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divpaytm-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div557-reverse-words-in-a-string-iii/README.md @@ -0,0 +1,21 @@ +

Microsoft
5
Amazon
3
Bolt
3
Yandex
2
Facebook
2
Apple
2
PayTM
2
557. Reverse Words in a String III

Easy


Given a string s, reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.

+ +

 

+

Example 1:

+
Input: s = "Let's take LeetCode contest"
+Output: "s'teL ekat edoCteeL tsetnoc"
+

Example 2:

+
Input: s = "God Ding"
+Output: "doG gniD"
+
+

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 5 * 104
  • +
  • s contains printable ASCII characters.
  • +
  • s does not contain any leading or trailing spaces.
  • +
  • There is at least one word in s.
  • +
  • All the words in s are separated by a single space.
  • +
+
\ No newline at end of file From 6ec9a92c55d9da58b055715cf671f9fdc69ce0a9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 1 Oct 2023 11:06:34 +0530 Subject: [PATCH 0052/1399] Time: 18 ms (35.38%), Space: 14.5 MB (16.76%) - LeetHub --- ...v-div557-reverse-words-in-a-string-iii.cpp | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbolt-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divyandex-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divpaytm-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div557-reverse-words-in-a-string-iii/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbolt-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divyandex-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divpaytm-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div557-reverse-words-in-a-string-iii.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbolt-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divyandex-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divpaytm-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div557-reverse-words-in-a-string-iii/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbolt-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divyandex-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divpaytm-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div557-reverse-words-in-a-string-iii.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbolt-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divyandex-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divpaytm-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div557-reverse-words-in-a-string-iii/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbolt-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divyandex-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divpaytm-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div557-reverse-words-in-a-string-iii.cpp new file mode 100644 index 00000000..610932ec --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbolt-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divyandex-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divpaytm-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div557-reverse-words-in-a-string-iii/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbolt-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divyandex-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divpaytm-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div557-reverse-words-in-a-string-iii.cpp @@ -0,0 +1,22 @@ +class Solution { +public: + string reverseWords(string s) { + + stringstream ss(s); + + string word; + + string ans; + + while(ss >> word) + { + reverse(word.begin(), word.end()); + ans += word + ' '; + } + + ans.pop_back(); + + return ans; + + } +}; \ No newline at end of file From f5542df0e87bd6c8837e9bd1add0eee36320fcff Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 1 Oct 2023 11:07:26 +0530 Subject: [PATCH 0053/1399] Create README - LeetHub --- 0557-reverse-words-in-a-string-iii/README.md | 21 ++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 0557-reverse-words-in-a-string-iii/README.md diff --git a/0557-reverse-words-in-a-string-iii/README.md b/0557-reverse-words-in-a-string-iii/README.md new file mode 100644 index 00000000..cdc6104c --- /dev/null +++ b/0557-reverse-words-in-a-string-iii/README.md @@ -0,0 +1,21 @@ +

557. Reverse Words in a String III

Easy


Given a string s, reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.

+ +

 

+

Example 1:

+
Input: s = "Let's take LeetCode contest"
+Output: "s'teL ekat edoCteeL tsetnoc"
+

Example 2:

+
Input: s = "God Ding"
+Output: "doG gniD"
+
+

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 5 * 104
  • +
  • s contains printable ASCII characters.
  • +
  • s does not contain any leading or trailing spaces.
  • +
  • There is at least one word in s.
  • +
  • All the words in s are separated by a single space.
  • +
+
\ No newline at end of file From e9286719274ba5620a495688d453b4bd3053c7cd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 1 Oct 2023 11:07:27 +0530 Subject: [PATCH 0054/1399] Attach NOTES - LeetHub --- 0557-reverse-words-in-a-string-iii/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0557-reverse-words-in-a-string-iii/NOTES.md diff --git a/0557-reverse-words-in-a-string-iii/NOTES.md b/0557-reverse-words-in-a-string-iii/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0557-reverse-words-in-a-string-iii/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From d2b268ba089b341e384ab406fc5d1fb32c33d28e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 1 Oct 2023 11:07:30 +0530 Subject: [PATCH 0055/1399] Time: 18 ms (35.38%), Space: 14.5 MB (16.76%) - LeetHub --- .../0557-reverse-words-in-a-string-iii.cpp | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 0557-reverse-words-in-a-string-iii/0557-reverse-words-in-a-string-iii.cpp diff --git a/0557-reverse-words-in-a-string-iii/0557-reverse-words-in-a-string-iii.cpp b/0557-reverse-words-in-a-string-iii/0557-reverse-words-in-a-string-iii.cpp new file mode 100644 index 00000000..610932ec --- /dev/null +++ b/0557-reverse-words-in-a-string-iii/0557-reverse-words-in-a-string-iii.cpp @@ -0,0 +1,22 @@ +class Solution { +public: + string reverseWords(string s) { + + stringstream ss(s); + + string word; + + string ans; + + while(ss >> word) + { + reverse(word.begin(), word.end()); + ans += word + ' '; + } + + ans.pop_back(); + + return ans; + + } +}; \ No newline at end of file From 3a9248693d4cb5637b0e2df4c86bed0ed98322fa Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 1 Oct 2023 11:09:00 +0530 Subject: [PATCH 0056/1399] Attach NOTES - LeetHub From ae6149fa06835e3618af466d693ee7d7d9da995b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 1 Oct 2023 11:09:03 +0530 Subject: [PATCH 0057/1399] Time: 18 ms (35.38%), Space: 14.5 MB (16.76%) - LeetHub From b0fdffba3df0e041b46d4a68d27e64ed42a73d21 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 1 Oct 2023 13:41:38 +0530 Subject: [PATCH 0058/1399] Create README - LeetHub --- .../README.md | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2876-count-visited-nodes-in-a-directed-graph/README.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2876-count-visited-nodes-in-a-directed-graph/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2876-count-visited-nodes-in-a-directed-graph/README.md new file mode 100644 index 00000000..a4dcde69 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2876-count-visited-nodes-in-a-directed-graph/README.md @@ -0,0 +1,41 @@ +

No companies found for this problem
2876. Count Visited Nodes in a Directed Graph

Hard


There is a directed graph consisting of n nodes numbered from 0 to n - 1 and n directed edges.

+ +

You are given a 0-indexed array edges where edges[i] indicates that there is an edge from node i to node edges[i].

+ +

Consider the following process on the graph:

+ +
    +
  • You start from a node x and keep visiting other nodes through edges until you reach a node that you have already visited before on this same process.
  • +
+ +

Return an array answer where answer[i] is the number of different nodes that you will visit if you perform the process starting from node i.

+ +

 

+

Example 1:

+ +
Input: edges = [1,2,0,0]
+Output: [3,3,3,4]
+Explanation: We perform the process starting from each node in the following way:
+- Starting from node 0, we visit the nodes 0 -> 1 -> 2 -> 0. The number of different nodes we visit is 3.
+- Starting from node 1, we visit the nodes 1 -> 2 -> 0 -> 1. The number of different nodes we visit is 3.
+- Starting from node 2, we visit the nodes 2 -> 0 -> 1 -> 2. The number of different nodes we visit is 3.
+- Starting from node 3, we visit the nodes 3 -> 0 -> 1 -> 2 -> 0. The number of different nodes we visit is 4.
+
+ +

Example 2:

+ +
Input: edges = [1,2,3,4,0]
+Output: [5,5,5,5,5]
+Explanation: Starting from any node we can visit every node in the graph in the process.
+
+ +

 

+

Constraints:

+ +
    +
  • n == edges.length
  • +
  • 2 <= n <= 105
  • +
  • 0 <= edges[i] <= n - 1
  • +
  • edges[i] != i
  • +
+
\ No newline at end of file From dd2770383668752fb2a8ebc29adee067ed144a38 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 1 Oct 2023 13:41:38 +0530 Subject: [PATCH 0059/1399] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2876-count-visited-nodes-in-a-directed-graph/NOTES.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2876-count-visited-nodes-in-a-directed-graph/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2876-count-visited-nodes-in-a-directed-graph/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2876-count-visited-nodes-in-a-directed-graph/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 6dc1651687d999df16a848991ba4cf78f9960b43 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 1 Oct 2023 13:41:42 +0530 Subject: [PATCH 0060/1399] Time: 247 ms (40.00%), Space: 161 MB (100.00%) - LeetHub --- ...ount-visited-nodes-in-a-directed-graph.cpp | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2876-count-visited-nodes-in-a-directed-graph/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2876-count-visited-nodes-in-a-directed-graph.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2876-count-visited-nodes-in-a-directed-graph/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2876-count-visited-nodes-in-a-directed-graph.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2876-count-visited-nodes-in-a-directed-graph/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2876-count-visited-nodes-in-a-directed-graph.cpp new file mode 100644 index 00000000..878be0de --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2876-count-visited-nodes-in-a-directed-graph/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2876-count-visited-nodes-in-a-directed-graph.cpp @@ -0,0 +1,84 @@ +class Solution { +public: + vector countVisitedNodes(vector& edges) { + + int n = edges.size(); + + vector indegree(n, 0); + + for(int i = 0; i < n; ++i) + { + ++indegree[edges[i]]; + } + + vector visited(n, false); + + queue q; + stack st; + + for(int i = 0; i < n; ++i) + { + if(indegree[i] == 0) + { + q.push(i); + } + } + + while(!q.empty()) + { + int curr = q.front(); + visited[curr] = true; + st.push(curr); + q.pop(); + + if(--indegree[edges[curr]] == 0) + { + q.push(edges[curr]); + } + } + + vector res(n, 0); + + function dfs = [&](int sv) + { + int length = 0; + + for(int i = sv; !visited[i]; i = edges[i]) + { + visited[i] = true; + ++length; + } + + res[sv] = length; + + for(int i = edges[sv]; i != sv; i = edges[i]) + { + res[i] = length; + } + }; + + for(int i = 0; i < n; ++i) + { + if(!visited[i]) + { + dfs(i); + } + } + + for(int i = 0; i < n; ++i) + { + if(!visited[i]) + dfs(i); + } + + while(!st.empty()) + { + int curr = st.top(); + res[curr] = res[edges[curr]] + 1; + st.pop(); + } + + return res; + + } +}; \ No newline at end of file From ef1556c76c50ca55488d966830e3a93c28211963 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 1 Oct 2023 19:44:24 +0530 Subject: [PATCH 0061/1399] Create README - LeetHub --- Boundary traversal of matrix - GFG/README.md | 28 ++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Boundary traversal of matrix - GFG/README.md diff --git a/Boundary traversal of matrix - GFG/README.md b/Boundary traversal of matrix - GFG/README.md new file mode 100644 index 00000000..99be67a8 --- /dev/null +++ b/Boundary traversal of matrix - GFG/README.md @@ -0,0 +1,28 @@ +# Boundary traversal of matrix +## Easy +

You are given a matrix of dimensions n x m. The task is to perform boundary traversal on the matrix in a clockwise manner.

Example 1:

+
Input:
+n = 4, m = 4
+matrix[][] = {{1, 2, 3, 4},
+         {5, 6, 7, 8},
+         {9, 10, 11, 12},
+         {13, 14, 15,16}}
+Output: 1 2 3 4 8 12 16 15 14 13 9 5
+Explanation:
+The matrix is:
+1 2 3 4
+5 6 7 8
+9 10 11 12
+13 14 15 16
+The boundary traversal is:
+1 2 3 4 8 12 16 15 14 13 9 5
+
+

Example 2:

+
Input:
+n = 3, m = 4
+matrrix[][] = {{12, 11, 10, 9},
+         {8, 7, 6, 5},
+         {4, 3, 2, 1}}
+Output: 12 11 10 9 5 1 2 3 4 8
+
+

Your Task:
Complete the function boundaryTraversal() that takes matrix, n and m as input parameters and returns the list of integers that form the boundary traversal of the matrix in a clockwise manner.


Expected Time Complexity: O(N + M)
Expected Auxiliary Space: O(1)


Constraints:
1 <= n, m<= 1000
0 <= matrixi <= 1000

\ No newline at end of file From 5a5636a3fcea04da914ecbd8e2788047b65561d0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 1 Oct 2023 19:44:24 +0530 Subject: [PATCH 0062/1399] Added solution - LeetHub --- .../boundary-traversal-of-matrix.cpp | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 Boundary traversal of matrix - GFG/boundary-traversal-of-matrix.cpp diff --git a/Boundary traversal of matrix - GFG/boundary-traversal-of-matrix.cpp b/Boundary traversal of matrix - GFG/boundary-traversal-of-matrix.cpp new file mode 100644 index 00000000..9688b6c2 --- /dev/null +++ b/Boundary traversal of matrix - GFG/boundary-traversal-of-matrix.cpp @@ -0,0 +1,69 @@ +//{ Driver Code Starts +#include +using namespace std; + +// } Driver Code Ends + +class Solution +{ + public: + //Function to return list of integers that form the boundary + //traversal of the matrix in a clockwise manner. + vector boundaryTraversal(vector > matrix, int n, int m) + { + // code here + + vector ans; + + for(int i = 0; i < m; ++i) + ans.push_back(matrix[0][i]); + + if(n > 1) + { + for(int i = 1; i < n; ++i) + ans.push_back(matrix[i][m-1]); + + for(int i = m-2; i >= 0; --i) + ans.push_back(matrix[n-1][i]); + + if(m > 1) + { + for(int i = n-2; i >= 1; --i) + ans.push_back(matrix[i][0]); + } + } + + return ans; + } +}; + + +//{ Driver Code Starts. +int main() { + int t; + cin>>t; + + while(t--) + { + int n,m; + cin>>n>>m; + vector > matrix(n); + + for(int i=0; i>matrix[i][j]; + } + } + + Solution ob; + vector result = ob.boundaryTraversal(matrix, n, m); + for (int i = 0; i < result.size(); ++i) + cout< Date: Mon, 2 Oct 2023 08:05:23 +0530 Subject: [PATCH 0063/1399] Create README - LeetHub --- .../README.md | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divwalmart-global-tech-divdiv-classcompanytagscontainer-tagoccurence9-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2038-remove-colored-pieces-if-both-neighbors-are-the-same-color/README.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divwalmart-global-tech-divdiv-classcompanytagscontainer-tagoccurence9-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2038-remove-colored-pieces-if-both-neighbors-are-the-same-color/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divwalmart-global-tech-divdiv-classcompanytagscontainer-tagoccurence9-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2038-remove-colored-pieces-if-both-neighbors-are-the-same-color/README.md new file mode 100644 index 00000000..6499f289 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divwalmart-global-tech-divdiv-classcompanytagscontainer-tagoccurence9-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2038-remove-colored-pieces-if-both-neighbors-are-the-same-color/README.md @@ -0,0 +1,63 @@ +

Walmart Global Tech
9
2038. Remove Colored Pieces if Both Neighbors are the Same Color

Medium


There are n pieces arranged in a line, and each piece is colored either by 'A' or by 'B'. You are given a string colors of length n where colors[i] is the color of the ith piece.

+ +

Alice and Bob are playing a game where they take alternating turns removing pieces from the line. In this game, Alice moves first.

+ +
    +
  • Alice is only allowed to remove a piece colored 'A' if both its neighbors are also colored 'A'. She is not allowed to remove pieces that are colored 'B'.
  • +
  • Bob is only allowed to remove a piece colored 'B' if both its neighbors are also colored 'B'. He is not allowed to remove pieces that are colored 'A'.
  • +
  • Alice and Bob cannot remove pieces from the edge of the line.
  • +
  • If a player cannot make a move on their turn, that player loses and the other player wins.
  • +
+ +

Assuming Alice and Bob play optimally, return true if Alice wins, or return false if Bob wins.

+ +

 

+

Example 1:

+ +
Input: colors = "AAABABB"
+Output: true
+Explanation:
+AAABABB -> AABABB
+Alice moves first.
+She removes the second 'A' from the left since that is the only 'A' whose neighbors are both 'A'.
+
+Now it's Bob's turn.
+Bob cannot make a move on his turn since there are no 'B's whose neighbors are both 'B'.
+Thus, Alice wins, so return true.
+
+ +

Example 2:

+ +
Input: colors = "AA"
+Output: false
+Explanation:
+Alice has her turn first.
+There are only two 'A's and both are on the edge of the line, so she cannot move on her turn.
+Thus, Bob wins, so return false.
+
+ +

Example 3:

+ +
Input: colors = "ABBBBBBBAAA"
+Output: false
+Explanation:
+ABBBBBBBAAA -> ABBBBBBBAA
+Alice moves first.
+Her only option is to remove the second to last 'A' from the right.
+
+ABBBBBBBAA -> ABBBBBBAA
+Next is Bob's turn.
+He has many options for which 'B' piece to remove. He can pick any.
+
+On Alice's second turn, she has no more pieces that she can remove.
+Thus, Bob wins, so return false.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= colors.length <= 105
  • +
  • colors consists of only the letters 'A' and 'B'
  • +
+
\ No newline at end of file From 9c0e359903e8be6f4d4213a0f156aad46c5c4dd6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 2 Oct 2023 08:05:27 +0530 Subject: [PATCH 0064/1399] Time: 39 ms (15.83%), Space: 13.6 MB (17.42%) - LeetHub --- ...s-if-both-neighbors-are-the-same-color.cpp | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divwalmart-global-tech-divdiv-classcompanytagscontainer-tagoccurence9-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2038-remove-colored-pieces-if-both-neighbors-are-the-same-color/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divwalmart-global-tech-divdiv-classcompanytagscontainer-tagoccurence9-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2038-remove-colored-pieces-if-both-neighbors-are-the-same-color.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divwalmart-global-tech-divdiv-classcompanytagscontainer-tagoccurence9-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2038-remove-colored-pieces-if-both-neighbors-are-the-same-color/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divwalmart-global-tech-divdiv-classcompanytagscontainer-tagoccurence9-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2038-remove-colored-pieces-if-both-neighbors-are-the-same-color.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divwalmart-global-tech-divdiv-classcompanytagscontainer-tagoccurence9-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2038-remove-colored-pieces-if-both-neighbors-are-the-same-color/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divwalmart-global-tech-divdiv-classcompanytagscontainer-tagoccurence9-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2038-remove-colored-pieces-if-both-neighbors-are-the-same-color.cpp new file mode 100644 index 00000000..adbdb99b --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divwalmart-global-tech-divdiv-classcompanytagscontainer-tagoccurence9-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2038-remove-colored-pieces-if-both-neighbors-are-the-same-color/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divwalmart-global-tech-divdiv-classcompanytagscontainer-tagoccurence9-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2038-remove-colored-pieces-if-both-neighbors-are-the-same-color.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + bool winnerOfGame(string colors) { + + int n = colors.size(); + + int aliceMoves = 0, bobMoves = 0; + + for(int i = 1; i < n-1; ++i) + { + char prev = colors[i-1]; + char curr = colors[i]; + char next = colors[i+1]; + + if(prev == curr and curr == next) + { + if(curr == 'A') + ++aliceMoves; + else + ++bobMoves; + } + } + + return (aliceMoves >= bobMoves + 1 ? 1 : 0); + + } +}; \ No newline at end of file From 90b989cd0a8b03b647543ce52fb3f8dbc6954649 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 2 Oct 2023 08:06:59 +0530 Subject: [PATCH 0065/1399] Create README - LeetHub --- .../README.md | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 2038-remove-colored-pieces-if-both-neighbors-are-the-same-color/README.md diff --git a/2038-remove-colored-pieces-if-both-neighbors-are-the-same-color/README.md b/2038-remove-colored-pieces-if-both-neighbors-are-the-same-color/README.md new file mode 100644 index 00000000..a5034a96 --- /dev/null +++ b/2038-remove-colored-pieces-if-both-neighbors-are-the-same-color/README.md @@ -0,0 +1,63 @@ +

2038. Remove Colored Pieces if Both Neighbors are the Same Color

Medium


There are n pieces arranged in a line, and each piece is colored either by 'A' or by 'B'. You are given a string colors of length n where colors[i] is the color of the ith piece.

+ +

Alice and Bob are playing a game where they take alternating turns removing pieces from the line. In this game, Alice moves first.

+ +
    +
  • Alice is only allowed to remove a piece colored 'A' if both its neighbors are also colored 'A'. She is not allowed to remove pieces that are colored 'B'.
  • +
  • Bob is only allowed to remove a piece colored 'B' if both its neighbors are also colored 'B'. He is not allowed to remove pieces that are colored 'A'.
  • +
  • Alice and Bob cannot remove pieces from the edge of the line.
  • +
  • If a player cannot make a move on their turn, that player loses and the other player wins.
  • +
+ +

Assuming Alice and Bob play optimally, return true if Alice wins, or return false if Bob wins.

+ +

 

+

Example 1:

+ +
Input: colors = "AAABABB"
+Output: true
+Explanation:
+AAABABB -> AABABB
+Alice moves first.
+She removes the second 'A' from the left since that is the only 'A' whose neighbors are both 'A'.
+
+Now it's Bob's turn.
+Bob cannot make a move on his turn since there are no 'B's whose neighbors are both 'B'.
+Thus, Alice wins, so return true.
+
+ +

Example 2:

+ +
Input: colors = "AA"
+Output: false
+Explanation:
+Alice has her turn first.
+There are only two 'A's and both are on the edge of the line, so she cannot move on her turn.
+Thus, Bob wins, so return false.
+
+ +

Example 3:

+ +
Input: colors = "ABBBBBBBAAA"
+Output: false
+Explanation:
+ABBBBBBBAAA -> ABBBBBBBAA
+Alice moves first.
+Her only option is to remove the second to last 'A' from the right.
+
+ABBBBBBBAA -> ABBBBBBAA
+Next is Bob's turn.
+He has many options for which 'B' piece to remove. He can pick any.
+
+On Alice's second turn, she has no more pieces that she can remove.
+Thus, Bob wins, so return false.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= colors.length <= 105
  • +
  • colors consists of only the letters 'A' and 'B'
  • +
+
\ No newline at end of file From 4ad50643200379dbdc377fab035766730a65629e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 2 Oct 2023 08:06:59 +0530 Subject: [PATCH 0066/1399] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2038-remove-colored-pieces-if-both-neighbors-are-the-same-color/NOTES.md diff --git a/2038-remove-colored-pieces-if-both-neighbors-are-the-same-color/NOTES.md b/2038-remove-colored-pieces-if-both-neighbors-are-the-same-color/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2038-remove-colored-pieces-if-both-neighbors-are-the-same-color/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 19ed49b13ca8153426a2b32165876e9e5c5a4f9e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 2 Oct 2023 08:07:03 +0530 Subject: [PATCH 0067/1399] Time: 39 ms (15.83%), Space: 13.6 MB (17.42%) - LeetHub --- ...s-if-both-neighbors-are-the-same-color.cpp | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 2038-remove-colored-pieces-if-both-neighbors-are-the-same-color/2038-remove-colored-pieces-if-both-neighbors-are-the-same-color.cpp diff --git a/2038-remove-colored-pieces-if-both-neighbors-are-the-same-color/2038-remove-colored-pieces-if-both-neighbors-are-the-same-color.cpp b/2038-remove-colored-pieces-if-both-neighbors-are-the-same-color/2038-remove-colored-pieces-if-both-neighbors-are-the-same-color.cpp new file mode 100644 index 00000000..adbdb99b --- /dev/null +++ b/2038-remove-colored-pieces-if-both-neighbors-are-the-same-color/2038-remove-colored-pieces-if-both-neighbors-are-the-same-color.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + bool winnerOfGame(string colors) { + + int n = colors.size(); + + int aliceMoves = 0, bobMoves = 0; + + for(int i = 1; i < n-1; ++i) + { + char prev = colors[i-1]; + char curr = colors[i]; + char next = colors[i+1]; + + if(prev == curr and curr == next) + { + if(curr == 'A') + ++aliceMoves; + else + ++bobMoves; + } + } + + return (aliceMoves >= bobMoves + 1 ? 1 : 0); + + } +}; \ No newline at end of file From 6cdffa5d8cead0aeff60f23f3b28dd2ec79550da Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 3 Oct 2023 07:14:37 +0530 Subject: [PATCH 0068/1399] Create README - LeetHub --- .../README.md | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1512-number-of-good-pairs/README.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1512-number-of-good-pairs/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1512-number-of-good-pairs/README.md new file mode 100644 index 00000000..c5e5ff2c --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1512-number-of-good-pairs/README.md @@ -0,0 +1,33 @@ +

Microsoft
3
Amazon
3
Adobe
2
1512. Number of Good Pairs

Easy


Given an array of integers nums, return the number of good pairs.

+ +

A pair (i, j) is called good if nums[i] == nums[j] and i < j.

+ +

 

+

Example 1:

+ +
Input: nums = [1,2,3,1,1,3]
+Output: 4
+Explanation: There are 4 good pairs (0,3), (0,4), (3,4), (2,5) 0-indexed.
+
+ +

Example 2:

+ +
Input: nums = [1,1,1,1]
+Output: 6
+Explanation: Each pair in the array are good.
+
+ +

Example 3:

+ +
Input: nums = [1,2,3]
+Output: 0
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 100
  • +
  • 1 <= nums[i] <= 100
  • +
+
\ No newline at end of file From 757b953937b898881a41579c2b48565e63bbe590 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 3 Oct 2023 07:14:37 +0530 Subject: [PATCH 0069/1399] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1512-number-of-good-pairs/NOTES.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1512-number-of-good-pairs/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1512-number-of-good-pairs/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1512-number-of-good-pairs/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 935c154ed91a2ed6b301cc15365cb962e49824d9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 3 Oct 2023 07:14:42 +0530 Subject: [PATCH 0070/1399] Time: 0 ms (100.00%), Space: 7.6 MB (7.10%) - LeetHub --- ...v-div-div-div1512-number-of-good-pairs.cpp | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1512-number-of-good-pairs/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1512-number-of-good-pairs.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1512-number-of-good-pairs/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1512-number-of-good-pairs.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1512-number-of-good-pairs/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1512-number-of-good-pairs.cpp new file mode 100644 index 00000000..1b672930 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1512-number-of-good-pairs/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1512-number-of-good-pairs.cpp @@ -0,0 +1,21 @@ +class Solution { +public: + int numIdenticalPairs(vector& nums) { + + int n = nums.size(); + + unordered_map mp; + + for(auto& itr : nums) + ++mp[itr]; + + int count = 0; + + for(auto& [key, value] : mp) + { + count += (value * (value-1))/2; + } + + return count; + } +}; \ No newline at end of file From 44e3acd049ce5243dfb722f85779b008257014de Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 3 Oct 2023 07:16:28 +0530 Subject: [PATCH 0071/1399] Create README - LeetHub --- 1512-number-of-good-pairs/README.md | 33 +++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 1512-number-of-good-pairs/README.md diff --git a/1512-number-of-good-pairs/README.md b/1512-number-of-good-pairs/README.md new file mode 100644 index 00000000..8cdeb85f --- /dev/null +++ b/1512-number-of-good-pairs/README.md @@ -0,0 +1,33 @@ +

1512. Number of Good Pairs

Easy


Given an array of integers nums, return the number of good pairs.

+ +

A pair (i, j) is called good if nums[i] == nums[j] and i < j.

+ +

 

+

Example 1:

+ +
Input: nums = [1,2,3,1,1,3]
+Output: 4
+Explanation: There are 4 good pairs (0,3), (0,4), (3,4), (2,5) 0-indexed.
+
+ +

Example 2:

+ +
Input: nums = [1,1,1,1]
+Output: 6
+Explanation: Each pair in the array are good.
+
+ +

Example 3:

+ +
Input: nums = [1,2,3]
+Output: 0
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 100
  • +
  • 1 <= nums[i] <= 100
  • +
+
\ No newline at end of file From d6b58c65aff173bc58a8f42a0ef87962eed7ad23 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 3 Oct 2023 07:16:28 +0530 Subject: [PATCH 0072/1399] Attach NOTES - LeetHub --- 1512-number-of-good-pairs/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1512-number-of-good-pairs/NOTES.md diff --git a/1512-number-of-good-pairs/NOTES.md b/1512-number-of-good-pairs/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1512-number-of-good-pairs/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From b1a86746c478ab72665258339532714e758f4e04 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 3 Oct 2023 07:16:32 +0530 Subject: [PATCH 0073/1399] Time: 0 ms (100.00%), Space: 7.6 MB (7.10%) - LeetHub --- .../1512-number-of-good-pairs.cpp | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 1512-number-of-good-pairs/1512-number-of-good-pairs.cpp diff --git a/1512-number-of-good-pairs/1512-number-of-good-pairs.cpp b/1512-number-of-good-pairs/1512-number-of-good-pairs.cpp new file mode 100644 index 00000000..1b672930 --- /dev/null +++ b/1512-number-of-good-pairs/1512-number-of-good-pairs.cpp @@ -0,0 +1,21 @@ +class Solution { +public: + int numIdenticalPairs(vector& nums) { + + int n = nums.size(); + + unordered_map mp; + + for(auto& itr : nums) + ++mp[itr]; + + int count = 0; + + for(auto& [key, value] : mp) + { + count += (value * (value-1))/2; + } + + return count; + } +}; \ No newline at end of file From bc023cf8d5b7214d1e19664a52416c2388be4958 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 3 Oct 2023 07:17:04 +0530 Subject: [PATCH 0074/1399] Attach NOTES - LeetHub From f9ca3c989978c562ec73f4f44f5b03f2a31728db Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 3 Oct 2023 07:17:08 +0530 Subject: [PATCH 0075/1399] Time: 0 ms (100.00%), Space: 7.6 MB (7.10%) - LeetHub From 23f2aa5d1125f851aa9e622e7b61e790b44ff5cc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 3 Oct 2023 07:19:19 +0530 Subject: [PATCH 0076/1399] Attach NOTES - LeetHub From 8f9f75cb022ca50dfb85b99123e72ff4ecef2382 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 3 Oct 2023 07:58:25 +0530 Subject: [PATCH 0077/1399] Create README - LeetHub --- .../README.md | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Column name from a given column number - GFG/README.md diff --git a/Column name from a given column number - GFG/README.md b/Column name from a given column number - GFG/README.md new file mode 100644 index 00000000..eb24562d --- /dev/null +++ b/Column name from a given column number - GFG/README.md @@ -0,0 +1,21 @@ +# Column name from a given column number +## Medium +

Given a positive integer, return its corresponding column title as appear in an Excel sheet.
Excel columns has a pattern like A, B, C, … ,Z, AA, AB, AC,…. ,AZ, BA, BB, … ZZ, AAA, AAB ….. etc. In other words, column 1 is named as “A”, column 2 as “B”, column 27 as “AA” and so on.

+

Example 1:

+
Input:
+N = 28
+Output: AB
+Explanation: 1 to 26 are A to Z.
+Then, 27 is AA and 28 = AB.
+
+
+

Example 2:

+
Input: 
+N = 13
+Output: M
+Explanation: M is the 13th character of
+alphabet.
+
+

Your Task:
You don't need to read input or print anything. Your task is to complete the function colName() which takes the column number N as input and returns the column name represented as a string.

Expected Time Complexity: O(LogN).
Expected Auxiliary Space: O(1).

+

Constraints:
1 <= N <= 1018

+

 

\ No newline at end of file From aa523568799229fe9c7ea03376fbe27a122fa7ab Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 3 Oct 2023 07:58:26 +0530 Subject: [PATCH 0078/1399] Added solution - LeetHub --- ...column-name-from-a-given-column-number.cpp | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 Column name from a given column number - GFG/column-name-from-a-given-column-number.cpp diff --git a/Column name from a given column number - GFG/column-name-from-a-given-column-number.cpp b/Column name from a given column number - GFG/column-name-from-a-given-column-number.cpp new file mode 100644 index 00000000..c1086872 --- /dev/null +++ b/Column name from a given column number - GFG/column-name-from-a-given-column-number.cpp @@ -0,0 +1,41 @@ +//{ Driver Code Starts +#include +using namespace std; + +// } Driver Code Ends + + +class Solution{ + public: + string colName (long long int n) + { + // your code here + + string ans; + + while(n--) + { + int rem = n%26; + ans.push_back(rem + 'A'); + n /= 26; + } + + reverse(ans.begin(), ans.end()); + + return ans; + } +}; + +//{ Driver Code Starts. +int main() +{ + int t; cin >> t; + while (t--) + { + long long int n; cin >> n; + Solution ob; + cout << ob.colName (n) << '\n'; + } +} + +// } Driver Code Ends \ No newline at end of file From bb6cd9273c9cc167c7ef74fe76c8890c81bb2381 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 4 Oct 2023 07:21:04 +0530 Subject: [PATCH 0079/1399] Create README - LeetHub --- .../README.md | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence10-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divlinkedin-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divwalmart-global-tech-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div706-design-hashmap/README.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence10-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divlinkedin-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divwalmart-global-tech-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div706-design-hashmap/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence10-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divlinkedin-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divwalmart-global-tech-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div706-design-hashmap/README.md new file mode 100644 index 00000000..afd9c7ec --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence10-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divlinkedin-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divwalmart-global-tech-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div706-design-hashmap/README.md @@ -0,0 +1,40 @@ +

Amazon
10
Microsoft
6
LinkedIn
5
Oracle
4
Salesforce
3
Apple
3
Goldman Sachs
3
Walmart Global Tech
3
706. Design HashMap

Easy


Design a HashMap without using any built-in hash table libraries.

+ +

Implement the MyHashMap class:

+ +
    +
  • MyHashMap() initializes the object with an empty map.
  • +
  • void put(int key, int value) inserts a (key, value) pair into the HashMap. If the key already exists in the map, update the corresponding value.
  • +
  • int get(int key) returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key.
  • +
  • void remove(key) removes the key and its corresponding value if the map contains the mapping for the key.
  • +
+ +

 

+

Example 1:

+ +
Input
+["MyHashMap", "put", "put", "get", "get", "put", "get", "remove", "get"]
+[[], [1, 1], [2, 2], [1], [3], [2, 1], [2], [2], [2]]
+Output
+[null, null, null, 1, -1, null, 1, null, -1]
+
+Explanation
+MyHashMap myHashMap = new MyHashMap();
+myHashMap.put(1, 1); // The map is now [[1,1]]
+myHashMap.put(2, 2); // The map is now [[1,1], [2,2]]
+myHashMap.get(1);    // return 1, The map is now [[1,1], [2,2]]
+myHashMap.get(3);    // return -1 (i.e., not found), The map is now [[1,1], [2,2]]
+myHashMap.put(2, 1); // The map is now [[1,1], [2,1]] (i.e., update the existing value)
+myHashMap.get(2);    // return 1, The map is now [[1,1], [2,1]]
+myHashMap.remove(2); // remove the mapping for 2, The map is now [[1,1]]
+myHashMap.get(2);    // return -1 (i.e., not found), The map is now [[1,1]]
+
+ +

 

+

Constraints:

+ +
    +
  • 0 <= key, value <= 106
  • +
  • At most 104 calls will be made to put, get, and remove.
  • +
+
\ No newline at end of file From e8a5854b4a446a90d61b8d09b0c4743a795125fc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 4 Oct 2023 07:21:04 +0530 Subject: [PATCH 0080/1399] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence10-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divlinkedin-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divwalmart-global-tech-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div706-design-hashmap/NOTES.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence10-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divlinkedin-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divwalmart-global-tech-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div706-design-hashmap/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence10-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divlinkedin-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divwalmart-global-tech-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div706-design-hashmap/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence10-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divlinkedin-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divwalmart-global-tech-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div706-design-hashmap/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 2edaa2067206ad85e4c1559cf2e6d8d12d3adccb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 4 Oct 2023 07:21:08 +0530 Subject: [PATCH 0081/1399] Time: 106 ms (70.74%), Space: 208.4 MB (14.05%) - LeetHub --- ...-svg-div-div-div-div706-design-hashmap.cpp | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence10-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divlinkedin-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divwalmart-global-tech-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div706-design-hashmap/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence10-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divlinkedin-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divwalmart-global-tech-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div706-design-hashmap.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence10-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divlinkedin-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divwalmart-global-tech-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div706-design-hashmap/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence10-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divlinkedin-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divwalmart-global-tech-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div706-design-hashmap.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence10-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divlinkedin-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divwalmart-global-tech-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div706-design-hashmap/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence10-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divlinkedin-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divwalmart-global-tech-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div706-design-hashmap.cpp new file mode 100644 index 00000000..81cf155a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence10-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divlinkedin-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divwalmart-global-tech-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div706-design-hashmap/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence10-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divlinkedin-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divwalmart-global-tech-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div706-design-hashmap.cpp @@ -0,0 +1,29 @@ +class MyHashMap { +public: + + int arr[1000001]; + + MyHashMap() { + fill(arr, arr+1000001, -1); + } + + void put(int key, int value) { + arr[key] = value; + } + + int get(int key) { + return arr[key]; + } + + void remove(int key) { + arr[key] = -1; + } +}; + +/** + * Your MyHashMap object will be instantiated and called as such: + * MyHashMap* obj = new MyHashMap(); + * obj->put(key,value); + * int param_2 = obj->get(key); + * obj->remove(key); + */ \ No newline at end of file From acc613b6f63bd28ee2178c822717d0c2d4fd3f95 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 4 Oct 2023 07:23:11 +0530 Subject: [PATCH 0082/1399] Create README - LeetHub --- 0706-design-hashmap/README.md | 40 +++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 0706-design-hashmap/README.md diff --git a/0706-design-hashmap/README.md b/0706-design-hashmap/README.md new file mode 100644 index 00000000..830381e2 --- /dev/null +++ b/0706-design-hashmap/README.md @@ -0,0 +1,40 @@ +

706. Design HashMap

Easy


Design a HashMap without using any built-in hash table libraries.

+ +

Implement the MyHashMap class:

+ +
    +
  • MyHashMap() initializes the object with an empty map.
  • +
  • void put(int key, int value) inserts a (key, value) pair into the HashMap. If the key already exists in the map, update the corresponding value.
  • +
  • int get(int key) returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key.
  • +
  • void remove(key) removes the key and its corresponding value if the map contains the mapping for the key.
  • +
+ +

 

+

Example 1:

+ +
Input
+["MyHashMap", "put", "put", "get", "get", "put", "get", "remove", "get"]
+[[], [1, 1], [2, 2], [1], [3], [2, 1], [2], [2], [2]]
+Output
+[null, null, null, 1, -1, null, 1, null, -1]
+
+Explanation
+MyHashMap myHashMap = new MyHashMap();
+myHashMap.put(1, 1); // The map is now [[1,1]]
+myHashMap.put(2, 2); // The map is now [[1,1], [2,2]]
+myHashMap.get(1);    // return 1, The map is now [[1,1], [2,2]]
+myHashMap.get(3);    // return -1 (i.e., not found), The map is now [[1,1], [2,2]]
+myHashMap.put(2, 1); // The map is now [[1,1], [2,1]] (i.e., update the existing value)
+myHashMap.get(2);    // return 1, The map is now [[1,1], [2,1]]
+myHashMap.remove(2); // remove the mapping for 2, The map is now [[1,1]]
+myHashMap.get(2);    // return -1 (i.e., not found), The map is now [[1,1]]
+
+ +

 

+

Constraints:

+ +
    +
  • 0 <= key, value <= 106
  • +
  • At most 104 calls will be made to put, get, and remove.
  • +
+
\ No newline at end of file From 93d2306f8c0410b942ca0d08a63e29d3f0233619 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 4 Oct 2023 20:58:15 +0530 Subject: [PATCH 0083/1399] Create README - LeetHub From 1046326d1004ba228d2c3a7d19ad67ecb24820cf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 4 Oct 2023 20:58:19 +0530 Subject: [PATCH 0084/1399] Time: 105 ms (72.93%), Space: 62.2 MB (48.49%) - LeetHub --- 0706-design-hashmap/0706-design-hashmap.cpp | 63 +++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 0706-design-hashmap/0706-design-hashmap.cpp diff --git a/0706-design-hashmap/0706-design-hashmap.cpp b/0706-design-hashmap/0706-design-hashmap.cpp new file mode 100644 index 00000000..d991530d --- /dev/null +++ b/0706-design-hashmap/0706-design-hashmap.cpp @@ -0,0 +1,63 @@ +class MyHashMap { +public: + + vector> > hash; + int size = 10001; + + MyHashMap() { + hash.resize(size); + } + + void put(int key, int value) { + int idx = key % size; + + for(auto& [k, val] : hash[idx]) + { + if(k == key) + { + val = value; + return; + } + } + + hash[idx].push_back({key, value}); + } + + int get(int key) { + + int idx = key % size; + + if(hash[idx].empty()) + return -1; + + for(auto& [k, val] : hash[idx]) + { + if(k == key) + return val; + } + + return -1; + } + + void remove(int key) { + + int idx = key % size; + + for(auto itr = hash[idx].begin(); itr != hash[idx].end(); ++itr) + { + if(itr->first == key) + { + hash[idx].erase(itr); + return; + } + } + } +}; + +/** + * Your MyHashMap object will be instantiated and called as such: + * MyHashMap* obj = new MyHashMap(); + * obj->put(key,value); + * int param_2 = obj->get(key); + * obj->remove(key); + */ \ No newline at end of file From f5487e16a2c2b47a756d93e7feda8b035f9b6dcf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 4 Oct 2023 21:42:23 +0530 Subject: [PATCH 0085/1399] Attach NOTES - LeetHub From 2491298c653e764c8398c963c99fd8415ddcae79 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 4 Oct 2023 21:42:26 +0530 Subject: [PATCH 0086/1399] Time: 91 ms (36.72%), Space: 50.6 MB (31.53%) - LeetHub --- 0705-design-hashset/0705-design-hashset.cpp | 43 ++++++++++++++++++--- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/0705-design-hashset/0705-design-hashset.cpp b/0705-design-hashset/0705-design-hashset.cpp index c76dbb4f..f8bc114b 100644 --- a/0705-design-hashset/0705-design-hashset.cpp +++ b/0705-design-hashset/0705-design-hashset.cpp @@ -1,22 +1,55 @@ class MyHashSet { public: - unordered_map mp; + vector> hash; + int size = 10001; MyHashSet() { - + hash.resize(size); } void add(int key) { - ++mp[key]; + + int idx = key % size; + + for(auto& itr : hash[idx]) + { + if(itr == key) + { + return; + } + } + + hash[idx].push_back(key); } void remove(int key) { - mp.erase(key); + + int idx = key % size; + + for(auto itr = hash[idx].begin(); itr != hash[idx].end(); ++itr) + { + if(*itr == key) + { + hash[idx].erase(itr); + return; + } + } + } bool contains(int key) { - return mp[key] > 0; + + int idx = key % size; + + for(auto& itr : hash[idx]) + { + if(itr == key) + { + return true; + } + } + return false; } }; From c2b724eaf314286b98ef1baed52a54130a5933af Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 5 Oct 2023 07:25:12 +0530 Subject: [PATCH 0087/1399] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div229-majority-element-ii/NOTES.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div229-majority-element-ii/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div229-majority-element-ii/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div229-majority-element-ii/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 669b993117aac15423c15b1c503907a3fb63604b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 5 Oct 2023 07:25:15 +0530 Subject: [PATCH 0088/1399] Time: 7 ms (87.90%), Space: 16.3 MB (18.73%) - LeetHub --- ...div-div-div-div229-majority-element-ii.cpp | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div229-majority-element-ii/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div229-majority-element-ii.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div229-majority-element-ii/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div229-majority-element-ii.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div229-majority-element-ii/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div229-majority-element-ii.cpp new file mode 100644 index 00000000..cb2c75f3 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div229-majority-element-ii/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div229-majority-element-ii.cpp @@ -0,0 +1,57 @@ +class Solution { +public: + vector majorityElement(vector& nums) { + + int n = nums.size(); + + int count1 = 0, count2 = 0; + + int num1 = INT_MAX, num2 = INT_MAX; + + for(auto& itr : nums) + { + if(itr == num1) + { + ++count1; + } + else if(itr == num2) + { + ++count2; + } + else if(count1 == 0) + { + count1 = 1; + num1 = itr; + } + else if(count2 == 0) + { + count2 = 1; + num2 = itr; + } + else + { + --count1, --count2; + } + } + + count1 = count2 = 0; + + for(auto& itr : nums) + { + if(itr == num1) + ++count1; + else if(itr == num2) + ++count2; + } + + vector ans; + + if(count1 > n/3) + ans.push_back(num1); + if(count2 > n/3) + ans.push_back(num2); + + return ans; + + } +}; \ No newline at end of file From 740b8e897b20c46696dee8ee1910bced6f7bab5a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 6 Oct 2023 19:09:00 +0530 Subject: [PATCH 0089/1399] Create README - LeetHub --- .../README.md | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div343-integer-break/README.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div343-integer-break/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div343-integer-break/README.md new file mode 100644 index 00000000..929907f0 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div343-integer-break/README.md @@ -0,0 +1,26 @@ +

Facebook
3
343. Integer Break

Medium


Given an integer n, break it into the sum of k positive integers, where k >= 2, and maximize the product of those integers.

+ +

Return the maximum product you can get.

+ +

 

+

Example 1:

+ +
Input: n = 2
+Output: 1
+Explanation: 2 = 1 + 1, 1 × 1 = 1.
+
+ +

Example 2:

+ +
Input: n = 10
+Output: 36
+Explanation: 10 = 3 + 3 + 4, 3 × 3 × 4 = 36.
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= n <= 58
  • +
+
\ No newline at end of file From ab2873180b2fe1fe3e5cc202cb00efb157a4daba Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 6 Oct 2023 19:09:00 +0530 Subject: [PATCH 0090/1399] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div343-integer-break/NOTES.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div343-integer-break/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div343-integer-break/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div343-integer-break/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 852e7cda1708c8733eb8f5fdf3243e8dace60d13 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 6 Oct 2023 19:09:03 +0530 Subject: [PATCH 0091/1399] Time: 42 ms (5.03%), Space: 6.4 MB (36.38%) - LeetHub --- ...t-svg-div-div-div-div343-integer-break.cpp | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div343-integer-break/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div343-integer-break.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div343-integer-break/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div343-integer-break.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div343-integer-break/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div343-integer-break.cpp new file mode 100644 index 00000000..75c189d1 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div343-integer-break/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div343-integer-break.cpp @@ -0,0 +1,30 @@ +class Solution { +public: + + int ans; + + void helper(int idx, int prod, int n, int k) + { + if(n == 0 and k >= 2) + { + ans = max(ans, prod); + return; + } + + if(n-idx >= 0) + { + helper(idx, prod*idx, n-idx, k+1); + helper(idx+1, prod, n, k); + } + + } + + int integerBreak(int n) { + + ans = 1; + + helper(1, 1, n, 0); + + return ans; + } +}; \ No newline at end of file From 35659d29e546ea995c29474133070734bdc22b06 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 6 Oct 2023 19:11:34 +0530 Subject: [PATCH 0092/1399] Create README - LeetHub --- 0343-integer-break/README.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 0343-integer-break/README.md diff --git a/0343-integer-break/README.md b/0343-integer-break/README.md new file mode 100644 index 00000000..eeb0e4fa --- /dev/null +++ b/0343-integer-break/README.md @@ -0,0 +1,26 @@ +

343. Integer Break

Medium


Given an integer n, break it into the sum of k positive integers, where k >= 2, and maximize the product of those integers.

+ +

Return the maximum product you can get.

+ +

 

+

Example 1:

+ +
Input: n = 2
+Output: 1
+Explanation: 2 = 1 + 1, 1 × 1 = 1.
+
+ +

Example 2:

+ +
Input: n = 10
+Output: 36
+Explanation: 10 = 3 + 3 + 4, 3 × 3 × 4 = 36.
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= n <= 58
  • +
+
\ No newline at end of file From 902b7e9fba6a6f7b008721e8989a16b303629d3c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 6 Oct 2023 19:11:38 +0530 Subject: [PATCH 0093/1399] Time: 42 ms (5.03%), Space: 6.4 MB (36.38%) - LeetHub --- 0343-integer-break/0343-integer-break.cpp | 30 +++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 0343-integer-break/0343-integer-break.cpp diff --git a/0343-integer-break/0343-integer-break.cpp b/0343-integer-break/0343-integer-break.cpp new file mode 100644 index 00000000..75c189d1 --- /dev/null +++ b/0343-integer-break/0343-integer-break.cpp @@ -0,0 +1,30 @@ +class Solution { +public: + + int ans; + + void helper(int idx, int prod, int n, int k) + { + if(n == 0 and k >= 2) + { + ans = max(ans, prod); + return; + } + + if(n-idx >= 0) + { + helper(idx, prod*idx, n-idx, k+1); + helper(idx+1, prod, n, k); + } + + } + + int integerBreak(int n) { + + ans = 1; + + helper(1, 1, n, 0); + + return ans; + } +}; \ No newline at end of file From f15a0b8349aefc9a0030ee06a807dc9740b0f17f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 6 Oct 2023 19:12:14 +0530 Subject: [PATCH 0094/1399] Attach NOTES - LeetHub --- 0343-integer-break/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0343-integer-break/NOTES.md diff --git a/0343-integer-break/NOTES.md b/0343-integer-break/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0343-integer-break/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From e5488947d260f73becc17d7bb61ea4f0df581401 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 6 Oct 2023 19:12:18 +0530 Subject: [PATCH 0095/1399] Time: 42 ms (5.03%), Space: 6.4 MB (36.38%) - LeetHub From b321a6f4266441eab73757ab5f8cbcc63a1f02f1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 6 Oct 2023 19:16:56 +0530 Subject: [PATCH 0096/1399] Attach NOTES - LeetHub From 36c2d824b46b16196e07c2a8bfc79e6d371ee270 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 6 Oct 2023 19:16:59 +0530 Subject: [PATCH 0097/1399] Time: 42 ms (5.03%), Space: 6.4 MB (36.38%) - LeetHub From fe12a856e97a6f18587d0cbd9e5a05a1fdff0e27 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 6 Oct 2023 19:39:46 +0530 Subject: [PATCH 0098/1399] Attach NOTES - LeetHub From d8b38fcc15af727ce66f743b4682909f748028fc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 6 Oct 2023 19:39:50 +0530 Subject: [PATCH 0099/1399] Time: 1487 ms (5.03%), Space: 6.4 MB (36.38%) - LeetHub --- ...t-svg-div-div-div-div343-integer-break.cpp | 30 ++++++++----------- 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div343-integer-break/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div343-integer-break.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div343-integer-break/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div343-integer-break.cpp index 75c189d1..7a82da2d 100644 --- a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div343-integer-break/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div343-integer-break.cpp +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div343-integer-break/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div343-integer-break.cpp @@ -1,30 +1,24 @@ class Solution { public: - int ans; - - void helper(int idx, int prod, int n, int k) + int helper(int idx, int sum, int n) { - if(n == 0 and k >= 2) - { - ans = max(ans, prod); - return; - } + if(sum == 0) + return 1; + + if(sum < 0 or idx >= n) + return 0; + + int res = helper(idx+1, sum, n); + + res = max(res, idx * helper(idx, sum-idx, n)); - if(n-idx >= 0) - { - helper(idx, prod*idx, n-idx, k+1); - helper(idx+1, prod, n, k); - } - + return res; } int integerBreak(int n) { - - ans = 1; - helper(1, 1, n, 0); + return helper(1, n, n); - return ans; } }; \ No newline at end of file From cd67c6ca3d4b64339446a2db8cb4a655411da2dc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 6 Oct 2023 19:42:40 +0530 Subject: [PATCH 0100/1399] Attach NOTES - LeetHub From 7b808845d704f00c23cdf302fc3efd715aef144a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 6 Oct 2023 19:42:44 +0530 Subject: [PATCH 0101/1399] Time: 0 ms (100.00%), Space: 7.2 MB (6.83%) - LeetHub --- 0343-integer-break/0343-integer-break.cpp | 37 +++++++++++------------ 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/0343-integer-break/0343-integer-break.cpp b/0343-integer-break/0343-integer-break.cpp index 75c189d1..23c42f94 100644 --- a/0343-integer-break/0343-integer-break.cpp +++ b/0343-integer-break/0343-integer-break.cpp @@ -1,30 +1,29 @@ class Solution { public: - int ans; - - void helper(int idx, int prod, int n, int k) + int helper(int idx, int sum, int n, vector>& dp) { - if(n == 0 and k >= 2) - { - ans = max(ans, prod); - return; - } - - if(n-idx >= 0) - { - helper(idx, prod*idx, n-idx, k+1); - helper(idx+1, prod, n, k); - } - + if(sum == 0) + return 1; + + if(sum < 0 or idx >= n) + return 0; + + if(dp[idx][sum] != -1) + return dp[idx][sum]; + + int res = helper(idx+1, sum, n, dp); + + dp[idx][sum] = res = max(res, idx * helper(idx, sum-idx, n, dp)); + + return res; } int integerBreak(int n) { - - ans = 1; - helper(1, 1, n, 0); + vector> dp(n+1, vector(n+1, -1)); + + return helper(1, n, n, dp); - return ans; } }; \ No newline at end of file From 72f0545d5f15d78a9ff66cac5eebcc3ae975789b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 6 Oct 2023 19:44:48 +0530 Subject: [PATCH 0102/1399] Attach NOTES - LeetHub From bc3297636d7cddc36a7d9f11f32324b9440a7907 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 6 Oct 2023 19:44:52 +0530 Subject: [PATCH 0103/1399] Time: 0 ms (100.00%), Space: 7.2 MB (6.83%) - LeetHub From d3e0d64526a8cf80dced8c3286bbbcc487ac3211 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 6 Oct 2023 20:19:30 +0530 Subject: [PATCH 0104/1399] Attach NOTES - LeetHub From daff4e8ab7cc4dc16e84fd60c66d20383867886b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 6 Oct 2023 20:19:34 +0530 Subject: [PATCH 0105/1399] Time: 0 ms (100.00%), Space: 6.5 MB (29.03%) - LeetHub --- ...t-svg-div-div-div-div343-integer-break.cpp | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div343-integer-break/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div343-integer-break.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div343-integer-break/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div343-integer-break.cpp index 7a82da2d..03f2b022 100644 --- a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div343-integer-break/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div343-integer-break.cpp +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div343-integer-break/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div343-integer-break.cpp @@ -1,24 +1,29 @@ class Solution { public: - int helper(int idx, int sum, int n) + int helper(int n, vector& dp) { - if(sum == 0) - return 1; + if(n == 1) + return 1; - if(sum < 0 or idx >= n) - return 0; + if(dp[n] != -1) + return dp[n]; - int res = helper(idx+1, sum, n); + int res = 1; - res = max(res, idx * helper(idx, sum-idx, n)); + for(int i = 1; i <= n-1; ++i) + { + int prod = i * max(n - i, helper(n-i, dp)); + res = max(res, prod); + } - return res; + return dp[n] = res; } int integerBreak(int n) { - return helper(1, n, n); + vector dp(n+1, -1); + return helper(n, dp); } }; \ No newline at end of file From 89ce18450652c4f6c14387edf2ab9c13befc62b7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 6 Oct 2023 21:11:03 +0530 Subject: [PATCH 0106/1399] Create README - LeetHub --- .../README.md | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Reverse alternate nodes in Link List - GFG/README.md diff --git a/Reverse alternate nodes in Link List - GFG/README.md b/Reverse alternate nodes in Link List - GFG/README.md new file mode 100644 index 00000000..9ba82f5b --- /dev/null +++ b/Reverse alternate nodes in Link List - GFG/README.md @@ -0,0 +1,23 @@ +# Reverse alternate nodes in Link List +## Medium +

Given a linked list, you have to perform the following task:

+
    +
  1. Extract the alternative nodes starting from second node.
  2. +
  3. Reverse the extracted list.
  4. +
  5. Append the extracted list at the end of the original list.
  6. +
+

Note: Try to solve the problem without using any extra memory.

+

Example 1:

+
Input:
+LinkedList = 10->4->9->1->3->5->9->4
+Output: 
10 9 3 9 4 5 1 4 +Explanation:
Alternative nodes in the given linked list are 4,1,5,4. Reversing the alternative nodes from the given list, and then appending them to the end of the list results in a list 10->9->3->9->4->5->1->4. +
+

Example 2:

+
Input:
+LinkedList = 1->2->3->4->5
+Output: 
1 3 5 4 2  +Explanation:
Alternative nodes in the given linked list are 2 and 4. Reversing the alternative nodes from the given list, and then appending them to the end of the list results in a list 1->3->5->4->2.
+

Your Task:
You don't have to read input or print anything. Your task is to complete the function rearrange() which takes the head of the linked list as input and rearranges the list as required.

+

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)

+

Constraints:
1 <= N <= 105
0 <= Node_value <= 109

\ No newline at end of file From 983af858b8b32b43374950dc548418ce2415d0c5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 6 Oct 2023 21:11:03 +0530 Subject: [PATCH 0107/1399] Added solution - LeetHub --- .../reverse-alternate-nodes-in-link-list.cpp | 150 ++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 Reverse alternate nodes in Link List - GFG/reverse-alternate-nodes-in-link-list.cpp diff --git a/Reverse alternate nodes in Link List - GFG/reverse-alternate-nodes-in-link-list.cpp b/Reverse alternate nodes in Link List - GFG/reverse-alternate-nodes-in-link-list.cpp new file mode 100644 index 00000000..13df7d24 --- /dev/null +++ b/Reverse alternate nodes in Link List - GFG/reverse-alternate-nodes-in-link-list.cpp @@ -0,0 +1,150 @@ +//{ Driver Code Starts +#include +#include +#include +using namespace std; +/* A linked list node */ + + +struct Node +{ + int data; + struct Node *next; + + Node(int x){ + data = x; + next = NULL; + } + +}; + +struct Node *start = NULL; + +/* Function to print nodes in a given linked list */ +void printList(struct Node *node) +{ + while (node != NULL) + { + printf("%d ", node->data); + node = node->next; + } + printf("\n"); + +} + +void insert() +{ + int n,value; + cin>>n; + struct Node *temp; + for(int i=0;i>value; + if(i==0) + { + start = new Node(value); + temp = start; + continue; + } + else + { + temp->next = new Node(value); + temp = temp->next; + } + } +} + + +// } Driver Code Ends +/* + reverse alternate nodes and append at the end + The input list will have at least one element + Node is defined as + struct Node + { + int data; + struct Node *next; + + Node(int x){ + data = x; + next = NULL; + } + + }; + +*/ +class Solution +{ + public: + + Node* reverse(struct Node* head) + { + Node* prev = nullptr; + + while(head) + { + Node* nextNode = head->next; + head->next = prev; + prev = head; + head = nextNode; + } + + return prev; + } + + void rearrange(struct Node *odd) + { + //add code here + + Node* dummy = new Node(0), *ptr = dummy; + + Node* temp = odd, *prev = nullptr; + + bool ok = false; + + while(temp) + { + if(ok) + { + prev->next = (temp->next ? temp->next : nullptr); + ptr ->next = temp; + ptr = ptr->next; + } + + if(!ok) + prev = temp; + + temp = temp->next; + + ok ^= 1; + } + + + if(ptr->next) + ptr->next = nullptr; + + Node* rev = reverse(dummy->next); + + + prev->next = rev; + + } +}; + + + +//{ Driver Code Starts. +int main() +{ + int t; + cin>>t; + while (t--) { + insert(); + Solution ob; + ob.rearrange(start); + printList(start); + } + return 0; +} + +// } Driver Code Ends \ No newline at end of file From f671fbf682d7d0f655075aff60edc68caabc2c2d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 7 Oct 2023 07:29:32 +0530 Subject: [PATCH 0108/1399] Create README - LeetHub --- .../README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Pairwise swap elements of a linked list - GFG/README.md diff --git a/Pairwise swap elements of a linked list - GFG/README.md b/Pairwise swap elements of a linked list - GFG/README.md new file mode 100644 index 00000000..168956ee --- /dev/null +++ b/Pairwise swap elements of a linked list - GFG/README.md @@ -0,0 +1,17 @@ +# Pairwise swap elements of a linked list +## Easy +

Given a singly linked list of size N. The task is to swap elements in the linked list pairwise.
For example, if the input list is 1 2 3 4, the resulting list after swaps will be 2 1 4 3.
Note: You need to swap the nodes, not only the data. If only data is swapped then driver will print -1.

+

Example 1:

+
Input:
+LinkedList: 1->2->2->4->5->6->7->8
+Output: 
2 1 4 2 6 5 8 7 +Explanation:
After swapping each pair considering (1,2), (2, 4), (5, 6).. so on as pairs, we get 2, 1, 4, 2, 6, 5, 8, 7 as a new linked list.
+
+

Example 2:

+
Input:
+LinkedList: 1->3->4->7->9->10->1
+Output: 
3 1 7 4 10 9 1 +Explanation:
After swapping each pair considering (1,3), (4, 7), (9, 10).. so on as pairs, we get 3, 1, 7, 4, 10, 9, 1 as a new linked list.
+

Your Task:
The task is to complete the function pairWiseSwap() which takes the head node as the only argument and returns the head of modified linked list.

+

Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).

+

Constraints:
1 ≤ N ≤ 105

\ No newline at end of file From 100b6c164f27a38b24cdb0e3ed8a548829f12308 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 7 Oct 2023 07:29:32 +0530 Subject: [PATCH 0109/1399] Added solution - LeetHub --- ...airwise-swap-elements-of-a-linked-list.cpp | 135 ++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 Pairwise swap elements of a linked list - GFG/pairwise-swap-elements-of-a-linked-list.cpp diff --git a/Pairwise swap elements of a linked list - GFG/pairwise-swap-elements-of-a-linked-list.cpp b/Pairwise swap elements of a linked list - GFG/pairwise-swap-elements-of-a-linked-list.cpp new file mode 100644 index 00000000..dc962e68 --- /dev/null +++ b/Pairwise swap elements of a linked list - GFG/pairwise-swap-elements-of-a-linked-list.cpp @@ -0,0 +1,135 @@ +//{ Driver Code Starts +#include +using namespace std; + +struct Node +{ + int data; + struct Node* next; + + Node(int x){ + data = x; + next = NULL; + } +}; + + +// } Driver Code Ends +/* + Pairwise swap a linked list + The input list will have at least one element + node is defined as + +struct Node +{ + int data; + struct Node* next; + + Node(int x){ + data = x; + next = NULL; + } + +}*head; +*/ +class Solution +{ + public: + Node* pairWiseSwap(struct Node* head) + { + // The task is to complete this method + + if(!head or !head->next) + return head; + + Node* prev = nullptr, *temp = head, *prevPrev = nullptr; + Node* newHead = nullptr; + + bool ok = false; + + while(temp) + { + if(ok) + { + Node* nextNode = (temp->next ? temp->next : nullptr); + temp->next = prev; + prev->next = nextNode; + + if(prevPrev) + { + prevPrev->next = temp; + } + prevPrev = prev; + + if(!newHead) + newHead = temp; + + temp = prev; + } + + if(!ok) + prev = temp; + + temp = temp->next; + + ok ^= 1; + } + + return newHead; + } +}; + +//{ Driver Code Starts. + +void printList(Node* node) +{ + while (node != NULL) { + cout << node->data <<" "; + node = node->next; + } + cout<<"\n"; +} + +int main() +{ + int t; + cin>>t; + while(t--) + { + int n; + cin>>n; + + int data; + cin>>data; + struct Node *head = new Node(data); + struct Node *tail = head; + map mp; + mp[head] = head->data; + for (int i = 0; i>data; + tail->next = new Node(data); + tail = tail->next; + mp[tail] = tail->data; + } + struct Node *failure = new Node(-1); + Solution ob; + head = ob.pairWiseSwap(head); + int flag = 0; + struct Node *temp = head; + while(temp){ + if(mp[temp] != temp->data){ + flag = 1; + break; + } + temp = temp->next; + } + if(flag) + printList(failure); + else + printList(head); + } + return 0; +} + +// } Driver Code Ends \ No newline at end of file From 0d09819ff696d37964ae37cd54707abc358ac9b3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 7 Oct 2023 22:23:41 +0530 Subject: [PATCH 0110/1399] Create README - LeetHub --- .../README.md | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1420-build-array-where-you-can-find-the-maximum-exactly-k-comparisons/README.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1420-build-array-where-you-can-find-the-maximum-exactly-k-comparisons/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1420-build-array-where-you-can-find-the-maximum-exactly-k-comparisons/README.md new file mode 100644 index 00000000..b71eddf5 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1420-build-array-where-you-can-find-the-maximum-exactly-k-comparisons/README.md @@ -0,0 +1,43 @@ +

No companies found for this problem
1420. Build Array Where You Can Find The Maximum Exactly K Comparisons

Hard


You are given three integers n, m and k. Consider the following algorithm to find the maximum element of an array of positive integers:

+ +

You should build the array arr which has the following properties:

+ +
    +
  • arr has exactly n integers.
  • +
  • 1 <= arr[i] <= m where (0 <= i < n).
  • +
  • After applying the mentioned algorithm to arr, the value search_cost is equal to k.
  • +
+ +

Return the number of ways to build the array arr under the mentioned conditions. As the answer may grow large, the answer must be computed modulo 109 + 7.

+ +

 

+

Example 1:

+ +
Input: n = 2, m = 3, k = 1
+Output: 6
+Explanation: The possible arrays are [1, 1], [2, 1], [2, 2], [3, 1], [3, 2] [3, 3]
+
+ +

Example 2:

+ +
Input: n = 5, m = 2, k = 3
+Output: 0
+Explanation: There are no possible arrays that satisify the mentioned conditions.
+
+ +

Example 3:

+ +
Input: n = 9, m = 1, k = 1
+Output: 1
+Explanation: The only possible array is [1, 1, 1, 1, 1, 1, 1, 1, 1]
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= 50
  • +
  • 1 <= m <= 100
  • +
  • 0 <= k <= n
  • +
+
\ No newline at end of file From 6e4fc14321273a594bafceeabeacacd10f407f9f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 7 Oct 2023 22:23:41 +0530 Subject: [PATCH 0111/1399] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1420-build-array-where-you-can-find-the-maximum-exactly-k-comparisons/NOTES.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1420-build-array-where-you-can-find-the-maximum-exactly-k-comparisons/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1420-build-array-where-you-can-find-the-maximum-exactly-k-comparisons/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1420-build-array-where-you-can-find-the-maximum-exactly-k-comparisons/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 6a52e00bebbbd2648acc0082a33a215eae5042ca Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 7 Oct 2023 22:23:45 +0530 Subject: [PATCH 0112/1399] Time: 0 ms (100.00%), Space: 7.6 MB (61.75%) - LeetHub --- ...find-the-maximum-exactly-k-comparisons.cpp | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1420-build-array-where-you-can-find-the-maximum-exactly-k-comparisons/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1420-build-array-where-you-can-find-the-maximum-exactly-k-comparisons.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1420-build-array-where-you-can-find-the-maximum-exactly-k-comparisons/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1420-build-array-where-you-can-find-the-maximum-exactly-k-comparisons.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1420-build-array-where-you-can-find-the-maximum-exactly-k-comparisons/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1420-build-array-where-you-can-find-the-maximum-exactly-k-comparisons.cpp new file mode 100644 index 00000000..70edfa32 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1420-build-array-where-you-can-find-the-maximum-exactly-k-comparisons/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1420-build-array-where-you-can-find-the-maximum-exactly-k-comparisons.cpp @@ -0,0 +1,21 @@ +class Solution { +public: + int numOfArrays(int n, int m, int h) { + int64_t dp[n][h+1][m+1]; + memset(dp, 0, (n)*(h+1)*(m+1)*sizeof(int64_t)); + for(int i = 0; i < n; ++i) { + for(int j = 1; j <= h; ++j) { + for(int k = j; k <= m; ++k) { + dp[i][j][k] = dp[i][j][k-1]; + if(i == 0) { + dp[i][j][k] += j == 1 ? 1 : 0; + continue; + } + dp[i][j][k] = (dp[i][j][k] + dp[i-1][j-1][k-1] + (dp[i-1][j][k] + MOD - dp[i-1][j][k-1]) * k) % MOD; + } + } + } + return dp[n-1][h][m]; + } + static const int MOD = 1e9+7; +}; \ No newline at end of file From b5a881557e05eafdfa10b9873e0edd1cf89fc069 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 8 Oct 2023 19:04:01 +0530 Subject: [PATCH 0113/1399] Create README - LeetHub --- Insert in a Sorted List - GFG/README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 Insert in a Sorted List - GFG/README.md diff --git a/Insert in a Sorted List - GFG/README.md b/Insert in a Sorted List - GFG/README.md new file mode 100644 index 00000000..61d4403b --- /dev/null +++ b/Insert in a Sorted List - GFG/README.md @@ -0,0 +1,16 @@ +# Insert in a Sorted List +## Easy +

Given a linked list sorted in ascending order and an integer called data, insert data in the linked list such that the list remains sorted.

+

Example 1:

+
Input:
+LinkedList: 25->36->47->58->69->80
+data: 19
+Output: 
19 25 36 47 58 69 80
Explanation:

After inserting 19 the sorted linked list will look like the one in the output.
+

Example 2:

+
Input:
+LinkedList: 50->100
+data: 75
+Output: 
50 75 100
Explanation:
After inserting 75 the sorted linked list will look like the one in the output.
+

Your Task:
The task is to complete the function sortedInsert() which should insert the element in sorted Linked List and return the head of the linked list

+

Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).

+

Constraints:
1 <= N <= 104
-99999 <= A[i] <= 99999, for each valid i

\ No newline at end of file From ec252aa14be46134844d6af67558998d046f76fb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 8 Oct 2023 19:04:02 +0530 Subject: [PATCH 0114/1399] Added solution - LeetHub --- .../insert-in-a-sorted-list.cpp | 114 ++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 Insert in a Sorted List - GFG/insert-in-a-sorted-list.cpp diff --git a/Insert in a Sorted List - GFG/insert-in-a-sorted-list.cpp b/Insert in a Sorted List - GFG/insert-in-a-sorted-list.cpp new file mode 100644 index 00000000..2f15ec45 --- /dev/null +++ b/Insert in a Sorted List - GFG/insert-in-a-sorted-list.cpp @@ -0,0 +1,114 @@ +//{ Driver Code Starts +// + +#include +using namespace std; + +struct Node +{ + int data; + struct Node* next; + + Node(int x){ + data = x; + next = NULL; + } +}; + + +void printList(Node* node) +{ + while (node != NULL) { + cout << node->data <<" "; + node = node->next; + } + cout<<"\n"; +} + +// } Driver Code Ends +/* +structure of the node of the list is as +struct Node +{ + int data; + struct Node* next; + + Node(int x){ + data = x; + next = NULL; + } +}; +*/ + +class Solution{ + public: + // Should return head of the modified linked list + Node *sortedInsert(struct Node* head, int data) { + // Code + + + Node* newNode = new Node(data); + + Node* prev = nullptr, *temp = head; + + if(data <= temp->data) + { + newNode->next = head; + return newNode; + } + + while(temp) + { + if(temp->data >= data) + { + prev->next = newNode; + newNode->next = temp; + break; + } + prev = temp; + temp = temp->next; + } + + if(prev->data < data) + { + prev->next = newNode; + } + + return head; + + } +}; + + +//{ Driver Code Starts. +int main() +{ + int t; + cin>>t; + while(t--) + { + int n; + cin>>n; + + int data; + cin>>data; + + struct Node *head = new Node(data); + struct Node *tail = head; + for (int i = 0; i < n-1; ++i) + { + cin>>data; + tail->next = new Node(data); + tail = tail->next; + } + + int k; + cin>>k; + Solution obj; + head = obj.sortedInsert(head,k); + printList(head); + } + return 0; +} + +// } Driver Code Ends \ No newline at end of file From 454fd45e8cfc27032d21f272bdd86281bdacb432 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 8 Oct 2023 20:53:17 +0530 Subject: [PATCH 0115/1399] Create README - LeetHub --- .../README.md | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1458-max-dot-product-of-two-subsequences/README.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1458-max-dot-product-of-two-subsequences/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1458-max-dot-product-of-two-subsequences/README.md new file mode 100644 index 00000000..c15422c7 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1458-max-dot-product-of-two-subsequences/README.md @@ -0,0 +1,36 @@ +

No companies found for this problem
1458. Max Dot Product of Two Subsequences

Hard


Given two arrays nums1 and nums2.

+ +

Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length.

+ +

A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).

+ +

 

+

Example 1:

+ +
Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6]
+Output: 18
+Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2.
+Their dot product is (2*3 + (-2)*(-6)) = 18.
+ +

Example 2:

+ +
Input: nums1 = [3,-2], nums2 = [2,-6,7]
+Output: 21
+Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2.
+Their dot product is (3*7) = 21.
+ +

Example 3:

+ +
Input: nums1 = [-1,-1], nums2 = [1,1]
+Output: -1
+Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2.
+Their dot product is -1.
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums1.length, nums2.length <= 500
  • +
  • -1000 <= nums1[i], nums2[i] <= 1000
  • +
+
\ No newline at end of file From 049fbfbf18f727de092160e38d81382dbc2ed348 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 8 Oct 2023 20:53:21 +0530 Subject: [PATCH 0116/1399] Time: 45 ms (35.73%), Space: 13.7 MB (22.88%) - LeetHub --- ...58-max-dot-product-of-two-subsequences.cpp | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1458-max-dot-product-of-two-subsequences/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1458-max-dot-product-of-two-subsequences.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1458-max-dot-product-of-two-subsequences/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1458-max-dot-product-of-two-subsequences.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1458-max-dot-product-of-two-subsequences/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1458-max-dot-product-of-two-subsequences.cpp new file mode 100644 index 00000000..4413d5c2 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1458-max-dot-product-of-two-subsequences/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1458-max-dot-product-of-two-subsequences.cpp @@ -0,0 +1,56 @@ +class Solution { +public: + + int helper(int i, int j, int n, int m, vector& nums1, vector& nums2, vector>& dp) + { + if(i == n or j == m) + return 0; + + if(dp[i][j] != -1) + return dp[i][j]; + + int res = INT_MIN; + + int take = nums1[i] * nums2[j] + helper(i+1, j+1, n, m, nums1, nums2, dp); + + int moveI = helper(i+1, j, n, m, nums1, nums2, dp); + + int moveJ = helper(i, j+1, n, m, nums1, nums2, dp); + + return dp[i][j] = max({take, moveI, moveJ}); + + } + + int getMaxPair(vector& nums1, vector& nums2) + { + int ans = INT_MIN; + + for(auto& fir : nums1) + { + for(auto& sec : nums2) + { + ans = max(ans, fir*sec); + } + } + + return ans; + } + + int maxDotProduct(vector& nums1, vector& nums2) { + + int n = nums1.size(); + int m = nums2.size(); + + vector> dp(n+1, vector(m+1, -1)); + + int ans = helper(0, 0, n, m, nums1, nums2, dp); + + int maxPairValue = getMaxPair(nums1, nums2); + + if(ans == 0) + return maxPairValue; + + return ans; + + } +}; \ No newline at end of file From ed77b1f4f2222ca1e519c468952ec9bdac4aecd3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 8 Oct 2023 20:58:01 +0530 Subject: [PATCH 0117/1399] Create README - LeetHub --- .../README.md | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 1458-max-dot-product-of-two-subsequences/README.md diff --git a/1458-max-dot-product-of-two-subsequences/README.md b/1458-max-dot-product-of-two-subsequences/README.md new file mode 100644 index 00000000..d2dce7b9 --- /dev/null +++ b/1458-max-dot-product-of-two-subsequences/README.md @@ -0,0 +1,36 @@ +

1458. Max Dot Product of Two Subsequences

Hard


Given two arrays nums1 and nums2.

+ +

Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length.

+ +

A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).

+ +

 

+

Example 1:

+ +
Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6]
+Output: 18
+Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2.
+Their dot product is (2*3 + (-2)*(-6)) = 18.
+ +

Example 2:

+ +
Input: nums1 = [3,-2], nums2 = [2,-6,7]
+Output: 21
+Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2.
+Their dot product is (3*7) = 21.
+ +

Example 3:

+ +
Input: nums1 = [-1,-1], nums2 = [1,1]
+Output: -1
+Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2.
+Their dot product is -1.
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums1.length, nums2.length <= 500
  • +
  • -1000 <= nums1[i], nums2[i] <= 1000
  • +
+
\ No newline at end of file From 9c980dc047dfb99d3da91a4817d1d77bb169223b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 8 Oct 2023 20:58:07 +0530 Subject: [PATCH 0118/1399] Time: 45 ms (35.73%), Space: 13.7 MB (22.88%) - LeetHub --- ...58-max-dot-product-of-two-subsequences.cpp | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 1458-max-dot-product-of-two-subsequences/1458-max-dot-product-of-two-subsequences.cpp diff --git a/1458-max-dot-product-of-two-subsequences/1458-max-dot-product-of-two-subsequences.cpp b/1458-max-dot-product-of-two-subsequences/1458-max-dot-product-of-two-subsequences.cpp new file mode 100644 index 00000000..4413d5c2 --- /dev/null +++ b/1458-max-dot-product-of-two-subsequences/1458-max-dot-product-of-two-subsequences.cpp @@ -0,0 +1,56 @@ +class Solution { +public: + + int helper(int i, int j, int n, int m, vector& nums1, vector& nums2, vector>& dp) + { + if(i == n or j == m) + return 0; + + if(dp[i][j] != -1) + return dp[i][j]; + + int res = INT_MIN; + + int take = nums1[i] * nums2[j] + helper(i+1, j+1, n, m, nums1, nums2, dp); + + int moveI = helper(i+1, j, n, m, nums1, nums2, dp); + + int moveJ = helper(i, j+1, n, m, nums1, nums2, dp); + + return dp[i][j] = max({take, moveI, moveJ}); + + } + + int getMaxPair(vector& nums1, vector& nums2) + { + int ans = INT_MIN; + + for(auto& fir : nums1) + { + for(auto& sec : nums2) + { + ans = max(ans, fir*sec); + } + } + + return ans; + } + + int maxDotProduct(vector& nums1, vector& nums2) { + + int n = nums1.size(); + int m = nums2.size(); + + vector> dp(n+1, vector(m+1, -1)); + + int ans = helper(0, 0, n, m, nums1, nums2, dp); + + int maxPairValue = getMaxPair(nums1, nums2); + + if(ans == 0) + return maxPairValue; + + return ans; + + } +}; \ No newline at end of file From 9a51fa0d6d3afe20d62c2d64523e706d611490cc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 8 Oct 2023 21:06:10 +0530 Subject: [PATCH 0119/1399] Attach NOTES - LeetHub --- 1458-max-dot-product-of-two-subsequences/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1458-max-dot-product-of-two-subsequences/NOTES.md diff --git a/1458-max-dot-product-of-two-subsequences/NOTES.md b/1458-max-dot-product-of-two-subsequences/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1458-max-dot-product-of-two-subsequences/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From edf0d918f5c4c5a3dc2b98139830d063bb8e5a26 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 8 Oct 2023 21:06:14 +0530 Subject: [PATCH 0120/1399] Time: 45 ms (35.73%), Space: 13.7 MB (22.88%) - LeetHub From 5d849c7f88e7c2daf70a015630dbe072940bf30e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 8 Oct 2023 21:09:18 +0530 Subject: [PATCH 0121/1399] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1458-max-dot-product-of-two-subsequences/NOTES.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1458-max-dot-product-of-two-subsequences/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1458-max-dot-product-of-two-subsequences/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1458-max-dot-product-of-two-subsequences/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 1cfe8c3ceafe208ea7286f1aaba977cccc689ac9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 8 Oct 2023 21:09:22 +0530 Subject: [PATCH 0122/1399] Time: 36 ms (49.61%), Space: 13.7 MB (21.08%) - LeetHub --- ...58-max-dot-product-of-two-subsequences.cpp | 34 +++---------------- 1 file changed, 5 insertions(+), 29 deletions(-) diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1458-max-dot-product-of-two-subsequences/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1458-max-dot-product-of-two-subsequences.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1458-max-dot-product-of-two-subsequences/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1458-max-dot-product-of-two-subsequences.cpp index 4413d5c2..145c11a2 100644 --- a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1458-max-dot-product-of-two-subsequences/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1458-max-dot-product-of-two-subsequences.cpp +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1458-max-dot-product-of-two-subsequences/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1458-max-dot-product-of-two-subsequences.cpp @@ -4,38 +4,21 @@ class Solution { int helper(int i, int j, int n, int m, vector& nums1, vector& nums2, vector>& dp) { if(i == n or j == m) - return 0; + return -1e9; if(dp[i][j] != -1) return dp[i][j]; int res = INT_MIN; - int take = nums1[i] * nums2[j] + helper(i+1, j+1, n, m, nums1, nums2, dp); + int take = max(nums1[i] * nums2[j], nums1[i] * nums2[j] + helper(i+1, j+1, n, m, nums1, nums2, dp)); - int moveI = helper(i+1, j, n, m, nums1, nums2, dp); + int notTake = max(helper(i+1, j, n, m, nums1, nums2, dp), helper(i, j+1, n, m, nums1, nums2, dp)); - int moveJ = helper(i, j+1, n, m, nums1, nums2, dp); - - return dp[i][j] = max({take, moveI, moveJ}); + return dp[i][j] = max(take, notTake); } - int getMaxPair(vector& nums1, vector& nums2) - { - int ans = INT_MIN; - - for(auto& fir : nums1) - { - for(auto& sec : nums2) - { - ans = max(ans, fir*sec); - } - } - - return ans; - } - int maxDotProduct(vector& nums1, vector& nums2) { int n = nums1.size(); @@ -43,14 +26,7 @@ class Solution { vector> dp(n+1, vector(m+1, -1)); - int ans = helper(0, 0, n, m, nums1, nums2, dp); - - int maxPairValue = getMaxPair(nums1, nums2); - - if(ans == 0) - return maxPairValue; - - return ans; + return helper(0, 0, n, m, nums1, nums2, dp); } }; \ No newline at end of file From 705a6ac1eac5660ed15117ea6283bfcaadaed6d6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 9 Oct 2023 08:13:13 +0530 Subject: [PATCH 0123/1399] Create README - LeetHub --- .../README.md | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div34-find-first-and-last-position-of-element-in-sorted-array/README.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div34-find-first-and-last-position-of-element-in-sorted-array/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div34-find-first-and-last-position-of-element-in-sorted-array/README.md new file mode 100644 index 00000000..30176ded --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div34-find-first-and-last-position-of-element-in-sorted-array/README.md @@ -0,0 +1,27 @@ +

No companies found for this problem
34. Find First and Last Position of Element in Sorted Array

Medium


Given an array of integers nums sorted in non-decreasing order, find the starting and ending position of a given target value.

+ +

If target is not found in the array, return [-1, -1].

+ +

You must write an algorithm with O(log n) runtime complexity.

+ +

 

+

Example 1:

+
Input: nums = [5,7,7,8,8,10], target = 8
+Output: [3,4]
+

Example 2:

+
Input: nums = [5,7,7,8,8,10], target = 6
+Output: [-1,-1]
+

Example 3:

+
Input: nums = [], target = 0
+Output: [-1,-1]
+
+

 

+

Constraints:

+ +
    +
  • 0 <= nums.length <= 105
  • +
  • -109 <= nums[i] <= 109
  • +
  • nums is a non-decreasing array.
  • +
  • -109 <= target <= 109
  • +
+
\ No newline at end of file From 8c2c4ee705c9a36929badf2180144d9b0736e78d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 9 Oct 2023 08:13:13 +0530 Subject: [PATCH 0124/1399] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div34-find-first-and-last-position-of-element-in-sorted-array/NOTES.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div34-find-first-and-last-position-of-element-in-sorted-array/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div34-find-first-and-last-position-of-element-in-sorted-array/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div34-find-first-and-last-position-of-element-in-sorted-array/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From a86eeeb0792c4c1ad4b4f8e89d1821ad70846bb0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 9 Oct 2023 08:13:17 +0530 Subject: [PATCH 0125/1399] Time: 7 ms (51.15%), Space: 14 MB (14.74%) - LeetHub --- ...st-position-of-element-in-sorted-array.cpp | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div34-find-first-and-last-position-of-element-in-sorted-array/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div34-find-first-and-last-position-of-element-in-sorted-array.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div34-find-first-and-last-position-of-element-in-sorted-array/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div34-find-first-and-last-position-of-element-in-sorted-array.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div34-find-first-and-last-position-of-element-in-sorted-array/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div34-find-first-and-last-position-of-element-in-sorted-array.cpp new file mode 100644 index 00000000..16bba9d0 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div34-find-first-and-last-position-of-element-in-sorted-array/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div34-find-first-and-last-position-of-element-in-sorted-array.cpp @@ -0,0 +1,72 @@ +class Solution { +public: + + int binarySearchFirst(vector& nums, int target) + { + int start = 0, end = nums.size()-1; + + int first = - 1; + + while(start <= end) + { + int mid = start + (end - start)/ 2; + + if(nums[mid] == target) + { + first = mid; + end = mid-1; + } + else if(nums[mid] < target) + { + start = mid+1; + } + else + { + end = mid - 1; + } + } + + return first; + } + + int binarySearchEnd(vector& nums, int target) + { + int start = 0, end = nums.size()-1; + + int last = -1; + + while(start <= end) + { + int mid = start + (end - start)/ 2; + + if(nums[mid] == target) + { + last = mid; + start = mid+1; + } + else if(nums[mid] > target) + { + end = mid-1; + } + else + { + start = mid+1; + } + } + + return last; + } + + vector searchRange(vector& nums, int target) { + + int n = nums.size(); + + int first = -1, last = -1; + + first = binarySearchFirst(nums, target); + last = binarySearchEnd(nums, target); + + return {first, last}; + + } +}; \ No newline at end of file From b5c17cc9a81b177311f26e0e38d6d3550efc416a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 9 Oct 2023 08:14:19 +0530 Subject: [PATCH 0126/1399] Create README - LeetHub --- .../README.md | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 0034-find-first-and-last-position-of-element-in-sorted-array/README.md diff --git a/0034-find-first-and-last-position-of-element-in-sorted-array/README.md b/0034-find-first-and-last-position-of-element-in-sorted-array/README.md new file mode 100644 index 00000000..3ba57ee9 --- /dev/null +++ b/0034-find-first-and-last-position-of-element-in-sorted-array/README.md @@ -0,0 +1,27 @@ +

34. Find First and Last Position of Element in Sorted Array

Medium


Given an array of integers nums sorted in non-decreasing order, find the starting and ending position of a given target value.

+ +

If target is not found in the array, return [-1, -1].

+ +

You must write an algorithm with O(log n) runtime complexity.

+ +

 

+

Example 1:

+
Input: nums = [5,7,7,8,8,10], target = 8
+Output: [3,4]
+

Example 2:

+
Input: nums = [5,7,7,8,8,10], target = 6
+Output: [-1,-1]
+

Example 3:

+
Input: nums = [], target = 0
+Output: [-1,-1]
+
+

 

+

Constraints:

+ +
    +
  • 0 <= nums.length <= 105
  • +
  • -109 <= nums[i] <= 109
  • +
  • nums is a non-decreasing array.
  • +
  • -109 <= target <= 109
  • +
+
\ No newline at end of file From 5bb8aae5a2d246ab3cd911780e049188f3601a0c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 9 Oct 2023 08:14:23 +0530 Subject: [PATCH 0127/1399] Time: 7 ms (51.15%), Space: 14 MB (14.74%) - LeetHub --- ...st-position-of-element-in-sorted-array.cpp | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 0034-find-first-and-last-position-of-element-in-sorted-array/0034-find-first-and-last-position-of-element-in-sorted-array.cpp diff --git a/0034-find-first-and-last-position-of-element-in-sorted-array/0034-find-first-and-last-position-of-element-in-sorted-array.cpp b/0034-find-first-and-last-position-of-element-in-sorted-array/0034-find-first-and-last-position-of-element-in-sorted-array.cpp new file mode 100644 index 00000000..16bba9d0 --- /dev/null +++ b/0034-find-first-and-last-position-of-element-in-sorted-array/0034-find-first-and-last-position-of-element-in-sorted-array.cpp @@ -0,0 +1,72 @@ +class Solution { +public: + + int binarySearchFirst(vector& nums, int target) + { + int start = 0, end = nums.size()-1; + + int first = - 1; + + while(start <= end) + { + int mid = start + (end - start)/ 2; + + if(nums[mid] == target) + { + first = mid; + end = mid-1; + } + else if(nums[mid] < target) + { + start = mid+1; + } + else + { + end = mid - 1; + } + } + + return first; + } + + int binarySearchEnd(vector& nums, int target) + { + int start = 0, end = nums.size()-1; + + int last = -1; + + while(start <= end) + { + int mid = start + (end - start)/ 2; + + if(nums[mid] == target) + { + last = mid; + start = mid+1; + } + else if(nums[mid] > target) + { + end = mid-1; + } + else + { + start = mid+1; + } + } + + return last; + } + + vector searchRange(vector& nums, int target) { + + int n = nums.size(); + + int first = -1, last = -1; + + first = binarySearchFirst(nums, target); + last = binarySearchEnd(nums, target); + + return {first, last}; + + } +}; \ No newline at end of file From cd3563e7de80c60837aaf02e99d152ca55f7ef07 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 9 Oct 2023 08:18:00 +0530 Subject: [PATCH 0128/1399] Create README - LeetHub --- Height of Binary Tree - GFG/README.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Height of Binary Tree - GFG/README.md diff --git a/Height of Binary Tree - GFG/README.md b/Height of Binary Tree - GFG/README.md new file mode 100644 index 00000000..ca23b059 --- /dev/null +++ b/Height of Binary Tree - GFG/README.md @@ -0,0 +1,21 @@ +# Height of Binary Tree +## Easy +

Given a binary tree, find its height.

+

Example 1:

+
Input:
+     1
+    /  \
+   2    3
+Output: 2
+
+

Example 2:

+
Input:
+  2
+   \
+    1
+   /
+ 3
+Output: 3   
+

Your Task:
You don't need to read input or print anything. Your task is to complete the function height() which takes root node of the tree as input parameter and returns an integer denoting the height of the tree. If the tree is empty, return 0. 

+

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)

+

Constraints:
1 <= Number of nodes <= 105
1 <= Data of a node <= 109

\ No newline at end of file From 8fe26cee2010b463ce2fbbe29d09780771105b54 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 9 Oct 2023 08:18:01 +0530 Subject: [PATCH 0129/1399] Added solution - LeetHub --- .../height-of-binary-tree.cpp | 127 ++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 Height of Binary Tree - GFG/height-of-binary-tree.cpp diff --git a/Height of Binary Tree - GFG/height-of-binary-tree.cpp b/Height of Binary Tree - GFG/height-of-binary-tree.cpp new file mode 100644 index 00000000..325faf43 --- /dev/null +++ b/Height of Binary Tree - GFG/height-of-binary-tree.cpp @@ -0,0 +1,127 @@ +//{ Driver Code Starts +//Initial template for C++ + +#include +using namespace std; + +struct Node +{ + int data; + struct Node *left; + struct Node *right; + + Node(int val) { + data = val; + left = right = NULL; + } +}; + +// Function to Build Tree +Node* buildTree(string str) +{ + // Corner Case + if(str.length() == 0 || str[0] == 'N') + return NULL; + + // Creating vector of strings from input + // string after spliting by space + vector ip; + + istringstream iss(str); + for(string str; iss >> str; ) + ip.push_back(str); + + // Create the root of the tree + Node *root = new Node(stoi(ip[0])); + + // Push the root to the queue + queue queue; + queue.push(root); + + // Starting from the second element + int i = 1; + while(!queue.empty() && i < ip.size()) { + + // Get and remove the front of the queue + Node* currNode = queue.front(); + queue.pop(); + + // Get the current node's value from the string + string currVal = ip[i]; + + // If the left child is not null + if(currVal != "N") { + + // Create the left child for the current Node + currNode->left = new Node(stoi(currVal)); + + // Push it to the queue + queue.push(currNode->left); + } + + // For the right child + i++; + if(i >= ip.size()) + break; + currVal = ip[i]; + + // If the right child is not null + if(currVal != "N") { + + // Create the right child for the current node + currNode->right = new Node(stoi(currVal)); + + // Push it to the queue + queue.push(currNode->right); + } + i++; + } + + return root; +} + +// } Driver Code Ends +//User function template for C++ + +/* +struct Node +{ + int data; + struct Node* left; + struct Node* right; + + Node(int x){ + data = x; + left = right = NULL; + } +}; +*/ +class Solution{ + public: + //Function to find the height of a binary tree. + int height(struct Node* node){ + // code here + + if(!node) + return 0; + + return 1 + max(height(node->left), height(node->right)); + } +}; + +//{ Driver Code Starts. +int main() +{ + int t; + scanf("%d ",&t); + while(t--) + { + string treeString; + getline(cin,treeString); + Node* root = buildTree(treeString); + Solution ob; + cout< Date: Tue, 10 Oct 2023 22:00:21 +0530 Subject: [PATCH 0130/1399] Create README - LeetHub --- .../README.md | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2009-minimum-number-of-operations-to-make-array-continuous/README.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2009-minimum-number-of-operations-to-make-array-continuous/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2009-minimum-number-of-operations-to-make-array-continuous/README.md new file mode 100644 index 00000000..0ec8717b --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2009-minimum-number-of-operations-to-make-array-continuous/README.md @@ -0,0 +1,48 @@ +

No companies found for this problem
2009. Minimum Number of Operations to Make Array Continuous

Hard


You are given an integer array nums. In one operation, you can replace any element in nums with any integer.

+ +

nums is considered continuous if both of the following conditions are fulfilled:

+ +
    +
  • All elements in nums are unique.
  • +
  • The difference between the maximum element and the minimum element in nums equals nums.length - 1.
  • +
+ +

For example, nums = [4, 2, 5, 3] is continuous, but nums = [1, 2, 3, 5, 6] is not continuous.

+ +

Return the minimum number of operations to make nums continuous.

+ +

 

+

Example 1:

+ +
Input: nums = [4,2,5,3]
+Output: 0
+Explanation: nums is already continuous.
+
+ +

Example 2:

+ +
Input: nums = [1,2,3,5,6]
+Output: 1
+Explanation: One possible solution is to change the last element to 4.
+The resulting array is [1,2,3,5,4], which is continuous.
+
+ +

Example 3:

+ +
Input: nums = [1,10,100,1000]
+Output: 3
+Explanation: One possible solution is to:
+- Change the second element to 2.
+- Change the third element to 3.
+- Change the fourth element to 4.
+The resulting array is [1,2,3,4], which is continuous.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 105
  • +
  • 1 <= nums[i] <= 109
  • +
+
\ No newline at end of file From 22dfc04c305d1d0e83204c48f8b9bc4c41acedc7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 10 Oct 2023 22:00:25 +0530 Subject: [PATCH 0131/1399] Time: 151 ms (73.01%), Space: 69.9 MB (50.84%) - LeetHub --- ...of-operations-to-make-array-continuous.cpp | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2009-minimum-number-of-operations-to-make-array-continuous/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2009-minimum-number-of-operations-to-make-array-continuous.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2009-minimum-number-of-operations-to-make-array-continuous/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2009-minimum-number-of-operations-to-make-array-continuous.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2009-minimum-number-of-operations-to-make-array-continuous/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2009-minimum-number-of-operations-to-make-array-continuous.cpp new file mode 100644 index 00000000..0214797b --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2009-minimum-number-of-operations-to-make-array-continuous/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2009-minimum-number-of-operations-to-make-array-continuous.cpp @@ -0,0 +1,22 @@ +class Solution { +public: + int minOperations(vector& arr) { + int n = arr.size(); + if(n == 1) return 0; + sort(arr.begin(), arr.end()); + + vector a; + for(int i = 0; i < n-1; i++) { + while(arr[i] == arr[i+1]) i++; + a.push_back(arr[i]); + } + if(arr.back() != a.back()) a.push_back(arr.back()); + + int mx = 0; + for(int i = 0, j = 0; i < a.size(); i++) { + while(j <= i && (a[i]-a[j]+1) > n) j++; + mx = max(mx, i-j+1); + } + return n - mx; +} +}; \ No newline at end of file From 0b0ed88098f18e60a460ac6356298486ce81ff72 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 11 Oct 2023 20:02:31 +0530 Subject: [PATCH 0132/1399] Create README - LeetHub --- Check for Balanced Tree - GFG/README.md | 32 +++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 Check for Balanced Tree - GFG/README.md diff --git a/Check for Balanced Tree - GFG/README.md b/Check for Balanced Tree - GFG/README.md new file mode 100644 index 00000000..470fdd32 --- /dev/null +++ b/Check for Balanced Tree - GFG/README.md @@ -0,0 +1,32 @@ +# Check for Balanced Tree +## Easy +

Given a binary tree, find if it is height balanced or not. 
A tree is height balanced if difference between heights of left and right subtrees is not more than one for all nodes of tree. 

+

A height balanced tree
        1
     /     \
   10      39
  /
5

+

An unbalanced tree
        1
     /    
   10   
  /
5

+

Example 1:

+
Input:
+      1
+    /
+   2
+    \
+     3 
+Output: 0
+Explanation: The max difference in height
+of left subtree and right subtree is 2,
+which is greater than 1. Hence unbalanced
+
+

Example 2:

+
Input:
+       10
+     /   \
+    20   30 
+  /   \
+ 40   60
+Output: 1
+Explanation: The max difference in height
+of left subtree and right subtree is 1.
+Hence balanced. 
+
+

Your Task:
You don't need to take input. Just complete the function isBalanced() that takes root node as parameter and returns true, if the tree is balanced else returns false.

+

Constraints:
1 <= Number of nodes <= 105
1 <= Data of a node <= 109

+

Expected time complexity: O(N)
Expected auxiliary space: O(h) , where h = height of tree

\ No newline at end of file From e69a3068e0367c0ab4293a43d6ea9f7529527221 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 11 Oct 2023 20:02:32 +0530 Subject: [PATCH 0133/1399] Added solution - LeetHub --- .../check-for-balanced-tree.cpp | 152 ++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 Check for Balanced Tree - GFG/check-for-balanced-tree.cpp diff --git a/Check for Balanced Tree - GFG/check-for-balanced-tree.cpp b/Check for Balanced Tree - GFG/check-for-balanced-tree.cpp new file mode 100644 index 00000000..df84409c --- /dev/null +++ b/Check for Balanced Tree - GFG/check-for-balanced-tree.cpp @@ -0,0 +1,152 @@ +//{ Driver Code Starts +//Initial Template for C++ + + +#include +using namespace std; + + +// Tree Node +struct Node { + int data; + Node* left; + Node* right; +}; + +// Utility function to create a new Tree Node +Node* newNode(int val) { + Node* temp = new Node; + temp->data = val; + temp->left = NULL; + temp->right = NULL; + + return temp; +} + + +// Function to Build Tree +Node* buildTree(string str) { + // Corner Case + if (str.length() == 0 || str[0] == 'N') return NULL; + + // Creating vector of strings from input + // string after spliting by space + vector ip; + + istringstream iss(str); + for (string str; iss >> str;) ip.push_back(str); + + // Create the root of the tree + Node* root = newNode(stoi(ip[0])); + + // Push the root to the queue + queue queue; + queue.push(root); + + // Starting from the second element + int i = 1; + while (!queue.empty() && i < ip.size()) { + + // Get and remove the front of the queue + Node* currNode = queue.front(); + queue.pop(); + + // Get the current node's value from the string + string currVal = ip[i]; + + // If the left child is not null + if (currVal != "N") { + + // Create the left child for the current node + currNode->left = newNode(stoi(currVal)); + + // Push it to the queue + queue.push(currNode->left); + } + + // For the right child + i++; + if (i >= ip.size()) break; + currVal = ip[i]; + + // If the right child is not null + if (currVal != "N") { + + // Create the right child for the current node + currNode->right = newNode(stoi(currVal)); + + // Push it to the queue + queue.push(currNode->right); + } + i++; + } + + return root; +} + + +// } Driver Code Ends +/* A binary tree node structure + +struct Node +{ + int data; + struct Node* left; + struct Node* right; + + Node(int x){ + data = x; + left = right = NULL; + } +}; + */ + +class Solution{ + public: + //Function to check whether a binary tree is balanced or not. + + int height(Node* root) + { + if(!root) + return 0; + return 1 + max(height(root->left), height(root->right)); + } + + bool isBalanced(Node *root) + { + // Your Code here + + if(!root) + return true; + + int left = height(root->left); + int right = height(root->right); + + return abs(left-right) <=1 and isBalanced(root->left) and isBalanced(root->right); + } +}; + + +//{ Driver Code Starts. + +/* Driver program to test size function*/ + + + +int main() { + + + int t; + scanf("%d ", &t); + while (t--) { + string s, ch; + getline(cin, s); + + Node* root = buildTree(s); + Solution ob; + cout << ob.isBalanced(root) << endl; + } + return 0; +} + +// } Driver Code Ends \ No newline at end of file From 096896245374ad8209b036e86e889bd74e57d0de Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 11 Oct 2023 21:40:47 +0530 Subject: [PATCH 0134/1399] Create README - LeetHub --- .../README.md | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2251-number-of-flowers-in-full-bloom/README.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2251-number-of-flowers-in-full-bloom/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2251-number-of-flowers-in-full-bloom/README.md new file mode 100644 index 00000000..fc333323 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2251-number-of-flowers-in-full-bloom/README.md @@ -0,0 +1,32 @@ +

No companies found for this problem
2251. Number of Flowers in Full Bloom

Hard


You are given a 0-indexed 2D integer array flowers, where flowers[i] = [starti, endi] means the ith flower will be in full bloom from starti to endi (inclusive). You are also given a 0-indexed integer array people of size n, where people[i] is the time that the ith person will arrive to see the flowers.

+ +

Return an integer array answer of size n, where answer[i] is the number of flowers that are in full bloom when the ith person arrives.

+ +

 

+

Example 1:

+ +
Input: flowers = [[1,6],[3,7],[9,12],[4,13]], poeple = [2,3,7,11]
+Output: [1,2,2,2]
+Explanation: The figure above shows the times when the flowers are in full bloom and when the people arrive.
+For each person, we return the number of flowers in full bloom during their arrival.
+
+ +

Example 2:

+ +
Input: flowers = [[1,10],[3,3]], poeple = [3,3,2]
+Output: [2,2,1]
+Explanation: The figure above shows the times when the flowers are in full bloom and when the people arrive.
+For each person, we return the number of flowers in full bloom during their arrival.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= flowers.length <= 5 * 104
  • +
  • flowers[i].length == 2
  • +
  • 1 <= starti <= endi <= 109
  • +
  • 1 <= people.length <= 5 * 104
  • +
  • 1 <= people[i] <= 109
  • +
+
\ No newline at end of file From c1b2c9e7e776724c4389c040cb085cd6780fb817 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 11 Oct 2023 21:40:50 +0530 Subject: [PATCH 0135/1399] Time: 247 ms (63.34%), Space: 82.5 MB (74.95%) - LeetHub --- ...-div2251-number-of-flowers-in-full-bloom.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2251-number-of-flowers-in-full-bloom/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2251-number-of-flowers-in-full-bloom.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2251-number-of-flowers-in-full-bloom/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2251-number-of-flowers-in-full-bloom.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2251-number-of-flowers-in-full-bloom/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2251-number-of-flowers-in-full-bloom.cpp new file mode 100644 index 00000000..b4d8e152 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2251-number-of-flowers-in-full-bloom/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2251-number-of-flowers-in-full-bloom.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + vector fullBloomFlowers(vector>& flowers, vector& persons) { + vector start, end; + for (auto& t : flowers) + start.push_back(t[0]), end.push_back(t[1]); + sort(start.begin(), start.end()); + sort(end.begin(), end.end()); + vector res; + for (int t : persons) { + int started = upper_bound(start.begin(), start.end(), t) - start.begin(); + int ended = lower_bound(end.begin(), end.end(), t) - end.begin(); + res.push_back(started - ended); + } + return res; + } +}; \ No newline at end of file From d7b573170668e15649ef21cc0a435b3dcf88ebf9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 11 Oct 2023 21:41:13 +0530 Subject: [PATCH 0136/1399] Create README - LeetHub --- .../README.md | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 2251-number-of-flowers-in-full-bloom/README.md diff --git a/2251-number-of-flowers-in-full-bloom/README.md b/2251-number-of-flowers-in-full-bloom/README.md new file mode 100644 index 00000000..e6a39513 --- /dev/null +++ b/2251-number-of-flowers-in-full-bloom/README.md @@ -0,0 +1,32 @@ +

2251. Number of Flowers in Full Bloom

Hard


You are given a 0-indexed 2D integer array flowers, where flowers[i] = [starti, endi] means the ith flower will be in full bloom from starti to endi (inclusive). You are also given a 0-indexed integer array people of size n, where people[i] is the time that the ith person will arrive to see the flowers.

+ +

Return an integer array answer of size n, where answer[i] is the number of flowers that are in full bloom when the ith person arrives.

+ +

 

+

Example 1:

+ +
Input: flowers = [[1,6],[3,7],[9,12],[4,13]], poeple = [2,3,7,11]
+Output: [1,2,2,2]
+Explanation: The figure above shows the times when the flowers are in full bloom and when the people arrive.
+For each person, we return the number of flowers in full bloom during their arrival.
+
+ +

Example 2:

+ +
Input: flowers = [[1,10],[3,3]], poeple = [3,3,2]
+Output: [2,2,1]
+Explanation: The figure above shows the times when the flowers are in full bloom and when the people arrive.
+For each person, we return the number of flowers in full bloom during their arrival.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= flowers.length <= 5 * 104
  • +
  • flowers[i].length == 2
  • +
  • 1 <= starti <= endi <= 109
  • +
  • 1 <= people.length <= 5 * 104
  • +
  • 1 <= people[i] <= 109
  • +
+
\ No newline at end of file From 1dbf96abc7d910a7721b6981b834331937a1a890 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 11 Oct 2023 21:41:13 +0530 Subject: [PATCH 0137/1399] Attach NOTES - LeetHub --- 2251-number-of-flowers-in-full-bloom/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2251-number-of-flowers-in-full-bloom/NOTES.md diff --git a/2251-number-of-flowers-in-full-bloom/NOTES.md b/2251-number-of-flowers-in-full-bloom/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2251-number-of-flowers-in-full-bloom/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From ef4a9cbe79a2282f231728e63e12aff0c02ee08a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 11 Oct 2023 21:41:17 +0530 Subject: [PATCH 0138/1399] Time: 247 ms (63.34%), Space: 82.5 MB (74.95%) - LeetHub --- .../2251-number-of-flowers-in-full-bloom.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 2251-number-of-flowers-in-full-bloom/2251-number-of-flowers-in-full-bloom.cpp diff --git a/2251-number-of-flowers-in-full-bloom/2251-number-of-flowers-in-full-bloom.cpp b/2251-number-of-flowers-in-full-bloom/2251-number-of-flowers-in-full-bloom.cpp new file mode 100644 index 00000000..b4d8e152 --- /dev/null +++ b/2251-number-of-flowers-in-full-bloom/2251-number-of-flowers-in-full-bloom.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + vector fullBloomFlowers(vector>& flowers, vector& persons) { + vector start, end; + for (auto& t : flowers) + start.push_back(t[0]), end.push_back(t[1]); + sort(start.begin(), start.end()); + sort(end.begin(), end.end()); + vector res; + for (int t : persons) { + int started = upper_bound(start.begin(), start.end(), t) - start.begin(); + int ended = lower_bound(end.begin(), end.end(), t) - end.begin(); + res.push_back(started - ended); + } + return res; + } +}; \ No newline at end of file From c3b51b3745c54245a1d5333c009b175ab131fcd1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 12 Oct 2023 23:44:19 +0530 Subject: [PATCH 0139/1399] Create README - LeetHub --- .../README.md | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1095-find-in-mountain-array/README.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1095-find-in-mountain-array/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1095-find-in-mountain-array/README.md new file mode 100644 index 00000000..2e3225cb --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1095-find-in-mountain-array/README.md @@ -0,0 +1,48 @@ +

No companies found for this problem
1095. Find in Mountain Array

Hard


(This problem is an interactive problem.)

+ +

You may recall that an array arr is a mountain array if and only if:

+ +
    +
  • arr.length >= 3
  • +
  • There exists some i with 0 < i < arr.length - 1 such that: +
      +
    • arr[0] < arr[1] < ... < arr[i - 1] < arr[i]
    • +
    • arr[i] > arr[i + 1] > ... > arr[arr.length - 1]
    • +
    +
  • +
+ +

Given a mountain array mountainArr, return the minimum index such that mountainArr.get(index) == target. If such an index does not exist, return -1.

+ +

You cannot access the mountain array directly. You may only access the array using a MountainArray interface:

+ +
    +
  • MountainArray.get(k) returns the element of the array at index k (0-indexed).
  • +
  • MountainArray.length() returns the length of the array.
  • +
+ +

Submissions making more than 100 calls to MountainArray.get will be judged Wrong Answer. Also, any solutions that attempt to circumvent the judge will result in disqualification.

+ +

 

+

Example 1:

+ +
Input: array = [1,2,3,4,5,3,1], target = 3
+Output: 2
+Explanation: 3 exists in the array, at index=2 and index=5. Return the minimum index, which is 2.
+ +

Example 2:

+ +
Input: array = [0,1,2,4,2,1], target = 3
+Output: -1
+Explanation: 3 does not exist in the array, so we return -1.
+
+ +

 

+

Constraints:

+ +
    +
  • 3 <= mountain_arr.length() <= 104
  • +
  • 0 <= target <= 109
  • +
  • 0 <= mountain_arr.get(index) <= 109
  • +
+
\ No newline at end of file From 3db385891bc7102a123edb4d2bfcf9bb17cc5fa2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 12 Oct 2023 23:44:23 +0530 Subject: [PATCH 0140/1399] Time: 3 ms (78.78%), Space: 7.4 MB (14.97%) - LeetHub --- ...div-div-div1095-find-in-mountain-array.cpp | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1095-find-in-mountain-array/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1095-find-in-mountain-array.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1095-find-in-mountain-array/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1095-find-in-mountain-array.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1095-find-in-mountain-array/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1095-find-in-mountain-array.cpp new file mode 100644 index 00000000..f0261a5c --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1095-find-in-mountain-array/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1095-find-in-mountain-array.cpp @@ -0,0 +1,51 @@ +/** + * // This is the MountainArray's API interface. + * // You should not implement it, or speculate about its implementation + * class MountainArray { + * public: + * int get(int index); + * int length(); + * }; + */ + +class Solution { +public: + int findInMountainArray(int target, MountainArray A) { + int n = A.length(), l, r, m, peak = 0; + + l = 0; + r = n - 1; + while (l < r) { + m = (l + r) / 2; + if (A.get(m) < A.get(m + 1)) + l = peak = m + 1; + else + r = m; + } + + l = 0; + r = peak; + while (l <= r) { + m = (l + r) / 2; + if (A.get(m) < target) + l = m + 1; + else if (A.get(m) > target) + r = m - 1; + else + return m; + } + + l = peak; + r = n - 1; + while (l <= r) { + m = (l + r) / 2; + if (A.get(m) > target) + l = m + 1; + else if (A.get(m) < target) + r = m - 1; + else + return m; + } + return -1; + } +}; \ No newline at end of file From af4becbefff558d88e96b5cc435e0fcfd7d20d06 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 13 Oct 2023 20:43:24 +0530 Subject: [PATCH 0141/1399] Create README - LeetHub --- .../README.md | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div746-min-cost-climbing-stairs/README.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div746-min-cost-climbing-stairs/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div746-min-cost-climbing-stairs/README.md new file mode 100644 index 00000000..5ca7f39b --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div746-min-cost-climbing-stairs/README.md @@ -0,0 +1,38 @@ +

Amazon
5
Adobe
4
Bloomberg
2
746. Min Cost Climbing Stairs

Easy


You are given an integer array cost where cost[i] is the cost of ith step on a staircase. Once you pay the cost, you can either climb one or two steps.

+ +

You can either start from the step with index 0, or the step with index 1.

+ +

Return the minimum cost to reach the top of the floor.

+ +

 

+

Example 1:

+ +
Input: cost = [10,15,20]
+Output: 15
+Explanation: You will start at index 1.
+- Pay 15 and climb two steps to reach the top.
+The total cost is 15.
+
+ +

Example 2:

+ +
Input: cost = [1,100,1,1,1,100,1,1,100,1]
+Output: 6
+Explanation: You will start at index 0.
+- Pay 1 and climb two steps to reach index 2.
+- Pay 1 and climb two steps to reach index 4.
+- Pay 1 and climb two steps to reach index 6.
+- Pay 1 and climb one step to reach index 7.
+- Pay 1 and climb two steps to reach index 9.
+- Pay 1 and climb one step to reach the top.
+The total cost is 6.
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= cost.length <= 1000
  • +
  • 0 <= cost[i] <= 999
  • +
+
\ No newline at end of file From 1389626c1c596f00db6e9751376e44234e2f8f19 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 13 Oct 2023 20:43:27 +0530 Subject: [PATCH 0142/1399] Time: 8 ms (20.63%), Space: 14 MB (26.23%) - LeetHub --- ...iv-div-div746-min-cost-climbing-stairs.cpp | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div746-min-cost-climbing-stairs/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div746-min-cost-climbing-stairs.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div746-min-cost-climbing-stairs/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div746-min-cost-climbing-stairs.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div746-min-cost-climbing-stairs/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div746-min-cost-climbing-stairs.cpp new file mode 100644 index 00000000..f2dbaf0d --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div746-min-cost-climbing-stairs/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div746-min-cost-climbing-stairs.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + + int helper(int idx, int n, vector& cost, vector& dp) + { + if(idx >= n) + return 0; + + if(dp[idx] != -1) + return dp[idx]; + + int oneStep = cost[idx] + helper(idx+1, n, cost, dp); + int twoStep = cost[idx] + helper(idx+2, n, cost, dp); + + return dp[idx] = min(oneStep, twoStep); + } + + int minCostClimbingStairs(vector& cost) { + + int n = cost.size(); + + vector dp(n+1, -1); + + return min(helper(0, n, cost, dp), helper(1, n, cost, dp)); + + } +}; \ No newline at end of file From a08f20a5a2287336247238eb3cda1b995bd86ad1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 13 Oct 2023 20:51:26 +0530 Subject: [PATCH 0143/1399] Create README - LeetHub --- 0746-min-cost-climbing-stairs/README.md | 38 +++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 0746-min-cost-climbing-stairs/README.md diff --git a/0746-min-cost-climbing-stairs/README.md b/0746-min-cost-climbing-stairs/README.md new file mode 100644 index 00000000..c810b720 --- /dev/null +++ b/0746-min-cost-climbing-stairs/README.md @@ -0,0 +1,38 @@ +

746. Min Cost Climbing Stairs

Easy


You are given an integer array cost where cost[i] is the cost of ith step on a staircase. Once you pay the cost, you can either climb one or two steps.

+ +

You can either start from the step with index 0, or the step with index 1.

+ +

Return the minimum cost to reach the top of the floor.

+ +

 

+

Example 1:

+ +
Input: cost = [10,15,20]
+Output: 15
+Explanation: You will start at index 1.
+- Pay 15 and climb two steps to reach the top.
+The total cost is 15.
+
+ +

Example 2:

+ +
Input: cost = [1,100,1,1,1,100,1,1,100,1]
+Output: 6
+Explanation: You will start at index 0.
+- Pay 1 and climb two steps to reach index 2.
+- Pay 1 and climb two steps to reach index 4.
+- Pay 1 and climb two steps to reach index 6.
+- Pay 1 and climb one step to reach index 7.
+- Pay 1 and climb two steps to reach index 9.
+- Pay 1 and climb one step to reach the top.
+The total cost is 6.
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= cost.length <= 1000
  • +
  • 0 <= cost[i] <= 999
  • +
+
\ No newline at end of file From 48c4e833fdbdaa6e57c0ead3b04be74417a3b55c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 13 Oct 2023 20:51:27 +0530 Subject: [PATCH 0144/1399] Attach NOTES - LeetHub --- 0746-min-cost-climbing-stairs/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0746-min-cost-climbing-stairs/NOTES.md diff --git a/0746-min-cost-climbing-stairs/NOTES.md b/0746-min-cost-climbing-stairs/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0746-min-cost-climbing-stairs/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 41a078bb63f002c7b26b8a5e44d3865be32436a8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 13 Oct 2023 20:51:30 +0530 Subject: [PATCH 0145/1399] Time: 8 ms (20.63%), Space: 14 MB (26.23%) - LeetHub --- .../0746-min-cost-climbing-stairs.cpp | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 0746-min-cost-climbing-stairs/0746-min-cost-climbing-stairs.cpp diff --git a/0746-min-cost-climbing-stairs/0746-min-cost-climbing-stairs.cpp b/0746-min-cost-climbing-stairs/0746-min-cost-climbing-stairs.cpp new file mode 100644 index 00000000..f2dbaf0d --- /dev/null +++ b/0746-min-cost-climbing-stairs/0746-min-cost-climbing-stairs.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + + int helper(int idx, int n, vector& cost, vector& dp) + { + if(idx >= n) + return 0; + + if(dp[idx] != -1) + return dp[idx]; + + int oneStep = cost[idx] + helper(idx+1, n, cost, dp); + int twoStep = cost[idx] + helper(idx+2, n, cost, dp); + + return dp[idx] = min(oneStep, twoStep); + } + + int minCostClimbingStairs(vector& cost) { + + int n = cost.size(); + + vector dp(n+1, -1); + + return min(helper(0, n, cost, dp), helper(1, n, cost, dp)); + + } +}; \ No newline at end of file From d483527997a5dbe9e8ba05b91d4932ff1afbcda4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 13 Oct 2023 20:51:53 +0530 Subject: [PATCH 0146/1399] Attach NOTES - LeetHub From 8ae85ddbc04f9489792af1a0f9bb2ed8df29bdc0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 13 Oct 2023 20:51:57 +0530 Subject: [PATCH 0147/1399] Time: 8 ms (20.63%), Space: 14 MB (26.23%) - LeetHub From f0b5fb4e7794eef3684bf22d82de1c20dca211ff Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 13 Oct 2023 21:02:03 +0530 Subject: [PATCH 0148/1399] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div746-min-cost-climbing-stairs/NOTES.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div746-min-cost-climbing-stairs/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div746-min-cost-climbing-stairs/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div746-min-cost-climbing-stairs/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 00a44c3f58c343ef6ea01efbd3de30e5930d5ebe Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 13 Oct 2023 21:02:06 +0530 Subject: [PATCH 0149/1399] Time: 4 ms (61.59%), Space: 14 MB (37.30%) - LeetHub --- ...iv-div-div746-min-cost-climbing-stairs.cpp | 25 ++++++------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div746-min-cost-climbing-stairs/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div746-min-cost-climbing-stairs.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div746-min-cost-climbing-stairs/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div746-min-cost-climbing-stairs.cpp index f2dbaf0d..383cab45 100644 --- a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div746-min-cost-climbing-stairs/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div746-min-cost-climbing-stairs.cpp +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div746-min-cost-climbing-stairs/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div746-min-cost-climbing-stairs.cpp @@ -1,27 +1,18 @@ class Solution { public: - - int helper(int idx, int n, vector& cost, vector& dp) - { - if(idx >= n) - return 0; - - if(dp[idx] != -1) - return dp[idx]; - - int oneStep = cost[idx] + helper(idx+1, n, cost, dp); - int twoStep = cost[idx] + helper(idx+2, n, cost, dp); - - return dp[idx] = min(oneStep, twoStep); - } - int minCostClimbingStairs(vector& cost) { int n = cost.size(); - vector dp(n+1, -1); + vector dp(n+1, 0); - return min(helper(0, n, cost, dp), helper(1, n, cost, dp)); + for(int i = 2; i <= n; ++i) + { + int oneStep = cost[i-1] + dp[i-1]; + int twoStep = cost[i-2] + dp[i-2]; + dp[i] = min(oneStep, twoStep); + } + return dp[n]; } }; \ No newline at end of file From bed86c54ca8c338da8c07c0604fcb54730efe3fc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 14 Oct 2023 22:19:21 +0530 Subject: [PATCH 0150/1399] Create README - LeetHub --- Find Common Nodes in two BSTs - GFG/README.md | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 Find Common Nodes in two BSTs - GFG/README.md diff --git a/Find Common Nodes in two BSTs - GFG/README.md b/Find Common Nodes in two BSTs - GFG/README.md new file mode 100644 index 00000000..be9f1877 --- /dev/null +++ b/Find Common Nodes in two BSTs - GFG/README.md @@ -0,0 +1,40 @@ +# Find Common Nodes in two BSTs +## Easy +

Given two Binary Search Trees. Find the nodes that are common in both of them, ie- find the intersection of the two BSTs.

+

Note: Return the common nodes in sorted order.

+

Example 1:

+
Input:
+BST1:
+                  5
+               /     \
+             1        10
+           /   \      /
+          0     4    7
+                      \
+                       9
+BST2:
+                10 
+              /    \
+             7     20
+           /   \ 
+          4     9
+Output: 4 7 9 10
+
+
+

Example 2:

+
Input:
+BST1:
+     10
+    /  \
+   2   11
+  /  \
+ 1   3
+BST2:
+       2
+     /  \
+    1    3
+Output: 1 2 3
+
+

Your Task:
You don't need to read input or print anything. Your task is to complete the function findCommon() that takes roots of the two BSTs as input parameters and returns a list of integers containing the common nodes in sorted order. 

+

Expected Time Complexity: O(N1 + N2) where N1 and N2 are the sizes of the 2 BSTs.
Expected Auxiliary Space: O(H1 + H2) where H1 and H2 are the heights of the 2 BSTs.

+

Constraints:
1 <= Number of Nodes <= 105
1 <= Node data <= 109

\ No newline at end of file From b8bfc2ace00229d9ac2f9f8031852e71492a18e4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 14 Oct 2023 22:19:22 +0530 Subject: [PATCH 0151/1399] Added solution - LeetHub --- .../find-common-nodes-in-two-bsts.cpp | 149 ++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 Find Common Nodes in two BSTs - GFG/find-common-nodes-in-two-bsts.cpp diff --git a/Find Common Nodes in two BSTs - GFG/find-common-nodes-in-two-bsts.cpp b/Find Common Nodes in two BSTs - GFG/find-common-nodes-in-two-bsts.cpp new file mode 100644 index 00000000..d57d7cf2 --- /dev/null +++ b/Find Common Nodes in two BSTs - GFG/find-common-nodes-in-two-bsts.cpp @@ -0,0 +1,149 @@ +//{ Driver Code Starts +#include +using namespace std; + +// Tree Node +struct Node { + int data; + Node *left; + Node *right; + + Node(int val) { + data = val; + left = right = NULL; + } +}; + +// Function to Build Tree +Node* buildTree(string str) +{ + // Corner Case + if(str.length() == 0 || str[0] == 'N') + return NULL; + + // Creating vector of strings from input + // string after spliting by space + vector ip; + + istringstream iss(str); + for(string str; iss >> str; ) + ip.push_back(str); + + // Create the root of the tree + Node* root = new Node(stoi(ip[0])); + + // Push the root to the queue + queue queue; + queue.push(root); + + // Starting from the second element + int i = 1; + while(!queue.empty() && i < ip.size()) { + + // Get and remove the front of the queue + Node* currNode = queue.front(); + queue.pop(); + + // Get the current node's value from the string + string currVal = ip[i]; + + // If the left child is not null + if(currVal != "N") { + + // Create the left child for the current node + currNode->left = new Node(stoi(currVal)); + + // Push it to the queue + queue.push(currNode->left); + } + + // For the right child + i++; + if(i >= ip.size()) + break; + currVal = ip[i]; + + // If the right child is not null + if(currVal != "N") { + + // Create the right child for the current node + currNode->right = new Node(stoi(currVal)); + + // Push it to the queue + queue.push(currNode->right); + } + i++; + } + + return root; +} + + +// } Driver Code Ends + + +class Solution +{ + public: + //Function to find the nodes that are common in both BST. + + map mp; + + void helper(Node* root) + { + if(root) + { + helper(root->left); + ++mp[root->data]; + helper(root->right); + } + } + + vector findCommon(Node *root1, Node *root2) + { + //Your code here + + helper(root1); + helper(root2); + + vector ans; + + for(auto& itr : mp) + { + if(itr.second > 1) + ans.push_back(itr.first); + } + + return ans; + + } +}; + + + + +//{ Driver Code Starts. + +int main() +{ + int t; + cin>>t; + getchar(); + while(t--) + { + string s; + getline(cin,s); + Node* root1 = buildTree(s); + + getline(cin,s); + Node* root2 = buildTree(s); + Solution ob; + vector res = ob.findCommon(root1, root2); + for (int i : res) + cout << i << " "; + cout<< endl; + } + + return 1; +} +// } Driver Code Ends \ No newline at end of file From 6df3c17ae511eca4937ea465a35ed73fdce63e26 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 14 Oct 2023 23:06:50 +0530 Subject: [PATCH 0152/1399] Create README - LeetHub --- .../README.md | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2742-painting-the-walls/README.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2742-painting-the-walls/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2742-painting-the-walls/README.md new file mode 100644 index 00000000..b6396262 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2742-painting-the-walls/README.md @@ -0,0 +1,34 @@ +

No companies found for this problem
2742. Painting the Walls

Hard


You are given two 0-indexed integer arrays, cost and time, of size n representing the costs and the time taken to paint n different walls respectively. There are two painters available:

+ +
    +
  • A paid painter that paints the ith wall in time[i] units of time and takes cost[i] units of money.
  • +
  • A free painter that paints any wall in 1 unit of time at a cost of 0. But the free painter can only be used if the paid painter is already occupied.
  • +
+ +

Return the minimum amount of money required to paint the n walls.

+ +

 

+

Example 1:

+ +
Input: cost = [1,2,3,2], time = [1,2,3,2]
+Output: 3
+Explanation: The walls at index 0 and 1 will be painted by the paid painter, and it will take 3 units of time; meanwhile, the free painter will paint the walls at index 2 and 3, free of cost in 2 units of time. Thus, the total cost is 1 + 2 = 3.
+
+ +

Example 2:

+ +
Input: cost = [2,3,4,2], time = [1,1,1,1]
+Output: 4
+Explanation: The walls at index 0 and 3 will be painted by the paid painter, and it will take 2 units of time; meanwhile, the free painter will paint the walls at index 1 and 2, free of cost in 2 units of time. Thus, the total cost is 2 + 2 = 4.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= cost.length <= 500
  • +
  • cost.length == time.length
  • +
  • 1 <= cost[i] <= 106
  • +
  • 1 <= time[i] <= 500
  • +
+
\ No newline at end of file From 0915cf8584b48e13396f5f6d65c0c3419fd0f061 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 14 Oct 2023 23:06:50 +0530 Subject: [PATCH 0153/1399] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2742-painting-the-walls/NOTES.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2742-painting-the-walls/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2742-painting-the-walls/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2742-painting-the-walls/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 0e5993cfc82726703b82392c969e0231f5288077 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 14 Oct 2023 23:07:36 +0530 Subject: [PATCH 0154/1399] Attach NOTES - LeetHub From b3648e3a63730998e99a25b10ec19e3858e8715d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 15 Oct 2023 20:16:11 +0530 Subject: [PATCH 0155/1399] Create README - LeetHub --- Normal BST to Balanced BST - GFG/README.md | 30 ++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Normal BST to Balanced BST - GFG/README.md diff --git a/Normal BST to Balanced BST - GFG/README.md b/Normal BST to Balanced BST - GFG/README.md new file mode 100644 index 00000000..4a51938e --- /dev/null +++ b/Normal BST to Balanced BST - GFG/README.md @@ -0,0 +1,30 @@ +# Normal BST to Balanced BST +## Easy +

Given a Binary Search Tree, modify the given BST such that it is balanced and has minimum possible height. Return the balanced BST.

+

Example1:

+
Input:
+       30
+      /
+     20
+    /
+   10
Output: + 20 + / \ + 10 30 +
+

Example2:

+
Input:
+         4
+        /
+       3
+      /
+     2
+    /
+   1
+Output:
+      3            3           2
+    /  \         /  \        /  \
+   1    4   OR  2    4  OR  1    3   
+    \          /                  \ 
2 1 4
+


Your Task:

The task is to complete the function buildBalancedTree() which takes root as the input argument and returns the root of tree after converting the given BST into a balanced BST with minimum possible height. The driver code will print the height of the updated tree in output itself.

 
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Here N denotes total number of nodes in given BST.

+

Constraints:
1 <= N <= 105
1 <= Node data <= 109

\ No newline at end of file From ec04f52ca1c8f4c879dbaed7d2af7a11bff87a35 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 15 Oct 2023 20:16:12 +0530 Subject: [PATCH 0156/1399] Added solution - LeetHub --- .../normal-bst-to-balanced-bst.cpp | 186 ++++++++++++++++++ 1 file changed, 186 insertions(+) create mode 100644 Normal BST to Balanced BST - GFG/normal-bst-to-balanced-bst.cpp diff --git a/Normal BST to Balanced BST - GFG/normal-bst-to-balanced-bst.cpp b/Normal BST to Balanced BST - GFG/normal-bst-to-balanced-bst.cpp new file mode 100644 index 00000000..0a4ca566 --- /dev/null +++ b/Normal BST to Balanced BST - GFG/normal-bst-to-balanced-bst.cpp @@ -0,0 +1,186 @@ +//{ Driver Code Starts +#include +using namespace std; + +struct Node +{ + int data; + struct Node *left; + struct Node *right; + + Node(int x){ + data = x; + left = NULL; + right = NULL; + } +}; + + +// } Driver Code Ends +/*Structure of the Node of the BST is as +struct Node +{ + int data; + Node* left, *right; +}; +*/ + +class Solution{ + + public: + // Your are required to complete this function + // function should return root of the modified BST + + vector inorder; + + void helper(Node* root) + { + if(root) + { + helper(root->left); + inorder.push_back(root->data); + helper(root->right); + } + } + + Node* BST(int start, int end) + { + if(start <= end) + { + int mid = (start + end) >> 1; + + Node* root = new Node(inorder[mid]); + root->left = BST(start, mid-1); + root->right = BST(mid+1, end); + return root; + } + + return nullptr; + } + Node* buildBalancedTree(Node* root) + { + // Code here + + helper(root); + + return BST(0, inorder.size()-1); + + } +}; + + +//{ Driver Code Starts. + +Node* insert(struct Node* node, int key){ + if (node == NULL) return new Node(key); + if (key < node->data) + node->left = insert(node->left, key); + else if (key > node->data) + node->right = insert(node->right, key); + return node; +} + +void preOrder(Node* node) +{ + if (node == NULL)return; + printf("%d ", node->data); + preOrder(node->left); + preOrder(node->right); +} + +int height(struct Node* node) +{ + if (node==NULL) + return 0; + int lDepth = height(node->left); + int rDepth = height(node->right); + if (lDepth > rDepth) + return(lDepth+1); + else + return(rDepth+1); +} +Node *buildTree(string str) { + // Corner Case + if (str.length() == 0 || str[0] == 'N') + return NULL; + + // Creating vector of strings from input + // string after spliting by space + vector ip; + + istringstream iss(str); + for (string str; iss >> str;) + ip.push_back(str); + + // Create the root of the tree + Node *root = new Node(stoi(ip[0])); + + // Push the root to the queue + queue queue; + queue.push(root); + + // Starting from the second element + int i = 1; + while (!queue.empty() && i < ip.size()) { + + // Get and remove the front of the queue + Node *currNode = queue.front(); + queue.pop(); + + // Get the current node's value from the string + string currVal = ip[i]; + + // If the left child is not null + if (currVal != "N") { + + // Create the left child for the current node + currNode->left = new Node(stoi(currVal)); + + // Push it to the queue + queue.push(currNode->left); + } + + // For the right child + i++; + if (i >= ip.size()) + break; + currVal = ip[i]; + + // If the right child is not null + if (currVal != "N") { + + // Create the right child for the current node + currNode->right = new Node(stoi(currVal)); + + // Push it to the queue + queue.push(currNode->right); + } + i++; + } + + return root; +} + +Node* buildBalancedTree(Node* root); + +int main() +{ + int t; + cin>>t; + getchar(); + while(t--) + { + struct Node *root = NULL; + int n, temp; + string tree; + getline(cin,tree); + root = buildTree(tree); + // cout< Date: Sun, 15 Oct 2023 21:13:29 +0530 Subject: [PATCH 0157/1399] Create README - LeetHub --- .../README.md | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1269-number-of-ways-to-stay-in-the-same-place-after-some-steps/README.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1269-number-of-ways-to-stay-in-the-same-place-after-some-steps/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1269-number-of-ways-to-stay-in-the-same-place-after-some-steps/README.md new file mode 100644 index 00000000..ad55ddcb --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1269-number-of-ways-to-stay-in-the-same-place-after-some-steps/README.md @@ -0,0 +1,39 @@ +

No companies found for this problem
1269. Number of Ways to Stay in the Same Place After Some Steps

Hard


You have a pointer at index 0 in an array of size arrLen. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).

+ +

Given two integers steps and arrLen, return the number of ways such that your pointer is still at index 0 after exactly steps steps. Since the answer may be too large, return it modulo 109 + 7.

+ +

 

+

Example 1:

+ +
Input: steps = 3, arrLen = 2
+Output: 4
+Explanation: There are 4 differents ways to stay at index 0 after 3 steps.
+Right, Left, Stay
+Stay, Right, Left
+Right, Stay, Left
+Stay, Stay, Stay
+
+ +

Example 2:

+ +
Input: steps = 2, arrLen = 4
+Output: 2
+Explanation: There are 2 differents ways to stay at index 0 after 2 steps
+Right, Left
+Stay, Stay
+
+ +

Example 3:

+ +
Input: steps = 4, arrLen = 2
+Output: 8
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= steps <= 500
  • +
  • 1 <= arrLen <= 106
  • +
+
\ No newline at end of file From 1078b5f27107a10f9a4217c730ad734a020fe8c9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 15 Oct 2023 21:13:33 +0530 Subject: [PATCH 0158/1399] Time: 29 ms (34.85%), Space: 13.8 MB (35.28%) - LeetHub --- ...tay-in-the-same-place-after-some-steps.cpp | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1269-number-of-ways-to-stay-in-the-same-place-after-some-steps/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1269-number-of-ways-to-stay-in-the-same-place-after-some-steps.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1269-number-of-ways-to-stay-in-the-same-place-after-some-steps/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1269-number-of-ways-to-stay-in-the-same-place-after-some-steps.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1269-number-of-ways-to-stay-in-the-same-place-after-some-steps/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1269-number-of-ways-to-stay-in-the-same-place-after-some-steps.cpp new file mode 100644 index 00000000..5c0ea1e2 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1269-number-of-ways-to-stay-in-the-same-place-after-some-steps/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1269-number-of-ways-to-stay-in-the-same-place-after-some-steps.cpp @@ -0,0 +1,35 @@ +class Solution { +public: + + const int mod = 1e9+7; + + int helper(int idx, int steps, int arrLen, vector>& dp) + { + if(steps == 0 and idx == 0) + { + return 1; + } + + if(idx < 0 or idx >= arrLen or (idx != 0 and steps == 0)) + return 0; + + if(dp[idx][steps] != -1) + return dp[idx][steps]; + + int stay = helper(idx, steps-1, arrLen, dp); + + int left = helper(idx-1, steps-1, arrLen, dp); + + int right = helper(idx+1, steps-1, arrLen, dp); + + return dp[idx][steps] = ((((stay)%mod + left)%mod + right)%mod)%mod; + } + + int numWays(int steps, int arrLen) { + + vector> dp(steps+1, vector(steps+1, -1)); + + return helper(0, steps, arrLen, dp); + + } +}; \ No newline at end of file From 999c0d95548d9921468936752474faf656fd6692 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 17 Oct 2023 00:10:56 +0530 Subject: [PATCH 0159/1399] Create README - LeetHub --- 0119-pascals-triangle-ii/README.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 0119-pascals-triangle-ii/README.md diff --git a/0119-pascals-triangle-ii/README.md b/0119-pascals-triangle-ii/README.md new file mode 100644 index 00000000..2c0ffdfb --- /dev/null +++ b/0119-pascals-triangle-ii/README.md @@ -0,0 +1,25 @@ +

119. Pascal's Triangle II

Easy


Given an integer rowIndex, return the rowIndexth (0-indexed) row of the Pascal's triangle.

+ +

In Pascal's triangle, each number is the sum of the two numbers directly above it as shown:

+ +

 

+

Example 1:

+
Input: rowIndex = 3
+Output: [1,3,3,1]
+

Example 2:

+
Input: rowIndex = 0
+Output: [1]
+

Example 3:

+
Input: rowIndex = 1
+Output: [1,1]
+
+

 

+

Constraints:

+ +
    +
  • 0 <= rowIndex <= 33
  • +
+ +

 

+

Follow up: Could you optimize your algorithm to use only O(rowIndex) extra space?

+
\ No newline at end of file From 10342f4eb37547cc0bbe5763a04752186cbedc3c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 17 Oct 2023 00:11:00 +0530 Subject: [PATCH 0160/1399] Time: 0 ms (100.00%), Space: 7.5 MB (7.53%) - LeetHub --- .../0119-pascals-triangle-ii.cpp | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 0119-pascals-triangle-ii/0119-pascals-triangle-ii.cpp diff --git a/0119-pascals-triangle-ii/0119-pascals-triangle-ii.cpp b/0119-pascals-triangle-ii/0119-pascals-triangle-ii.cpp new file mode 100644 index 00000000..e4855cf8 --- /dev/null +++ b/0119-pascals-triangle-ii/0119-pascals-triangle-ii.cpp @@ -0,0 +1,34 @@ +class Solution { +public: + vector getRow(int rowIndex) { + + vector> v(rowIndex); + + if(rowIndex == 0) + return {1}; + else if(rowIndex == 1) + return {1, 1}; + else + { + v.push_back({1}); + v.push_back({1, 1}); + + for(int i = 2; i <= rowIndex; ++i) + { + vector curr; + curr.push_back(1); + + for(int j =1; j < v.back().size(); ++j) + { + curr.push_back(v.back()[j] + v.back()[j-1]); + } + + curr.push_back(1); + + v.push_back(curr); + } + return v.back(); + } + + } +}; \ No newline at end of file From 3233ccc272ba8f3b6529ad57ece43c019284dafe Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 17 Oct 2023 00:12:43 +0530 Subject: [PATCH 0161/1399] Attach NOTES - LeetHub --- 0119-pascals-triangle-ii/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0119-pascals-triangle-ii/NOTES.md diff --git a/0119-pascals-triangle-ii/NOTES.md b/0119-pascals-triangle-ii/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0119-pascals-triangle-ii/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 400d7ec3f70dc431e1e98eb94af474879a3a82ac Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 17 Oct 2023 00:12:46 +0530 Subject: [PATCH 0162/1399] Time: 0 ms (100.00%), Space: 7.5 MB (7.53%) - LeetHub From b497f342dae20303fc74d64edb179647651220ac Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 17 Oct 2023 00:20:08 +0530 Subject: [PATCH 0163/1399] Create README - LeetHub --- .../README.md | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div119-pascals-triangle-ii/README.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div119-pascals-triangle-ii/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div119-pascals-triangle-ii/README.md new file mode 100644 index 00000000..e5747c89 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div119-pascals-triangle-ii/README.md @@ -0,0 +1,25 @@ +

Microsoft
2
Goldman Sachs
2
119. Pascal's Triangle II

Easy


Given an integer rowIndex, return the rowIndexth (0-indexed) row of the Pascal's triangle.

+ +

In Pascal's triangle, each number is the sum of the two numbers directly above it as shown:

+ +

 

+

Example 1:

+
Input: rowIndex = 3
+Output: [1,3,3,1]
+

Example 2:

+
Input: rowIndex = 0
+Output: [1]
+

Example 3:

+
Input: rowIndex = 1
+Output: [1,1]
+
+

 

+

Constraints:

+ +
    +
  • 0 <= rowIndex <= 33
  • +
+ +

 

+

Follow up: Could you optimize your algorithm to use only O(rowIndex) extra space?

+
\ No newline at end of file From ac8b38ebe224babcc919fc4566e482d43fe1dbbf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 17 Oct 2023 00:20:12 +0530 Subject: [PATCH 0164/1399] Time: 0 ms (100.00%), Space: 7 MB (11.01%) - LeetHub --- ...svg-div-div-div-div119-pascals-triangle-ii.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div119-pascals-triangle-ii/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div119-pascals-triangle-ii.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div119-pascals-triangle-ii/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div119-pascals-triangle-ii.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div119-pascals-triangle-ii/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div119-pascals-triangle-ii.cpp new file mode 100644 index 00000000..942b1785 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div119-pascals-triangle-ii/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div119-pascals-triangle-ii.cpp @@ -0,0 +1,15 @@ +class Solution { +public: + vector getRow(int rowIndex) { + + vector res(rowIndex+1, 1); + + for(int i = 0; i < rowIndex; ++i) + { + for(int j = i; j > 0; --j) + res[j] += res[j-1]; + } + return res; + } +}; + From d31a401ddef32c1495ad426c6a843454e68c1a37 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 17 Oct 2023 21:11:39 +0530 Subject: [PATCH 0165/1399] Create README - LeetHub --- Transitive closure of a Graph - GFG/README.md | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Transitive closure of a Graph - GFG/README.md diff --git a/Transitive closure of a Graph - GFG/README.md b/Transitive closure of a Graph - GFG/README.md new file mode 100644 index 00000000..b38a80ce --- /dev/null +++ b/Transitive closure of a Graph - GFG/README.md @@ -0,0 +1,26 @@ +# Transitive closure of a Graph +## Medium +

Given a directed graph, determine whether a vertex j is reachable from another vertex i for all vertex pairs (i, j) in the given graph. Here, vertex j is reachable from another vertex i means that there is a path from vertex i to j. The reachability matrix is called the transitive closure of a graph. The directed graph is represented by an adjacency matrix where there are N vertices. 

+

Example 1:

+
Input: N = 4
+graph = {{1, 1, 0, 1}, 
+         {0, 1, 1, 0}, 
+         {0, 0, 1, 1}, 
+         {0, 0, 0, 1}}
+Output: {{1, 1, 1, 1}, 
+         {0, 1, 1, 1}, 
+         {0, 0, 1, 1}, 
+         {0, 0, 0, 1}}
+Explanation: 
The output list shows the reachable indexes.
+

Example 2:

+
Input: N = 3
+graph = {{1, 0, 0}, 
+         {0, 1, 0}, 
+         {0, 0, 1}}
+Output: {{1, 0, 0}, 
+         {0, 1, 0}, 
+         {0, 0, 1}}
+Explanation: 
The output list shows the reachable indexes.
+

Your Task:
You do not need to read input or print anything. Your task is to complete the function transitiveClosure() which takes an integer N and a 2D array graph(adjacency matrix of the graph) as input parameters and returns the transitive closure of the graph in the form of 2D array.

+

Expected Time Complexity: O(N3)
Expected Auxiliary Space: O(N2)

+

Constraints:
1 ≤ N ≤ 100  

\ No newline at end of file From 75f778f87e2b71db94c2b53488769290ef35be40 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 17 Oct 2023 21:11:40 +0530 Subject: [PATCH 0166/1399] Added solution - LeetHub --- .../transitive-closure-of-a-graph.cpp | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 Transitive closure of a Graph - GFG/transitive-closure-of-a-graph.cpp diff --git a/Transitive closure of a Graph - GFG/transitive-closure-of-a-graph.cpp b/Transitive closure of a Graph - GFG/transitive-closure-of-a-graph.cpp new file mode 100644 index 00000000..84151909 --- /dev/null +++ b/Transitive closure of a Graph - GFG/transitive-closure-of-a-graph.cpp @@ -0,0 +1,75 @@ +//{ Driver Code Starts +// Initial Template for C++ + +#include +using namespace std; + +// } Driver Code Ends +// Back-end complete function Template for C++ + +class Solution{ +public: + + void dfs(int node, int sv, vector adj[], vector& visited, vector>& res) + { + visited[sv] = true; + res[node][sv] = true; + + for(auto& itr : adj[sv]) + { + if(!visited[itr]) + dfs(node, itr, adj, visited, res); + } + } + + vector> transitiveClosure(int N, vector> graph) + { + // code here + + vector adj[N+1]; + + for(int i = 0; i < N; ++i) + { + for(int j = 0; j < N; ++j) + { + if(graph[i][j] == 1) + adj[i].push_back(j); + } + } + + vector> res(N, vector(N, 0)); + + for(int i = 0; i < N; ++i) + { + vector visited(N, false); + dfs(i, i, adj, visited, res); + } + + return res; + + } +}; + +//{ Driver Code Starts. + +int main(){ + int t; + cin>>t; + while(t--){ + int N; + cin>>N; + vector> graph(N, vector(N, -1)); + for(int i = 0;i < N;i++) + for(int j=0;j>graph[i][j]; + + Solution ob; + vector> ans = ob.transitiveClosure(N, graph); + for(int i = 0;i < N;i++) + {for(int j = 0;j < N;j++) + cout< Date: Tue, 17 Oct 2023 22:30:20 +0530 Subject: [PATCH 0167/1399] Create README - LeetHub --- .../README.md | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1361-validate-binary-tree-nodes/README.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1361-validate-binary-tree-nodes/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1361-validate-binary-tree-nodes/README.md new file mode 100644 index 00000000..6f7ec432 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1361-validate-binary-tree-nodes/README.md @@ -0,0 +1,34 @@ +

Facebook
2
1361. Validate Binary Tree Nodes

Medium


You have n binary tree nodes numbered from 0 to n - 1 where node i has two children leftChild[i] and rightChild[i], return true if and only if all the given nodes form exactly one valid binary tree.

+ +

If node i has no left child then leftChild[i] will equal -1, similarly for the right child.

+ +

Note that the nodes have no values and that we only use the node numbers in this problem.

+ +

 

+

Example 1:

+ +
Input: n = 4, leftChild = [1,-1,3,-1], rightChild = [2,-1,-1,-1]
+Output: true
+
+ +

Example 2:

+ +
Input: n = 4, leftChild = [1,-1,3,-1], rightChild = [2,3,-1,-1]
+Output: false
+
+ +

Example 3:

+ +
Input: n = 2, leftChild = [1,0], rightChild = [-1,-1]
+Output: false
+
+ +

 

+

Constraints:

+ +
    +
  • n == leftChild.length == rightChild.length
  • +
  • 1 <= n <= 104
  • +
  • -1 <= leftChild[i], rightChild[i] <= n - 1
  • +
+
\ No newline at end of file From dd51f2c6654090d3956c0758e09f662cc366fa50 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 17 Oct 2023 22:30:23 +0530 Subject: [PATCH 0168/1399] Time: 39 ms (59.88%), Space: 34.1 MB (51.67%) - LeetHub --- ...div-div1361-validate-binary-tree-nodes.cpp | 115 ++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1361-validate-binary-tree-nodes/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1361-validate-binary-tree-nodes.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1361-validate-binary-tree-nodes/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1361-validate-binary-tree-nodes.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1361-validate-binary-tree-nodes/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1361-validate-binary-tree-nodes.cpp new file mode 100644 index 00000000..12b70529 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1361-validate-binary-tree-nodes/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1361-validate-binary-tree-nodes.cpp @@ -0,0 +1,115 @@ +class DSU{ + private: + vector parent, size, rank; + public: + DSU(int n) + { + rank.resize(n+1, 0); + size.resize(n+1, 1); + parent.resize(n+1); + + for(int i = 0; i <= n; ++i) + parent[i] = i; + } + + int findParent(int u) + { + if(parent[u] == u) + return u; + return parent[u] = findParent(parent[u]); + } + + void unionByRank(int u, int v) + { + int parU = findParent(u); + int parV = findParent(v); + + if(parU == parV) + return; + + if(rank[parU] < rank[parV]) + { + parent[parU] = parV; + } + else if(rank[parV] < rank[parU]) + { + parent[parV] = parU; + } + else + { + parent[parV] = parU; + ++rank[parU]; + } + } + + void unionBySize(int u, int v) + { + int parU = findParent(u); + int parV = findParent(v); + + if(parU == parV) + return; + + if(size[parU] < size[parV]) + { + parent[parU] = parV; + size[parV] += size[parU]; + } + else + { + parent[parV] = parU; + size[parU] += size[parV]; + } + } + + bool isSame(int u, int v) + { + return findParent(u) == findParent(v); + } + +}; + + +class Solution { +public: + bool validateBinaryTreeNodes(int n, vector& leftChild, vector& rightChild) { + + vector indegree(n+1, 0); + + DSU dsu(n); + + for(int i = 0; i < n; ++i) + { + if(leftChild[i] != -1 and ++indegree[leftChild[i]] > 1) + return false; + if(rightChild[i] != -1 and ++indegree[rightChild[i]] > 1) + return false; + + if(leftChild[i] != -1) + { + if(dsu.findParent(i) == dsu.findParent(leftChild[i])) + return false; + else + dsu.unionBySize(i, leftChild[i]); + } + + if(rightChild[i] != -1) + { + if(dsu.findParent(i) == dsu.findParent(rightChild[i])) + return false; + else + dsu.unionBySize(i, rightChild[i]); + } + } + + int cnt = 0; + + for(int i = 0; i < n; ++i) + { + if(dsu.findParent(i) == i) + ++cnt; + } + + return cnt == 1; + } +}; \ No newline at end of file From 5f491b9f748dfc6a7144fe6fb1e4ce1aa115d97c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 17 Oct 2023 22:30:46 +0530 Subject: [PATCH 0169/1399] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1361-validate-binary-tree-nodes/NOTES.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1361-validate-binary-tree-nodes/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1361-validate-binary-tree-nodes/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1361-validate-binary-tree-nodes/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From cc1e4c4f42dfe565914dbd39de6de0562f77a15d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 17 Oct 2023 22:30:50 +0530 Subject: [PATCH 0170/1399] Time: 35 ms (72.95%), Space: 34.2 MB (51.67%) - LeetHub --- ...ect-svg-div-div-div-div1361-validate-binary-tree-nodes.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1361-validate-binary-tree-nodes/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1361-validate-binary-tree-nodes.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1361-validate-binary-tree-nodes/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1361-validate-binary-tree-nodes.cpp index 12b70529..5549c5cf 100644 --- a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1361-validate-binary-tree-nodes/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1361-validate-binary-tree-nodes.cpp +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1361-validate-binary-tree-nodes/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1361-validate-binary-tree-nodes.cpp @@ -90,7 +90,7 @@ class Solution { if(dsu.findParent(i) == dsu.findParent(leftChild[i])) return false; else - dsu.unionBySize(i, leftChild[i]); + dsu.unionByRank(i, leftChild[i]); } if(rightChild[i] != -1) @@ -98,7 +98,7 @@ class Solution { if(dsu.findParent(i) == dsu.findParent(rightChild[i])) return false; else - dsu.unionBySize(i, rightChild[i]); + dsu.unionByRank(i, rightChild[i]); } } From f2b7348dd2144adc93cca512e062a29adbd0e5d8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 18 Oct 2023 17:10:36 +0530 Subject: [PATCH 0171/1399] Create README - LeetHub --- Eventual Safe States - GFG/README.md | 51 ++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 Eventual Safe States - GFG/README.md diff --git a/Eventual Safe States - GFG/README.md b/Eventual Safe States - GFG/README.md new file mode 100644 index 00000000..59c21c34 --- /dev/null +++ b/Eventual Safe States - GFG/README.md @@ -0,0 +1,51 @@ +# Eventual Safe States +## Medium +

A directed graph of V vertices and E edges is given in the form of an adjacency list adj. Each node of the graph is labelled with a distinct integer in the range 0 to V - 1.

+ +

A node is a terminal node if there are no outgoing edges. A node is a safe node if every possible path starting from that node leads to a terminal node.

+ +

You have to return an array containing all the safe nodes of the graph. The answer should be sorted in ascending order.

+ +

Example 1:

+ +
Input:
+
+
+Output:
+2 4 5 6
+Explanation:
+The given graph is shown above.
+Nodes 5 and 6 are terminal nodes as there are no 
+outgoing edges from either of them. 
+Every path starting at nodes 2, 4, 5, and 6 all 
+lead to either node 5 or 6.
+
+ +

Example 2:

+ +
Input:
+
+
+Output:
+3
+Explanation:
+Only node 3 is a terminal node, and every path 
+starting at node 3 leads to node 3.
+
+ +

Your Task:
+You don't need to read or print anything. Your task is to complete the function eventualSafeNodes() which takes an integer V denoting no. of vertices and adj denoting adjacency list of the graph and returns an array of safe nodes.

+ +

Expected Time Complexity: O(V + E)

+ +

Expected Space Complexity: O(V)

+ +

Constraints:

+ +
    +
  • 1 <= V <= 104
  • +
  • 0 <= E <= 104
  • +
  • The graph won't contain self loops.
  • +
  • Each node in the graph has a distinct value in the range 0 to V - 1.
  • +
+
\ No newline at end of file From 34ff6166ff07e36162ba3ae51d50d619ab093faa Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 18 Oct 2023 17:10:36 +0530 Subject: [PATCH 0172/1399] Added solution - LeetHub --- .../eventual-safe-states.cpp | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 Eventual Safe States - GFG/eventual-safe-states.cpp diff --git a/Eventual Safe States - GFG/eventual-safe-states.cpp b/Eventual Safe States - GFG/eventual-safe-states.cpp new file mode 100644 index 00000000..e6f97a13 --- /dev/null +++ b/Eventual Safe States - GFG/eventual-safe-states.cpp @@ -0,0 +1,95 @@ +//{ Driver Code Starts +// Initial Template for C++ + +#include +using namespace std; + + +// } Driver Code Ends +// User function Template for C++ + +class Solution { + public: + vector eventualSafeNodes(int V, vector adj[]) { + // code here + + + vector revAdj[V+1]; + + for(int i = 0; i < V; ++i) + { + for(auto& itr : adj[i]) + { + revAdj[itr].push_back(i); + } + } + + vector indegree(V+1,0); + + for(int i = 0; i < V; ++i) + { + for(auto& itr : revAdj[i]) + ++indegree[itr]; + } + + queue q; + + for(int i = 0; i < V; ++i) + { + if(indegree[i] == 0) + q.push(i); + } + + + vector safe; + + while(!q.empty()) + { + int curr = q.front(); + q.pop(); + + safe.push_back(curr); + + for(auto& itr : revAdj[curr]) + { + --indegree[itr]; + + if(indegree[itr] == 0) + q.push(itr); + } + } + + sort(safe.begin(), safe.end()); + + return safe; + } +}; + + +//{ Driver Code Starts. + +int main() { + int t; + cin >> t; + while (t--) { + + int V, E; + cin >> V >> E; + vector adj[V]; + + for (int i = 0; i < E; i++) { + int u, v; + cin >> u >> v; + adj[u].push_back(v); + } + + Solution obj; + vector safeNodes = obj.eventualSafeNodes(V, adj); + for (auto i : safeNodes) { + cout << i << " "; + } + cout << endl; + } +} + +// } Driver Code Ends \ No newline at end of file From cedbdaabb93fb233cd5abdab855585205cb8cd65 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 18 Oct 2023 23:49:25 +0530 Subject: [PATCH 0173/1399] Create README - LeetHub --- .../README.md | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divpinterest-divdiv-classcompanytagscontainer-tagoccurence4-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2050-parallel-courses-iii/README.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divpinterest-divdiv-classcompanytagscontainer-tagoccurence4-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2050-parallel-courses-iii/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divpinterest-divdiv-classcompanytagscontainer-tagoccurence4-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2050-parallel-courses-iii/README.md new file mode 100644 index 00000000..b26d9a0c --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divpinterest-divdiv-classcompanytagscontainer-tagoccurence4-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2050-parallel-courses-iii/README.md @@ -0,0 +1,53 @@ +

Pinterest
4
2050. Parallel Courses III

Hard


You are given an integer n, which indicates that there are n courses labeled from 1 to n. You are also given a 2D integer array relations where relations[j] = [prevCoursej, nextCoursej] denotes that course prevCoursej has to be completed before course nextCoursej (prerequisite relationship). Furthermore, you are given a 0-indexed integer array time where time[i] denotes how many months it takes to complete the (i+1)th course.

+ +

You must find the minimum number of months needed to complete all the courses following these rules:

+ +
    +
  • You may start taking a course at any time if the prerequisites are met.
  • +
  • Any number of courses can be taken at the same time.
  • +
+ +

Return the minimum number of months needed to complete all the courses.

+ +

Note: The test cases are generated such that it is possible to complete every course (i.e., the graph is a directed acyclic graph).

+ +

 

+

Example 1:

+ + +
Input: n = 3, relations = [[1,3],[2,3]], time = [3,2,5]
+Output: 8
+Explanation: The figure above represents the given graph and the time required to complete each course. 
+We start course 1 and course 2 simultaneously at month 0.
+Course 1 takes 3 months and course 2 takes 2 months to complete respectively.
+Thus, the earliest time we can start course 3 is at month 3, and the total time required is 3 + 5 = 8 months.
+
+ +

Example 2:

+ + +
Input: n = 5, relations = [[1,5],[2,5],[3,5],[3,4],[4,5]], time = [1,2,3,4,5]
+Output: 12
+Explanation: The figure above represents the given graph and the time required to complete each course.
+You can start courses 1, 2, and 3 at month 0.
+You can complete them after 1, 2, and 3 months respectively.
+Course 4 can be taken only after course 3 is completed, i.e., after 3 months. It is completed after 3 + 4 = 7 months.
+Course 5 can be taken only after courses 1, 2, 3, and 4 have been completed, i.e., after max(1,2,3,7) = 7 months.
+Thus, the minimum time needed to complete all the courses is 7 + 5 = 12 months.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= 5 * 104
  • +
  • 0 <= relations.length <= min(n * (n - 1) / 2, 5 * 104)
  • +
  • relations[j].length == 2
  • +
  • 1 <= prevCoursej, nextCoursej <= n
  • +
  • prevCoursej != nextCoursej
  • +
  • All the pairs [prevCoursej, nextCoursej] are unique.
  • +
  • time.length == n
  • +
  • 1 <= time[i] <= 104
  • +
  • The given graph is a directed acyclic graph.
  • +
+
\ No newline at end of file From d780ffe65c94dde68efd3a2a7583dd608759de27 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 18 Oct 2023 23:49:28 +0530 Subject: [PATCH 0174/1399] Time: 311 ms (77.70%), Space: 132 MB (71.58%) - LeetHub --- ...v-div-div-div2050-parallel-courses-iii.cpp | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divpinterest-divdiv-classcompanytagscontainer-tagoccurence4-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2050-parallel-courses-iii/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divpinterest-divdiv-classcompanytagscontainer-tagoccurence4-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2050-parallel-courses-iii.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divpinterest-divdiv-classcompanytagscontainer-tagoccurence4-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2050-parallel-courses-iii/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divpinterest-divdiv-classcompanytagscontainer-tagoccurence4-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2050-parallel-courses-iii.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divpinterest-divdiv-classcompanytagscontainer-tagoccurence4-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2050-parallel-courses-iii/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divpinterest-divdiv-classcompanytagscontainer-tagoccurence4-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2050-parallel-courses-iii.cpp new file mode 100644 index 00000000..92d4e476 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divpinterest-divdiv-classcompanytagscontainer-tagoccurence4-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2050-parallel-courses-iii/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divpinterest-divdiv-classcompanytagscontainer-tagoccurence4-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2050-parallel-courses-iii.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + int minimumTime(int n, vector>& E, vector& T) { + vector> G(n); + vector indegree(n), dist(n); + for (auto &e : E) { + G[e[0] - 1].push_back(e[1] - 1); + indegree[e[1] - 1]++; + } + queue q; + for (int i = 0; i < n; ++i) { + if (indegree[i] == 0) { + q.push(i); + dist[i] = T[i]; + } + } + while (q.size()) { + int u = q.front(); + q.pop(); + for (int v : G[u]) { + dist[v] = max(dist[u] + T[v], dist[v]); + if (--indegree[v] == 0) q.push(v); + } + } + return *max_element(begin(dist), end(dist)); + } +}; \ No newline at end of file From 5c20e121b396bba08d8de2aaa66120e4dcd935cf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 20 Oct 2023 19:34:12 +0530 Subject: [PATCH 0175/1399] Create README - LeetHub --- .../README.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 Form a number divisible by 3 using array digits - GFG/README.md diff --git a/Form a number divisible by 3 using array digits - GFG/README.md b/Form a number divisible by 3 using array digits - GFG/README.md new file mode 100644 index 00000000..cacbddfd --- /dev/null +++ b/Form a number divisible by 3 using array digits - GFG/README.md @@ -0,0 +1,19 @@ +# Form a number divisible by 3 using array digits +## Easy +

You will be given an array arr of integers of length N. You can construct an integer from two integers by treating the integers as strings, and then concatenating them. For example, 19 and 4 can be used to construct 194 and 419.

+

The task is to find whether it’s possible to construct an integer using all the digits of these numbers such that it would be divisible by 3.
If it is possible then print 1 and if not print 0.

+

Example 1:

+
Input: N = 3
+arr = {40, 50, 90}
+Output: 1
+Explanation: One such number is 405090.
+

Example 2:

+
Input: N = 2
+arr = {1, 4}
+Output: 0
+Explanation: The numbers we can form 
+are 14 and 41. But neither of them are 
+divisible by 3.
+

Your Task:
You do not need to read input or print anything. Your task is to complete the function isPossible() which takes N and arr as input parameters and returns 1 if we can form a number by the digits of the given number, that would be divisible by 3, otherwise returns 0.

+

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)

+

Constraints:
1 ≤ N, arr[i] ≤ 105

\ No newline at end of file From e276b0ace0fad4bfe19c02a6f57a446db9fde4f8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 20 Oct 2023 19:34:13 +0530 Subject: [PATCH 0176/1399] Added solution - LeetHub --- ...mber-divisible-by-3-using-array-digits.cpp | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 Form a number divisible by 3 using array digits - GFG/form-a-number-divisible-by-3-using-array-digits.cpp diff --git a/Form a number divisible by 3 using array digits - GFG/form-a-number-divisible-by-3-using-array-digits.cpp b/Form a number divisible by 3 using array digits - GFG/form-a-number-divisible-by-3-using-array-digits.cpp new file mode 100644 index 00000000..50b25f4d --- /dev/null +++ b/Form a number divisible by 3 using array digits - GFG/form-a-number-divisible-by-3-using-array-digits.cpp @@ -0,0 +1,42 @@ +//{ Driver Code Starts +// Initial Template for C++ + +#include +using namespace std; + +// } Driver Code Ends +// User function Template for C++ + +class Solution { + public: + int isPossible(int N, int arr[]) { + // code here + + int sum = 0; + + for(int i = 0; i < N; ++i) + { + sum += (arr[i] % 3); + } + + return (sum % 3 == 0 ? 1 : 0); + } +}; + +//{ Driver Code Starts. + +int main() { + int t; + cin >> t; + while (t--) { + int N; + cin >> N; + int arr[N]; + for (int i = 0; i < N; i++) cin >> arr[i]; + + Solution ob; + cout << ob.isPossible(N, arr) << endl; + } + return 0; +} +// } Driver Code Ends \ No newline at end of file From 529504adbb6bddd4fe7ce73c2179a11c2d1aca79 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 20 Oct 2023 19:40:17 +0530 Subject: [PATCH 0177/1399] Create README - LeetHub --- .../README.md | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divibm-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbookingcom-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvisa-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtiktok-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div844-backspace-string-compare/README.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divibm-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbookingcom-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvisa-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtiktok-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div844-backspace-string-compare/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divibm-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbookingcom-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvisa-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtiktok-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div844-backspace-string-compare/README.md new file mode 100644 index 00000000..f563b42e --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divibm-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbookingcom-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvisa-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtiktok-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div844-backspace-string-compare/README.md @@ -0,0 +1,37 @@ +

IBM
6
Google
3
Booking.com
3
Apple
3
Visa
3
Bloomberg
2
tiktok
2
844. Backspace String Compare

Easy


Given two strings s and t, return true if they are equal when both are typed into empty text editors. '#' means a backspace character.

+ +

Note that after backspacing an empty text, the text will continue empty.

+ +

 

+

Example 1:

+ +
Input: s = "ab#c", t = "ad#c"
+Output: true
+Explanation: Both s and t become "ac".
+
+ +

Example 2:

+ +
Input: s = "ab##", t = "c#d#"
+Output: true
+Explanation: Both s and t become "".
+
+ +

Example 3:

+ +
Input: s = "a#c", t = "b"
+Output: false
+Explanation: s becomes "c" while t becomes "b".
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length, t.length <= 200
  • +
  • s and t only contain lowercase letters and '#' characters.
  • +
+ +

 

+

Follow up: Can you solve it in O(n) time and O(1) space?

+
\ No newline at end of file From 28a514fbac2a05bc1f93a669f0ddc281eb1fea7f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 20 Oct 2023 19:40:20 +0530 Subject: [PATCH 0178/1399] Time: 0 ms (100.00%), Space: 6.4 MB (70.46%) - LeetHub --- ...iv-div-div844-backspace-string-compare.cpp | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divibm-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbookingcom-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvisa-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtiktok-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div844-backspace-string-compare/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divibm-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbookingcom-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvisa-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtiktok-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div844-backspace-string-compare.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divibm-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbookingcom-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvisa-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtiktok-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div844-backspace-string-compare/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divibm-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbookingcom-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvisa-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtiktok-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div844-backspace-string-compare.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divibm-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbookingcom-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvisa-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtiktok-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div844-backspace-string-compare/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divibm-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbookingcom-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvisa-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtiktok-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div844-backspace-string-compare.cpp new file mode 100644 index 00000000..0e7ac67b --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divibm-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbookingcom-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvisa-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtiktok-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div844-backspace-string-compare/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divibm-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbookingcom-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvisa-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtiktok-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div844-backspace-string-compare.cpp @@ -0,0 +1,32 @@ +class Solution { +public: + bool backspaceCompare(string s, string t) { + + string one, two; + + for(auto& itr : s) + { + if(itr == '#') + { + if(!one.empty()) + one.pop_back(); + } + else + one.push_back(itr); + } + + for(auto& itr : t) + { + if(itr == '#') + { + if(!two.empty()) + two.pop_back(); + } + else + two.push_back(itr); + } + + return one == two; + + } +}; \ No newline at end of file From 1940beebfbc31e8fb98436fd79ad26965a77fb90 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 20 Oct 2023 19:49:04 +0530 Subject: [PATCH 0179/1399] Attach NOTES - LeetHub --- 0844-backspace-string-compare/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0844-backspace-string-compare/NOTES.md diff --git a/0844-backspace-string-compare/NOTES.md b/0844-backspace-string-compare/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0844-backspace-string-compare/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 80c521325d1c72f815cd3511fced8b15925e7537 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 20 Oct 2023 19:49:07 +0530 Subject: [PATCH 0180/1399] Time: 0 ms (100.00%), Space: 6.4 MB (70.46%) - LeetHub --- .../0844-backspace-string-compare.cpp | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 0844-backspace-string-compare/0844-backspace-string-compare.cpp diff --git a/0844-backspace-string-compare/0844-backspace-string-compare.cpp b/0844-backspace-string-compare/0844-backspace-string-compare.cpp new file mode 100644 index 00000000..0e7ac67b --- /dev/null +++ b/0844-backspace-string-compare/0844-backspace-string-compare.cpp @@ -0,0 +1,32 @@ +class Solution { +public: + bool backspaceCompare(string s, string t) { + + string one, two; + + for(auto& itr : s) + { + if(itr == '#') + { + if(!one.empty()) + one.pop_back(); + } + else + one.push_back(itr); + } + + for(auto& itr : t) + { + if(itr == '#') + { + if(!two.empty()) + two.pop_back(); + } + else + two.push_back(itr); + } + + return one == two; + + } +}; \ No newline at end of file From 295694c6519929d5100d3eaad9da1506a2b5a169 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 20 Oct 2023 20:00:32 +0530 Subject: [PATCH 0181/1399] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divibm-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbookingcom-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvisa-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtiktok-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div844-backspace-string-compare/NOTES.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divibm-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbookingcom-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvisa-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtiktok-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div844-backspace-string-compare/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divibm-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbookingcom-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvisa-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtiktok-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div844-backspace-string-compare/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divibm-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbookingcom-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvisa-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtiktok-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div844-backspace-string-compare/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 3ff4b06dd0244530c87cfdb1e17996b1958250a5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 20 Oct 2023 20:00:35 +0530 Subject: [PATCH 0182/1399] Time: 0 ms (100.00%), Space: 6.6 MB (40.98%) - LeetHub --- ...iv-div-div844-backspace-string-compare.cpp | 35 ++++++++++++------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divibm-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbookingcom-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvisa-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtiktok-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div844-backspace-string-compare/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divibm-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbookingcom-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvisa-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtiktok-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div844-backspace-string-compare.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divibm-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbookingcom-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvisa-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtiktok-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div844-backspace-string-compare/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divibm-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbookingcom-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvisa-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtiktok-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div844-backspace-string-compare.cpp index 0e7ac67b..da6b4577 100644 --- a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divibm-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbookingcom-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvisa-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtiktok-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div844-backspace-string-compare/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divibm-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbookingcom-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvisa-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtiktok-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div844-backspace-string-compare.cpp +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divibm-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbookingcom-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvisa-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtiktok-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div844-backspace-string-compare/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divibm-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbookingcom-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvisa-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtiktok-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div844-backspace-string-compare.cpp @@ -2,31 +2,42 @@ class Solution { public: bool backspaceCompare(string s, string t) { - string one, two; + int p(0), q(0); + + int n = s.size(), m = t.size(); - for(auto& itr : s) + for(int i = 0; i < n; ++i) { - if(itr == '#') + if(s[i] == '#') { - if(!one.empty()) - one.pop_back(); + p = max(0, --p); } else - one.push_back(itr); + { + s[p++] = s[i]; + } } - for(auto& itr : t) + for(int i = 0; i < m; ++i) { - if(itr == '#') + if(t[i] == '#') { - if(!two.empty()) - two.pop_back(); + q = max(0, --q); } else - two.push_back(itr); + { + t[q++] = t[i]; + } } - return one == two; + if(p != q) return false; + + for(int i = 0; i < p; ++i) + { + if(s[i] != t[i]) + return false; + } + return true; } }; \ No newline at end of file From cbba084f50f77d803c9fa9372b0b91d79631aa3f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 20 Oct 2023 20:02:42 +0530 Subject: [PATCH 0183/1399] Attach NOTES - LeetHub From 40f3c85c0a773ebe44194f4f9ca51c7af312633f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 20 Oct 2023 20:02:45 +0530 Subject: [PATCH 0184/1399] Time: 0 ms (100.00%), Space: 6.6 MB (40.98%) - LeetHub --- ...iv-div-div844-backspace-string-compare.cpp | 41 +++++++------------ 1 file changed, 14 insertions(+), 27 deletions(-) diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divibm-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbookingcom-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvisa-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtiktok-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div844-backspace-string-compare/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divibm-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbookingcom-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvisa-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtiktok-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div844-backspace-string-compare.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divibm-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbookingcom-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvisa-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtiktok-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div844-backspace-string-compare/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divibm-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbookingcom-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvisa-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtiktok-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div844-backspace-string-compare.cpp index da6b4577..e4860bae 100644 --- a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divibm-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbookingcom-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvisa-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtiktok-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div844-backspace-string-compare/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divibm-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbookingcom-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvisa-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtiktok-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div844-backspace-string-compare.cpp +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divibm-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbookingcom-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvisa-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtiktok-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div844-backspace-string-compare/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divibm-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbookingcom-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvisa-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtiktok-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div844-backspace-string-compare.cpp @@ -1,43 +1,30 @@ class Solution { public: - bool backspaceCompare(string s, string t) { - - int p(0), q(0); - - int n = s.size(), m = t.size(); + + string solve(string& str, int n) + { + int k = 0; for(int i = 0; i < n; ++i) { - if(s[i] == '#') - { - p = max(0, --p); - } - else - { - s[p++] = s[i]; - } - } - - for(int i = 0; i < m; ++i) - { - if(t[i] == '#') + if(str[i] == '#') { - q = max(0, --q); + k = max(0, --k); } else { - t[q++] = t[i]; + str[k++] = str[i]; } } - if(p != q) return false; + return str.substr(0, k); + } + + bool backspaceCompare(string s, string t) { + + int n = s.size(), m = t.size(); - for(int i = 0; i < p; ++i) - { - if(s[i] != t[i]) - return false; - } + return (solve(s, n) == solve(t, m)); - return true; } }; \ No newline at end of file From 18c6b024530928e5516dbf34f7d376ea01605e7b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 20 Oct 2023 20:46:10 +0530 Subject: [PATCH 0185/1399] Create README - LeetHub --- 0341-flatten-nested-list-iterator/README.md | 44 +++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 0341-flatten-nested-list-iterator/README.md diff --git a/0341-flatten-nested-list-iterator/README.md b/0341-flatten-nested-list-iterator/README.md new file mode 100644 index 00000000..be9109a3 --- /dev/null +++ b/0341-flatten-nested-list-iterator/README.md @@ -0,0 +1,44 @@ +

341. Flatten Nested List Iterator

Medium


You are given a nested list of integers nestedList. Each element is either an integer or a list whose elements may also be integers or other lists. Implement an iterator to flatten it.

+ +

Implement the NestedIterator class:

+ +
    +
  • NestedIterator(List<NestedInteger> nestedList) Initializes the iterator with the nested list nestedList.
  • +
  • int next() Returns the next integer in the nested list.
  • +
  • boolean hasNext() Returns true if there are still some integers in the nested list and false otherwise.
  • +
+ +

Your code will be tested with the following pseudocode:

+ +
initialize iterator with nestedList
+res = []
+while iterator.hasNext()
+    append iterator.next() to the end of res
+return res
+
+ +

If res matches the expected flattened list, then your code will be judged as correct.

+ +

 

+

Example 1:

+ +
Input: nestedList = [[1,1],2,[1,1]]
+Output: [1,1,2,1,1]
+Explanation: By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,1,2,1,1].
+
+ +

Example 2:

+ +
Input: nestedList = [1,[4,[6]]]
+Output: [1,4,6]
+Explanation: By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,4,6].
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nestedList.length <= 500
  • +
  • The values of the integers in the nested list is in the range [-106, 106].
  • +
+
\ No newline at end of file From 12e3696b8a428f0113967cca7485e58af01b04a1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 20 Oct 2023 20:46:14 +0530 Subject: [PATCH 0186/1399] Time: 12 ms (12.87%), Space: 15.9 MB (12.67%) - LeetHub --- .../0341-flatten-nested-list-iterator.cpp | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 0341-flatten-nested-list-iterator/0341-flatten-nested-list-iterator.cpp diff --git a/0341-flatten-nested-list-iterator/0341-flatten-nested-list-iterator.cpp b/0341-flatten-nested-list-iterator/0341-flatten-nested-list-iterator.cpp new file mode 100644 index 00000000..531e1fc2 --- /dev/null +++ b/0341-flatten-nested-list-iterator/0341-flatten-nested-list-iterator.cpp @@ -0,0 +1,57 @@ +/** + * // This is the interface that allows for creating nested lists. + * // You should not implement it, or speculate about its implementation + * class NestedInteger { + * public: + * // Return true if this NestedInteger holds a single integer, rather than a nested list. + * bool isInteger() const; + * + * // Return the single integer that this NestedInteger holds, if it holds a single integer + * // The result is undefined if this NestedInteger holds a nested list + * int getInteger() const; + * + * // Return the nested list that this NestedInteger holds, if it holds a nested list + * // The result is undefined if this NestedInteger holds a single integer + * const vector &getList() const; + * }; + */ + +class NestedIterator { +public: + + vector v; + int k = 0; + + void helper(vector nestedList) + { + for(auto& itr : nestedList) + { + if(itr.isInteger()) + v.push_back(itr.getInteger()); + else + helper(itr.getList()); + } + } + + NestedIterator(vector &nestedList) { + + helper(nestedList); + + } + + int next() { + int ans = v[k]; + ++k; + return ans; + } + + bool hasNext() { + return k < v.size(); + } +}; + +/** + * Your NestedIterator object will be instantiated and called as such: + * NestedIterator i(nestedList); + * while (i.hasNext()) cout << i.next(); + */ \ No newline at end of file From fa1c48cbfaca529754ede863cbaea30725e574a6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 20 Oct 2023 20:47:40 +0530 Subject: [PATCH 0187/1399] Attach NOTES - LeetHub --- 0341-flatten-nested-list-iterator/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0341-flatten-nested-list-iterator/NOTES.md diff --git a/0341-flatten-nested-list-iterator/NOTES.md b/0341-flatten-nested-list-iterator/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0341-flatten-nested-list-iterator/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 55d78ee0acec4a3d2c6816c9a22cd390393eb77c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 20 Oct 2023 20:47:43 +0530 Subject: [PATCH 0188/1399] Time: 11 ms (18.71%), Space: 13.6 MB (76.12%) - LeetHub --- .../0341-flatten-nested-list-iterator.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/0341-flatten-nested-list-iterator/0341-flatten-nested-list-iterator.cpp b/0341-flatten-nested-list-iterator/0341-flatten-nested-list-iterator.cpp index 531e1fc2..f450d064 100644 --- a/0341-flatten-nested-list-iterator/0341-flatten-nested-list-iterator.cpp +++ b/0341-flatten-nested-list-iterator/0341-flatten-nested-list-iterator.cpp @@ -22,7 +22,7 @@ class NestedIterator { vector v; int k = 0; - void helper(vector nestedList) + void helper(vector& nestedList) { for(auto& itr : nestedList) { From f4ffffd9ef3ab202d69b71b27e478ff02f808625 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 21 Oct 2023 09:32:47 +0530 Subject: [PATCH 0189/1399] Create README - LeetHub --- .../README.md | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 Sum of all divisors from 1 to n - GFG/README.md diff --git a/Sum of all divisors from 1 to n - GFG/README.md b/Sum of all divisors from 1 to n - GFG/README.md new file mode 100644 index 00000000..b0863e36 --- /dev/null +++ b/Sum of all divisors from 1 to n - GFG/README.md @@ -0,0 +1,32 @@ +# Sum of all divisors from 1 to n +## Easy +

Given a positive integer N., The task is to find the value of    \sum_{i=1}^{i=n} F(i)  where function F(i) for the number i is defined as the sum of all divisors of i.

+

Example 1:

+
Input:
+N = 4
+Output:
+15
+Explanation:
+F(1) = 1
+F(2) = 1 + 2 = 3
+F(3) = 1 + 3 = 4
+F(4) = 1 + 2 + 4 = 7
+ans = F(1) + F(2) + F(3) + F(4)
+    = 1 + 3 + 4 + 7
+    = 15
+

Example 2:

+
Input:
+N = 5
+Output:
+21
+Explanation:
+F(1) = 1
+F(2) = 1 + 2 = 3
+F(3) = 1 + 3 = 4
+F(4) = 1 + 2 + 4 = 7
+F(5) = 1 + 5 = 6
+ans = F(1) + F(2) + F(3) + F(4) + F(5)
+    = 1 + 3 + 4 + 7 + 6
+    = 21
+

Your Task:  
You don't need to read input or print anything. Your task is to complete the function sumOfDivisors() which takes an integer N as an input parameter and returns an integer.

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)

+

Constraints:
1 <= N <= 106

\ No newline at end of file From 3401e7d2d4fb18adbe7588e4f7fadcf59b6d567b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 21 Oct 2023 09:32:47 +0530 Subject: [PATCH 0190/1399] Added solution - LeetHub --- .../sum-of-all-divisors-from-1-to-n.cpp | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 Sum of all divisors from 1 to n - GFG/sum-of-all-divisors-from-1-to-n.cpp diff --git a/Sum of all divisors from 1 to n - GFG/sum-of-all-divisors-from-1-to-n.cpp b/Sum of all divisors from 1 to n - GFG/sum-of-all-divisors-from-1-to-n.cpp new file mode 100644 index 00000000..324dcc03 --- /dev/null +++ b/Sum of all divisors from 1 to n - GFG/sum-of-all-divisors-from-1-to-n.cpp @@ -0,0 +1,40 @@ +//{ Driver Code Starts +#include +using namespace std; + +// } Driver Code Ends +//User function Template for C++ +class Solution +{ +public: + long long sumOfDivisors(int N) + { + // Write Your Code here + + long long ans = 0; + + for(int i = 1; i <= N; ++i) + { + ans += (i * (N/i)); + } + + return ans; + } +}; + +//{ Driver Code Starts. +int main() +{ + int t; + cin >> t; + while (t--) + { + int N; + cin>>N; + Solution ob; + long long ans = ob.sumOfDivisors(N); + cout< Date: Sat, 21 Oct 2023 23:26:40 +0530 Subject: [PATCH 0191/1399] Create README - LeetHub --- .../README.md | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1425-constrained-subsequence-sum/README.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1425-constrained-subsequence-sum/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1425-constrained-subsequence-sum/README.md new file mode 100644 index 00000000..83b7de49 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1425-constrained-subsequence-sum/README.md @@ -0,0 +1,34 @@ +

No companies found for this problem
1425. Constrained Subsequence Sum

Hard


Given an integer array nums and an integer k, return the maximum sum of a non-empty subsequence of that array such that for every two consecutive integers in the subsequence, nums[i] and nums[j], where i < j, the condition j - i <= k is satisfied.

+ +

A subsequence of an array is obtained by deleting some number of elements (can be zero) from the array, leaving the remaining elements in their original order.

+ +

 

+

Example 1:

+ +
Input: nums = [10,2,-10,5,20], k = 2
+Output: 37
+Explanation: The subsequence is [10, 2, 5, 20].
+
+ +

Example 2:

+ +
Input: nums = [-1,-2,-3], k = 1
+Output: -1
+Explanation: The subsequence must be non-empty, so we choose the largest number.
+
+ +

Example 3:

+ +
Input: nums = [10,-2,-10,-5,20], k = 2
+Output: 23
+Explanation: The subsequence is [10, -2, -5, 20].
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= k <= nums.length <= 105
  • +
  • -104 <= nums[i] <= 104
  • +
+
\ No newline at end of file From c84fdc389e883ba3998e2f2d9c7a2d3274b1e38f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 21 Oct 2023 23:26:43 +0530 Subject: [PATCH 0192/1399] Time: 311 ms (27.35%), Space: 144.8 MB (27.96%) - LeetHub --- ...iv-div1425-constrained-subsequence-sum.cpp | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1425-constrained-subsequence-sum/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1425-constrained-subsequence-sum.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1425-constrained-subsequence-sum/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1425-constrained-subsequence-sum.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1425-constrained-subsequence-sum/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1425-constrained-subsequence-sum.cpp new file mode 100644 index 00000000..0120df06 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1425-constrained-subsequence-sum/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1425-constrained-subsequence-sum.cpp @@ -0,0 +1,34 @@ +class Solution { +public: + int constrainedSubsetSum(vector& nums, int k) { + + int n = nums.size(); + + vector dp(n+1); + + for(int i = 0; i < n; ++i) + dp[i] = nums[i]; + + priority_queue> pq; + + int ans = *max_element(begin(nums), end(nums)); + + pq.push({nums[0], 0}); + + for(int i = 1; i < n; ++i) + { + while(!pq.empty() and i - pq.top().second > k) + pq.pop(); + + dp[i] = max(dp[i], nums[i] + pq.top().first); + + ans = max(ans, dp[i]); + + pq.push({dp[i], i}); + } + + return ans; + } +}; + + \ No newline at end of file From a94061ad79fe61ca1f5f58bfae325eaba443c864 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 21 Oct 2023 23:37:40 +0530 Subject: [PATCH 0193/1399] Create README - LeetHub --- .../README.md | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence32-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence8-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divuber-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divcitadel-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbytedance-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwilio-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtiktok-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvmware-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbookingcom-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdoordash-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divde-shaw-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divquora-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div239-sliding-window-maximum/README.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence32-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence8-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divuber-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divcitadel-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbytedance-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwilio-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtiktok-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvmware-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbookingcom-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdoordash-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divde-shaw-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divquora-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div239-sliding-window-maximum/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence32-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence8-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divuber-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divcitadel-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbytedance-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwilio-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtiktok-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvmware-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbookingcom-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdoordash-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divde-shaw-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divquora-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div239-sliding-window-maximum/README.md new file mode 100644 index 00000000..158d4a8f --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence32-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence8-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divuber-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divcitadel-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbytedance-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwilio-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtiktok-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvmware-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbookingcom-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdoordash-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divde-shaw-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divquora-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div239-sliding-window-maximum/README.md @@ -0,0 +1,35 @@ +

Amazon
32
Google
8
Microsoft
6
Salesforce
6
Uber
5
Facebook
5
Adobe
4
Citadel
4
ByteDance
4
Apple
3
Twilio
3
tiktok
3
Bloomberg
2
VMware
2
Booking.com
2
DoorDash
2
DE Shaw
2
Quora
2
239. Sliding Window Maximum

Hard


You are given an array of integers nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position.

+ +

Return the max sliding window.

+ +

 

+

Example 1:

+ +
Input: nums = [1,3,-1,-3,5,3,6,7], k = 3
+Output: [3,3,5,5,6,7]
+Explanation: 
+Window position                Max
+---------------               -----
+[1  3  -1] -3  5  3  6  7       3
+ 1 [3  -1  -3] 5  3  6  7       3
+ 1  3 [-1  -3  5] 3  6  7       5
+ 1  3  -1 [-3  5  3] 6  7       5
+ 1  3  -1  -3 [5  3  6] 7       6
+ 1  3  -1  -3  5 [3  6  7]      7
+
+ +

Example 2:

+ +
Input: nums = [1], k = 1
+Output: [1]
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 105
  • +
  • -104 <= nums[i] <= 104
  • +
  • 1 <= k <= nums.length
  • +
+
\ No newline at end of file From 852771c2872d038aaab32437df2a266523448ebf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 21 Oct 2023 23:37:40 +0530 Subject: [PATCH 0194/1399] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence32-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence8-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divuber-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divcitadel-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbytedance-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwilio-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtiktok-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvmware-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbookingcom-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdoordash-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divde-shaw-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divquora-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div239-sliding-window-maximum/NOTES.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence32-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence8-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divuber-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divcitadel-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbytedance-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwilio-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtiktok-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvmware-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbookingcom-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdoordash-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divde-shaw-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divquora-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div239-sliding-window-maximum/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence32-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence8-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divuber-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divcitadel-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbytedance-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwilio-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtiktok-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvmware-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbookingcom-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdoordash-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divde-shaw-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divquora-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div239-sliding-window-maximum/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence32-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence8-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsalesforce-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divuber-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence5-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divcitadel-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbytedance-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwilio-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtiktok-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvmware-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbookingcom-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdoordash-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divde-shaw-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divquora-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div239-sliding-window-maximum/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 324f454ad23986ddf7ccfbcf30b77658431a34e2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 21 Oct 2023 23:38:50 +0530 Subject: [PATCH 0195/1399] Attach NOTES - LeetHub From c90f8f0abc7b95b55d478a6cfa0278f627feefdf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 21 Oct 2023 23:38:54 +0530 Subject: [PATCH 0196/1399] Time: 354 ms (15.43%), Space: 194.5 MB (9.70%) - LeetHub --- .../0239-sliding-window-maximum.cpp | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/0239-sliding-window-maximum/0239-sliding-window-maximum.cpp b/0239-sliding-window-maximum/0239-sliding-window-maximum.cpp index 93aab41f..f0c6f117 100644 --- a/0239-sliding-window-maximum/0239-sliding-window-maximum.cpp +++ b/0239-sliding-window-maximum/0239-sliding-window-maximum.cpp @@ -2,14 +2,14 @@ class Solution { public: vector maxSlidingWindow(vector& nums, int k) { - int i = 0, j = 0; - int n = nums.size(); - vector ans; + int i = 0, j = 0; list l; + vector ans; + while(j < n) { while(!l.empty() and nums[l.back()] <= nums[j]) @@ -22,9 +22,7 @@ class Solution { ans.push_back(nums[l.front()]); if(l.front() == i) - { l.pop_front(); - } ++i; } @@ -33,6 +31,5 @@ class Solution { } return ans; - } }; \ No newline at end of file From 80f12963cf7a0a080e7d8749508c81b00606415b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 21 Oct 2023 23:47:20 +0530 Subject: [PATCH 0197/1399] Create README - LeetHub --- 1425-constrained-subsequence-sum/README.md | 34 ++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 1425-constrained-subsequence-sum/README.md diff --git a/1425-constrained-subsequence-sum/README.md b/1425-constrained-subsequence-sum/README.md new file mode 100644 index 00000000..6afbb1b6 --- /dev/null +++ b/1425-constrained-subsequence-sum/README.md @@ -0,0 +1,34 @@ +

1425. Constrained Subsequence Sum

Hard


Given an integer array nums and an integer k, return the maximum sum of a non-empty subsequence of that array such that for every two consecutive integers in the subsequence, nums[i] and nums[j], where i < j, the condition j - i <= k is satisfied.

+ +

A subsequence of an array is obtained by deleting some number of elements (can be zero) from the array, leaving the remaining elements in their original order.

+ +

 

+

Example 1:

+ +
Input: nums = [10,2,-10,5,20], k = 2
+Output: 37
+Explanation: The subsequence is [10, 2, 5, 20].
+
+ +

Example 2:

+ +
Input: nums = [-1,-2,-3], k = 1
+Output: -1
+Explanation: The subsequence must be non-empty, so we choose the largest number.
+
+ +

Example 3:

+ +
Input: nums = [10,-2,-10,-5,20], k = 2
+Output: 23
+Explanation: The subsequence is [10, -2, -5, 20].
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= k <= nums.length <= 105
  • +
  • -104 <= nums[i] <= 104
  • +
+
\ No newline at end of file From 2640823f4bf71825e81d8b6a4cd8f18d2a34b6f9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 21 Oct 2023 23:47:22 +0530 Subject: [PATCH 0198/1399] Attach NOTES - LeetHub --- 1425-constrained-subsequence-sum/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1425-constrained-subsequence-sum/NOTES.md diff --git a/1425-constrained-subsequence-sum/NOTES.md b/1425-constrained-subsequence-sum/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1425-constrained-subsequence-sum/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 6121bf2ba59a72b718ccca89715d942fbea85930 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 21 Oct 2023 23:47:24 +0530 Subject: [PATCH 0199/1399] Time: 311 ms (27.35%), Space: 144.8 MB (27.96%) - LeetHub --- .../1425-constrained-subsequence-sum.cpp | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 1425-constrained-subsequence-sum/1425-constrained-subsequence-sum.cpp diff --git a/1425-constrained-subsequence-sum/1425-constrained-subsequence-sum.cpp b/1425-constrained-subsequence-sum/1425-constrained-subsequence-sum.cpp new file mode 100644 index 00000000..0120df06 --- /dev/null +++ b/1425-constrained-subsequence-sum/1425-constrained-subsequence-sum.cpp @@ -0,0 +1,34 @@ +class Solution { +public: + int constrainedSubsetSum(vector& nums, int k) { + + int n = nums.size(); + + vector dp(n+1); + + for(int i = 0; i < n; ++i) + dp[i] = nums[i]; + + priority_queue> pq; + + int ans = *max_element(begin(nums), end(nums)); + + pq.push({nums[0], 0}); + + for(int i = 1; i < n; ++i) + { + while(!pq.empty() and i - pq.top().second > k) + pq.pop(); + + dp[i] = max(dp[i], nums[i] + pq.top().first); + + ans = max(ans, dp[i]); + + pq.push({dp[i], i}); + } + + return ans; + } +}; + + \ No newline at end of file From 47e1f61e498d83f2a9bfdaeb9f5ceb96dce82bef Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 21 Oct 2023 23:53:12 +0530 Subject: [PATCH 0200/1399] Attach NOTES - LeetHub From e208c15eabb8bf783927b899957baa14d97d94de Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 21 Oct 2023 23:53:15 +0530 Subject: [PATCH 0201/1399] Time: 311 ms (27.35%), Space: 144.8 MB (27.96%) - LeetHub From 5f4fe8848a607a4a8fba71e0a0a001291bf17ecb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 21 Oct 2023 23:55:11 +0530 Subject: [PATCH 0202/1399] Attach NOTES - LeetHub From 2f3ace0d5a8b137e9ff945ac7b91a63590aa9b96 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 21 Oct 2023 23:55:14 +0530 Subject: [PATCH 0203/1399] Time: 311 ms (27.35%), Space: 144.8 MB (27.96%) - LeetHub From e3faabc5f1396597e74e9f26d428475621db9a66 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 21 Oct 2023 23:57:15 +0530 Subject: [PATCH 0204/1399] Attach NOTES - LeetHub From 99dff23de65e4dbd1dba9e4b88243dc163a7a695 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 21 Oct 2023 23:57:19 +0530 Subject: [PATCH 0205/1399] Time: 311 ms (27.35%), Space: 144.8 MB (27.96%) - LeetHub From 30e12a622e97bafcb9d79370969350ec51ca2c7d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 22 Oct 2023 00:01:34 +0530 Subject: [PATCH 0206/1399] Attach NOTES - LeetHub From b0015c5feb69d15c30e542afb94abaedb2ac0572 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 22 Oct 2023 00:01:37 +0530 Subject: [PATCH 0207/1399] Time: 311 ms (27.35%), Space: 144.8 MB (27.96%) - LeetHub From 95071bab4261934d58d79d35becd335e4cbcc2cc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 22 Oct 2023 00:04:07 +0530 Subject: [PATCH 0208/1399] Attach NOTES - LeetHub From 86fb18678a1d0d296fe1f503ce7ccf93c1106cac Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 22 Oct 2023 00:04:11 +0530 Subject: [PATCH 0209/1399] Time: 311 ms (27.35%), Space: 144.8 MB (27.96%) - LeetHub From 6192da5ed13e106dd4b1709725e42747ed0b282a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 22 Oct 2023 00:04:31 +0530 Subject: [PATCH 0210/1399] Attach NOTES - LeetHub From ad5003a3d006c243bc54eda43153dda8cf104b85 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 22 Oct 2023 00:04:34 +0530 Subject: [PATCH 0211/1399] Time: 311 ms (27.35%), Space: 144.8 MB (27.96%) - LeetHub From b193e540534fb06618f67f0890bd46dc19256912 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 22 Oct 2023 00:08:32 +0530 Subject: [PATCH 0212/1399] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1425-constrained-subsequence-sum/NOTES.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1425-constrained-subsequence-sum/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1425-constrained-subsequence-sum/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1425-constrained-subsequence-sum/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 13045689ccc6a1871445ad646fdeee72bb13df3c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 22 Oct 2023 00:08:35 +0530 Subject: [PATCH 0213/1399] Time: 202 ms (84.49%), Space: 120.1 MB (75.92%) - LeetHub --- ...iv-div1425-constrained-subsequence-sum.cpp | 31 +++++++++---------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1425-constrained-subsequence-sum/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1425-constrained-subsequence-sum.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1425-constrained-subsequence-sum/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1425-constrained-subsequence-sum.cpp index 0120df06..2a6a2dc4 100644 --- a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1425-constrained-subsequence-sum/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1425-constrained-subsequence-sum.cpp +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1425-constrained-subsequence-sum/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1425-constrained-subsequence-sum.cpp @@ -4,31 +4,28 @@ class Solution { int n = nums.size(); - vector dp(n+1); + vector dp(nums); + + deque dq; - for(int i = 0; i < n; ++i) - dp[i] = nums[i]; - - priority_queue> pq; - - int ans = *max_element(begin(nums), end(nums)); - - pq.push({nums[0], 0}); + int ans = dp[0]; - for(int i = 1; i < n; ++i) + for(int i = 0; i < n; ++i) { - while(!pq.empty() and i - pq.top().second > k) - pq.pop(); + while(!dq.empty() and i - dq.front() > k) + dq.pop_front(); - dp[i] = max(dp[i], nums[i] + pq.top().first); + if(!dq.empty()) + dp[i] = max(dp[i], nums[i] + dp[dq.front()]); - ans = max(ans, dp[i]); + while(!dq.empty() and dp[i] >= dp[dq.back()]) + dq.pop_back(); + + dq.push_back(i); - pq.push({dp[i], i}); + ans = max(ans, dp[i]); } return ans; } }; - - \ No newline at end of file From f49367df82b99ea2e008f170ab155d4b4ddd27b6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 22 Oct 2023 23:41:42 +0530 Subject: [PATCH 0214/1399] Create README - LeetHub --- Number of paths - GFG/README.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Number of paths - GFG/README.md diff --git a/Number of paths - GFG/README.md b/Number of paths - GFG/README.md new file mode 100644 index 00000000..bd1a9a3c --- /dev/null +++ b/Number of paths - GFG/README.md @@ -0,0 +1,28 @@ +# Number of paths +## Medium +

The problem is to count all the possible paths from top left to bottom right of an MxN matrix with the constraints that from each cell you can either move to right or down.

+

Return answer modulo 109+7.

+

Example 1:

+
Input:
+M = 3 and N = 3
+Output: 6
+Explanation:
+Let the given input 3*3 matrix is filled 
+as such:
+A B C
+D E F
+G H I
+The possible paths which exists to reach 
+'I' from 'A' following above conditions 
+are as follows:ABCFI, ABEHI, ADGHI, ADEFI, 
+ADEHI, ABEFI
+
+

Example 2:

+
Input:
+M = 1 and N = 4
+Output: 1
+Explanation:
+There is only one direction to go in,
and thus, there is only one path possible.
+

Your Task
You don't need to read input or print anything. Your task is to complete the function numberOfPaths() which takes the integer M and integer N as input parameters and returns an integer, the number of paths.

+

Expected Time Complexity: O(M)
Expected Space Complexity: O(1)

+

Constraints:
1 ≤ N ≤ 108
1 ≤ M ≤ 105

\ No newline at end of file From e4ba45d1b1756ee450c63daef879bb681b35b403 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 22 Oct 2023 23:41:43 +0530 Subject: [PATCH 0215/1399] Added solution - LeetHub --- Number of paths - GFG/number-of-paths.cpp | 73 +++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 Number of paths - GFG/number-of-paths.cpp diff --git a/Number of paths - GFG/number-of-paths.cpp b/Number of paths - GFG/number-of-paths.cpp new file mode 100644 index 00000000..4d0291c2 --- /dev/null +++ b/Number of paths - GFG/number-of-paths.cpp @@ -0,0 +1,73 @@ +//{ Driver Code Starts +#include +using namespace std; + + +// } Driver Code Ends +class Solution +{ + public: + + const int mod = 1e9+7; + + long long expo(long long x, long long y, long long mod) + { + long long res = 1; + + while(y > 0) + { + if(y & 1) + { + res = (res * x) % mod; + } + + x = (x * x) % mod; + + y >>= 1; + } + + return res; + } + + long long modInverse(long long x, long long m) + { + return expo(x, m-2, m); + } + + long long numberOfPaths(int M, int N) + { + // Code Here + + int n = M + N - 2; + int r = M-1; + + long long ans = 1; + + for(int i = 1; i <= r; ++i) + { + ans = (ans * (n-i+1)) % mod; + ans = (ans * modInverse(i , mod))%mod; + } + + return ans; + } +}; + + +//{ Driver Code Starts. + + +int main() +{ + int t; + cin>>t; + while(t--) + { + int N, M; + cin>>M>>N; + Solution ob; + cout << ob.numberOfPaths(M, N)< Date: Mon, 23 Oct 2023 00:44:53 +0530 Subject: [PATCH 0216/1399] Create README - LeetHub --- .../README.md | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1793-maximum-score-of-a-good-subarray/README.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1793-maximum-score-of-a-good-subarray/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1793-maximum-score-of-a-good-subarray/README.md new file mode 100644 index 00000000..8e27f09c --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1793-maximum-score-of-a-good-subarray/README.md @@ -0,0 +1,30 @@ +

No companies found for this problem
1793. Maximum Score of a Good Subarray

Hard


You are given an array of integers nums (0-indexed) and an integer k.

+ +

The score of a subarray (i, j) is defined as min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1). A good subarray is a subarray where i <= k <= j.

+ +

Return the maximum possible score of a good subarray.

+ +

 

+

Example 1:

+ +
Input: nums = [1,4,3,7,4,5], k = 3
+Output: 15
+Explanation: The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15. 
+
+ +

Example 2:

+ +
Input: nums = [5,5,4,5,4,1,1,1], k = 0
+Output: 20
+Explanation: The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 105
  • +
  • 1 <= nums[i] <= 2 * 104
  • +
  • 0 <= k < nums.length
  • +
+
\ No newline at end of file From dd47d5eff1a14b20249b6f653000b0d105feeab3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 23 Oct 2023 00:44:57 +0530 Subject: [PATCH 0217/1399] Time: 118 ms (93.20%), Space: 89.6 MB (80.92%) - LeetHub --- ...v1793-maximum-score-of-a-good-subarray.cpp | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1793-maximum-score-of-a-good-subarray/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1793-maximum-score-of-a-good-subarray.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1793-maximum-score-of-a-good-subarray/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1793-maximum-score-of-a-good-subarray.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1793-maximum-score-of-a-good-subarray/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1793-maximum-score-of-a-good-subarray.cpp new file mode 100644 index 00000000..1e22038a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1793-maximum-score-of-a-good-subarray/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1793-maximum-score-of-a-good-subarray.cpp @@ -0,0 +1,32 @@ +class Solution { +public: + int maximumScore(vector& nums, int k) { + + int n = nums.size(); + + int i = k, j = k; + + int ans = nums[k], currMin = nums[k]; + + while(i > 0 or j < n-1) + { + int left = (i > 0 ? nums[i-1] : 0); + int right = (j < n-1 ? nums[j+1] : 0); + + if(left > right) + { + --i; + currMin = min(currMin, nums[i]); + } + else + { + ++j; + currMin = min(currMin,nums[j]); + } + + ans = max(ans, currMin * (j-i+1)); + } + + return ans; + } +}; \ No newline at end of file From f8b58448ece833a45289ff66e895269040ef882d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 23 Oct 2023 00:45:19 +0530 Subject: [PATCH 0218/1399] Create README - LeetHub --- .../README.md | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 1793-maximum-score-of-a-good-subarray/README.md diff --git a/1793-maximum-score-of-a-good-subarray/README.md b/1793-maximum-score-of-a-good-subarray/README.md new file mode 100644 index 00000000..d32c4f68 --- /dev/null +++ b/1793-maximum-score-of-a-good-subarray/README.md @@ -0,0 +1,30 @@ +

1793. Maximum Score of a Good Subarray

Hard


You are given an array of integers nums (0-indexed) and an integer k.

+ +

The score of a subarray (i, j) is defined as min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1). A good subarray is a subarray where i <= k <= j.

+ +

Return the maximum possible score of a good subarray.

+ +

 

+

Example 1:

+ +
Input: nums = [1,4,3,7,4,5], k = 3
+Output: 15
+Explanation: The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15. 
+
+ +

Example 2:

+ +
Input: nums = [5,5,4,5,4,1,1,1], k = 0
+Output: 20
+Explanation: The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 105
  • +
  • 1 <= nums[i] <= 2 * 104
  • +
  • 0 <= k < nums.length
  • +
+
\ No newline at end of file From 94759f22b4245f9f208bca65574de31154ed4268 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 23 Oct 2023 00:45:19 +0530 Subject: [PATCH 0219/1399] Attach NOTES - LeetHub --- 1793-maximum-score-of-a-good-subarray/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1793-maximum-score-of-a-good-subarray/NOTES.md diff --git a/1793-maximum-score-of-a-good-subarray/NOTES.md b/1793-maximum-score-of-a-good-subarray/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1793-maximum-score-of-a-good-subarray/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 92617fd0ec0a82fd34ac3e172277d1599007e412 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 23 Oct 2023 00:45:22 +0530 Subject: [PATCH 0220/1399] Time: 118 ms (93.20%), Space: 89.6 MB (80.92%) - LeetHub --- .../1793-maximum-score-of-a-good-subarray.cpp | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 1793-maximum-score-of-a-good-subarray/1793-maximum-score-of-a-good-subarray.cpp diff --git a/1793-maximum-score-of-a-good-subarray/1793-maximum-score-of-a-good-subarray.cpp b/1793-maximum-score-of-a-good-subarray/1793-maximum-score-of-a-good-subarray.cpp new file mode 100644 index 00000000..1e22038a --- /dev/null +++ b/1793-maximum-score-of-a-good-subarray/1793-maximum-score-of-a-good-subarray.cpp @@ -0,0 +1,32 @@ +class Solution { +public: + int maximumScore(vector& nums, int k) { + + int n = nums.size(); + + int i = k, j = k; + + int ans = nums[k], currMin = nums[k]; + + while(i > 0 or j < n-1) + { + int left = (i > 0 ? nums[i-1] : 0); + int right = (j < n-1 ? nums[j+1] : 0); + + if(left > right) + { + --i; + currMin = min(currMin, nums[i]); + } + else + { + ++j; + currMin = min(currMin,nums[j]); + } + + ans = max(ans, currMin * (j-i+1)); + } + + return ans; + } +}; \ No newline at end of file From 2889b1f1dc2c25130e8479b2733701ddaa928512 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 23 Oct 2023 07:49:44 +0530 Subject: [PATCH 0221/1399] Create README - LeetHub --- .../README.md | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwo-sigma-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div342-power-of-four/README.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwo-sigma-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div342-power-of-four/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwo-sigma-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div342-power-of-four/README.md new file mode 100644 index 00000000..f84ffe4d --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwo-sigma-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div342-power-of-four/README.md @@ -0,0 +1,24 @@ +

Two Sigma
1
342. Power of Four

Easy


Given an integer n, return true if it is a power of four. Otherwise, return false.

+ +

An integer n is a power of four, if there exists an integer x such that n == 4x.

+ +

 

+

Example 1:

+
Input: n = 16
+Output: true
+

Example 2:

+
Input: n = 5
+Output: false
+

Example 3:

+
Input: n = 1
+Output: true
+
+

 

+

Constraints:

+ +
    +
  • -231 <= n <= 231 - 1
  • +
+ +

 

+Follow up: Could you solve it without loops/recursion?
\ No newline at end of file From 98b6df6cb219f9c7e2cc51aa3390c5ee12dd8cef Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 23 Oct 2023 07:49:44 +0530 Subject: [PATCH 0222/1399] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwo-sigma-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div342-power-of-four/NOTES.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwo-sigma-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div342-power-of-four/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwo-sigma-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div342-power-of-four/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwo-sigma-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div342-power-of-four/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 6a23356bac1a4aaab2d58f2dfbfb8682673bde73 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 23 Oct 2023 07:49:48 +0530 Subject: [PATCH 0223/1399] Time: 5 ms (10.09%), Space: 6.3 MB (47.70%) - LeetHub --- ...t-svg-div-div-div-div342-power-of-four.cpp | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwo-sigma-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div342-power-of-four/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwo-sigma-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div342-power-of-four.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwo-sigma-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div342-power-of-four/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwo-sigma-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div342-power-of-four.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwo-sigma-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div342-power-of-four/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwo-sigma-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div342-power-of-four.cpp new file mode 100644 index 00000000..eb1b35f2 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwo-sigma-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div342-power-of-four/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwo-sigma-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div342-power-of-four.cpp @@ -0,0 +1,21 @@ +class Solution { +public: + + bool helper(int n) + { + if(n <= 0) + return false; + + if(n == 1) + return true; + + if(n % 4 == 0) + return helper(n/4); + + return false; + } + + bool isPowerOfFour(int n) { + return helper(n); + } +}; \ No newline at end of file From 7567a1ba7b8229cd7fd6a0b14d9d0c3277f6bafe Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 23 Oct 2023 08:01:09 +0530 Subject: [PATCH 0224/1399] Create README - LeetHub --- 0342-power-of-four/README.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 0342-power-of-four/README.md diff --git a/0342-power-of-four/README.md b/0342-power-of-four/README.md new file mode 100644 index 00000000..e5dba89e --- /dev/null +++ b/0342-power-of-four/README.md @@ -0,0 +1,24 @@ +

342. Power of Four

Easy


Given an integer n, return true if it is a power of four. Otherwise, return false.

+ +

An integer n is a power of four, if there exists an integer x such that n == 4x.

+ +

 

+

Example 1:

+
Input: n = 16
+Output: true
+

Example 2:

+
Input: n = 5
+Output: false
+

Example 3:

+
Input: n = 1
+Output: true
+
+

 

+

Constraints:

+ +
    +
  • -231 <= n <= 231 - 1
  • +
+ +

 

+Follow up: Could you solve it without loops/recursion?
\ No newline at end of file From a732bcf4a7041d97197f1f3967b786eae49ed9d0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 23 Oct 2023 08:01:12 +0530 Subject: [PATCH 0225/1399] Time: 5 ms (10.09%), Space: 6.3 MB (47.70%) - LeetHub --- 0342-power-of-four/0342-power-of-four.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 0342-power-of-four/0342-power-of-four.cpp diff --git a/0342-power-of-four/0342-power-of-four.cpp b/0342-power-of-four/0342-power-of-four.cpp new file mode 100644 index 00000000..eb1b35f2 --- /dev/null +++ b/0342-power-of-four/0342-power-of-four.cpp @@ -0,0 +1,21 @@ +class Solution { +public: + + bool helper(int n) + { + if(n <= 0) + return false; + + if(n == 1) + return true; + + if(n % 4 == 0) + return helper(n/4); + + return false; + } + + bool isPowerOfFour(int n) { + return helper(n); + } +}; \ No newline at end of file From 6c36a2484352aabb82e6ae6e9992d16e22609061 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 23 Oct 2023 08:04:14 +0530 Subject: [PATCH 0226/1399] Attach NOTES - LeetHub --- 0342-power-of-four/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0342-power-of-four/NOTES.md diff --git a/0342-power-of-four/NOTES.md b/0342-power-of-four/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0342-power-of-four/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 80fcda9bc0e37bf796cf497df51656a2da702af6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 23 Oct 2023 08:04:17 +0530 Subject: [PATCH 0227/1399] Time: 5 ms (10.09%), Space: 6.3 MB (47.70%) - LeetHub From e28bb156533a392d013340e933e016e86123c42f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 23 Oct 2023 08:08:26 +0530 Subject: [PATCH 0228/1399] Attach NOTES - LeetHub From fe29e50d34dc872fb50426b81a3828c24583a922 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 23 Oct 2023 08:08:30 +0530 Subject: [PATCH 0229/1399] Time: 5 ms (10.09%), Space: 6.3 MB (47.70%) - LeetHub From b7a4f16407786390f3d87ccb7d5fb176a6f3e814 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 23 Oct 2023 08:09:10 +0530 Subject: [PATCH 0230/1399] Attach NOTES - LeetHub From 49f8ef379f16d7209a9b0bbac31c1b36db9f3787 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 23 Oct 2023 08:09:14 +0530 Subject: [PATCH 0231/1399] Time: 5 ms (10.09%), Space: 6.3 MB (47.70%) - LeetHub From 8149b51f4e1fb419622df445449a6859b4ef5865 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 24 Oct 2023 08:10:24 +0530 Subject: [PATCH 0232/1399] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence18-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div515-find-largest-value-in-each-tree-row/NOTES.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence18-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div515-find-largest-value-in-each-tree-row/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence18-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div515-find-largest-value-in-each-tree-row/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence18-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div515-find-largest-value-in-each-tree-row/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 7eca7e7f960830353d21a2d41b8cff18865eb173 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 24 Oct 2023 08:10:28 +0530 Subject: [PATCH 0233/1399] Time: 4 ms (91.88%), Space: 22.8 MB (9.19%) - LeetHub --- ...15-find-largest-value-in-each-tree-row.cpp | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence18-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div515-find-largest-value-in-each-tree-row/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence18-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div515-find-largest-value-in-each-tree-row.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence18-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div515-find-largest-value-in-each-tree-row/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence18-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div515-find-largest-value-in-each-tree-row.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence18-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div515-find-largest-value-in-each-tree-row/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence18-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div515-find-largest-value-in-each-tree-row.cpp new file mode 100644 index 00000000..f2f14855 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence18-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div515-find-largest-value-in-each-tree-row/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence18-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div515-find-largest-value-in-each-tree-row.cpp @@ -0,0 +1,49 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + vector largestValues(TreeNode* root) { + + if(!root) + return {}; + + queue q; + + vector ans; + + q.push(root); + + while(!q.empty()) + { + int size = q.size(); + + int maxi = INT_MIN; + + for(int i = 0; i < size; ++i) + { + TreeNode* curr = q.front(); + q.pop(); + + maxi = max(maxi, curr->val); + + if(curr->left) + q.push(curr->left); + + if(curr->right) + q.push(curr->right); + } + + ans.push_back(maxi); + } + return ans; + } +}; \ No newline at end of file From 0c519e70546e3d6a4c013f26dc2bd6ce3c52e90e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 24 Oct 2023 08:14:39 +0530 Subject: [PATCH 0234/1399] Create README - LeetHub --- .../README.md | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 0515-find-largest-value-in-each-tree-row/README.md diff --git a/0515-find-largest-value-in-each-tree-row/README.md b/0515-find-largest-value-in-each-tree-row/README.md new file mode 100644 index 00000000..95bb431d --- /dev/null +++ b/0515-find-largest-value-in-each-tree-row/README.md @@ -0,0 +1,23 @@ +

515. Find Largest Value in Each Tree Row

Medium


Given the root of a binary tree, return an array of the largest value in each row of the tree (0-indexed).

+ +

 

+

Example 1:

+ +
Input: root = [1,3,2,5,3,null,9]
+Output: [1,3,9]
+
+ +

Example 2:

+ +
Input: root = [1,2,3]
+Output: [1,3]
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree will be in the range [0, 104].
  • +
  • -231 <= Node.val <= 231 - 1
  • +
+
\ No newline at end of file From 18abad6a2a0fd7879f23029ead87762276efe1a8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 24 Oct 2023 08:14:39 +0530 Subject: [PATCH 0235/1399] Attach NOTES - LeetHub --- 0515-find-largest-value-in-each-tree-row/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0515-find-largest-value-in-each-tree-row/NOTES.md diff --git a/0515-find-largest-value-in-each-tree-row/NOTES.md b/0515-find-largest-value-in-each-tree-row/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0515-find-largest-value-in-each-tree-row/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 296d0395c7126f6e5e4421b524362d6db53de2d7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 24 Oct 2023 08:14:43 +0530 Subject: [PATCH 0236/1399] Time: 4 ms (91.88%), Space: 22.8 MB (9.19%) - LeetHub --- ...15-find-largest-value-in-each-tree-row.cpp | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 0515-find-largest-value-in-each-tree-row/0515-find-largest-value-in-each-tree-row.cpp diff --git a/0515-find-largest-value-in-each-tree-row/0515-find-largest-value-in-each-tree-row.cpp b/0515-find-largest-value-in-each-tree-row/0515-find-largest-value-in-each-tree-row.cpp new file mode 100644 index 00000000..f2f14855 --- /dev/null +++ b/0515-find-largest-value-in-each-tree-row/0515-find-largest-value-in-each-tree-row.cpp @@ -0,0 +1,49 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + vector largestValues(TreeNode* root) { + + if(!root) + return {}; + + queue q; + + vector ans; + + q.push(root); + + while(!q.empty()) + { + int size = q.size(); + + int maxi = INT_MIN; + + for(int i = 0; i < size; ++i) + { + TreeNode* curr = q.front(); + q.pop(); + + maxi = max(maxi, curr->val); + + if(curr->left) + q.push(curr->left); + + if(curr->right) + q.push(curr->right); + } + + ans.push_back(maxi); + } + return ans; + } +}; \ No newline at end of file From 1f66f056fb215ebc79bd59f3d7cff0dbeae69a9e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 24 Oct 2023 08:37:24 +0530 Subject: [PATCH 0237/1399] Attach NOTES - LeetHub From 723106d646ae5bb6ab5fd751022ebaf581d179ee Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 24 Oct 2023 08:37:27 +0530 Subject: [PATCH 0238/1399] Time: 4 ms (91.88%), Space: 22.8 MB (9.19%) - LeetHub From acd7849509e3d40649ed7b7dcd7ec4b6451427ac Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 25 Oct 2023 08:30:35 +0530 Subject: [PATCH 0239/1399] Create README - LeetHub --- Knapsack with Duplicate Items - GFG/README.md | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Knapsack with Duplicate Items - GFG/README.md diff --git a/Knapsack with Duplicate Items - GFG/README.md b/Knapsack with Duplicate Items - GFG/README.md new file mode 100644 index 00000000..07f5effd --- /dev/null +++ b/Knapsack with Duplicate Items - GFG/README.md @@ -0,0 +1,21 @@ +# Knapsack with Duplicate Items +## Medium +

Given a set of N items, each with a weight and a value, represented by the array w and val respectively. Also, a knapsack with weight limit W.
The task is to fill the knapsack in such a way that we can get the maximum profit. Return the maximum profit.
Note: Each item can be taken any number of times.

+

Example 1:

+
Input: 
N = 2
W = 3 +val = {1, 1} +wt = {2, 1} +Output:
3 +Explanation: +1.Pick the 2nd element thrice. +2.Total profit = 1 + 1 + 1 = 3. Also the total weight = 1 + 1 + 1 = 3 which is <= 3.
+
+

Example 2:

+
Input: 
N = 4
W = 8 +val[] = {6, 1, 7, 7} +wt[] = {1, 3, 4, 5} +Output:
48 +Explanation:
The optimal choice is to pick the 1st element 8 times.
+

Your Task:
You do not need to read input or print anything. Your task is to complete the function knapSack() which takes the values N, W and the arrays val and wt as input parameters and returns the maximum possible value.

+

Expected Time Complexity: O(N*W)
Expected Auxiliary Space: O(W)

+

Constraints:
1 ≤ N, W ≤ 1000
1 ≤ val[i], wt[i] ≤ 100

\ No newline at end of file From ad5d25f51479f1a270fe294dba1d76facd72e9f5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 25 Oct 2023 08:30:36 +0530 Subject: [PATCH 0240/1399] Added solution - LeetHub --- .../knapsack-with-duplicate-items.cpp | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 Knapsack with Duplicate Items - GFG/knapsack-with-duplicate-items.cpp diff --git a/Knapsack with Duplicate Items - GFG/knapsack-with-duplicate-items.cpp b/Knapsack with Duplicate Items - GFG/knapsack-with-duplicate-items.cpp new file mode 100644 index 00000000..d158438b --- /dev/null +++ b/Knapsack with Duplicate Items - GFG/knapsack-with-duplicate-items.cpp @@ -0,0 +1,60 @@ +//{ Driver Code Starts +// Initial Template for C++ + +#include +using namespace std; + +// } Driver Code Ends +// User function Template for C++ + +class Solution{ +public: + + int helper(int idx, int n, int W, int val[], int wt[], vector>& dp) + { + if(idx == n) + return 0; + + if(dp[idx][W] != -1) + return dp[idx][W]; + + int notTake = helper(idx+1, n, W, val, wt, dp); + + int take = 0; + + if(wt[idx] <= W) + take = val[idx] + helper(idx, n, W - wt[idx], val, wt, dp); + + return dp[idx][W] = max(take, notTake); + } + + int knapSack(int N, int W, int val[], int wt[]) + { + // code here + + vector> dp(N+1, vector(W+1, -1)); + + return helper(0, N, W, val, wt, dp); + } +}; + +//{ Driver Code Starts. + +int main(){ + int t; + cin>>t; + while(t--){ + int N, W; + cin>>N>>W; + int val[N], wt[N]; + for(int i = 0;i < N;i++) + cin>>val[i]; + for(int i = 0;i < N;i++) + cin>>wt[i]; + + Solution ob; + cout< Date: Wed, 25 Oct 2023 08:58:38 +0530 Subject: [PATCH 0241/1399] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div779-k-th-symbol-in-grammar/NOTES.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div779-k-th-symbol-in-grammar/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div779-k-th-symbol-in-grammar/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div779-k-th-symbol-in-grammar/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From ffaee1ea5ed7923d83e72ca0f90cdb189d61870d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 25 Oct 2023 08:58:42 +0530 Subject: [PATCH 0242/1399] Time: 2 ms (51.37%), Space: 6.4 MB (28.86%) - LeetHub --- ...-div-div-div-div779-k-th-symbol-in-grammar.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div779-k-th-symbol-in-grammar/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div779-k-th-symbol-in-grammar.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div779-k-th-symbol-in-grammar/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div779-k-th-symbol-in-grammar.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div779-k-th-symbol-in-grammar/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div779-k-th-symbol-in-grammar.cpp new file mode 100644 index 00000000..2acce397 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div779-k-th-symbol-in-grammar/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div779-k-th-symbol-in-grammar.cpp @@ -0,0 +1,15 @@ +class Solution { +public: + int kthGrammar(int n, int k) { + + if(n == 1 and k == 1) + return 0; + + int mid = pow(2, n-1) / 2; + + if(k <= mid) + return kthGrammar(n-1, k); + else + return !kthGrammar(n-1, k-mid); + } +}; \ No newline at end of file From 95f8df6f4fbdea6433fe0ffbf8ab8e913e0353da Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 26 Oct 2023 07:51:10 +0530 Subject: [PATCH 0243/1399] Create README - LeetHub --- Minimum Operations - GFG/README.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Minimum Operations - GFG/README.md diff --git a/Minimum Operations - GFG/README.md b/Minimum Operations - GFG/README.md new file mode 100644 index 00000000..664241f7 --- /dev/null +++ b/Minimum Operations - GFG/README.md @@ -0,0 +1,22 @@ +# Minimum Operations +## Easy +

Given a number N. Find the minimum number of operations required to reach N starting from 0. You have 2 operations available:

+
    +
  • Double the number
  • +
  • Add one to the number
  • +
+

Example 1:

+
Input:
+N = 8
+Output: 4
+Explanation: 
0 + 1 = 1 --> 1 + 1 = 2 --> 2 * 2 = 4 --> 4 * 2 = 8. +
+

Example 2:

+
Input: 
+N = 7
+Output: 5
+Explanation: 
0 + 1 = 1 --> 1 + 1 = 2 --> 1 + 2 = 3 --> 3 * 2 = 6 --> 6 + 1 = 7. +
+

Your Task:
You don't need to read input or print anything. Your task is to complete the function minOperation() which accepts an integer N and return number of minimum operations required to reach N from 0.

+

Expected Time Complexity: O(LogN)
Expected Auxiliary Space: O(1)

+

Constraints:
1 <= N <= 106

\ No newline at end of file From 33009433154dcdb5df452f136833a236b8d870e4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 26 Oct 2023 07:51:11 +0530 Subject: [PATCH 0244/1399] Added solution - LeetHub --- .../minimum-operations.cpp | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 Minimum Operations - GFG/minimum-operations.cpp diff --git a/Minimum Operations - GFG/minimum-operations.cpp b/Minimum Operations - GFG/minimum-operations.cpp new file mode 100644 index 00000000..96d6a112 --- /dev/null +++ b/Minimum Operations - GFG/minimum-operations.cpp @@ -0,0 +1,42 @@ +//{ Driver Code Starts +#include +using namespace std; + +// } Driver Code Ends +class Solution +{ + public: + int minOperation(int n) + { + //code here. + + int cnt = 0; + + while(n > 0) + { + if(n & 1) + --n; + else + n /= 2; + + ++cnt; + } + + return cnt; + } +}; + +//{ Driver Code Starts. +int main() +{ + int t; + cin>>t; + while(t--) + { + int n; + cin>>n; + Solution ob; + cout< Date: Thu, 26 Oct 2023 20:54:29 +0530 Subject: [PATCH 0245/1399] Create README - LeetHub --- .../README.md | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div823-binary-trees-with-factors/README.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div823-binary-trees-with-factors/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div823-binary-trees-with-factors/README.md new file mode 100644 index 00000000..7292e9fd --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div823-binary-trees-with-factors/README.md @@ -0,0 +1,28 @@ +

Amazon
2
823. Binary Trees With Factors

Medium


Given an array of unique integers, arr, where each integer arr[i] is strictly greater than 1.

+ +

We make a binary tree using these integers, and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of its children.

+ +

Return the number of binary trees we can make. The answer may be too large so return the answer modulo 109 + 7.

+ +

 

+

Example 1:

+ +
Input: arr = [2,4]
+Output: 3
+Explanation: We can make these trees: [2], [4], [4, 2, 2]
+ +

Example 2:

+ +
Input: arr = [2,4,5,10]
+Output: 7
+Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].
+ +

 

+

Constraints:

+ +
    +
  • 1 <= arr.length <= 1000
  • +
  • 2 <= arr[i] <= 109
  • +
  • All the values of arr are unique.
  • +
+
\ No newline at end of file From 4030ef5a1d10f3cb1da69dab610ddcce0bfcc107 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 26 Oct 2023 20:54:33 +0530 Subject: [PATCH 0246/1399] Time: 72 ms (25.98%), Space: 17.1 MB (11.81%) - LeetHub --- ...v-div-div823-binary-trees-with-factors.cpp | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div823-binary-trees-with-factors/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div823-binary-trees-with-factors.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div823-binary-trees-with-factors/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div823-binary-trees-with-factors.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div823-binary-trees-with-factors/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div823-binary-trees-with-factors.cpp new file mode 100644 index 00000000..d2d96ba0 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div823-binary-trees-with-factors/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div823-binary-trees-with-factors.cpp @@ -0,0 +1,39 @@ +class Solution { +public: + + const int mod = 1e9+7; + + int numFactoredBinaryTrees(vector& arr) { + + int n = arr.size(); + + unordered_map mp; + + for(auto& itr : arr) + ++mp[itr]; + + sort(arr.begin(), arr.end()); + + int ans = 0; + + for(int i = 1; i < n; ++i) + { + for(int j = 0; j < i; ++j) + { + if(arr[i] % arr[j] == 0) + { + long long count = (mp[arr[j]] * 1LL * mp[arr[i]/arr[j]]) % mod; + mp[arr[i]] = (mp[arr[i]] % mod + count)% mod; + } + } + } + + for(auto& itr : mp) + { + ans = (ans + itr.second) % mod; + } + + return ans; + + } +}; \ No newline at end of file From 522ca015c2f27d327cd3e37ccd504cb86896fca9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 26 Oct 2023 20:55:27 +0530 Subject: [PATCH 0247/1399] Create README - LeetHub --- 0823-binary-trees-with-factors/README.md | 28 ++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 0823-binary-trees-with-factors/README.md diff --git a/0823-binary-trees-with-factors/README.md b/0823-binary-trees-with-factors/README.md new file mode 100644 index 00000000..af1c0af6 --- /dev/null +++ b/0823-binary-trees-with-factors/README.md @@ -0,0 +1,28 @@ +

823. Binary Trees With Factors

Medium


Given an array of unique integers, arr, where each integer arr[i] is strictly greater than 1.

+ +

We make a binary tree using these integers, and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of its children.

+ +

Return the number of binary trees we can make. The answer may be too large so return the answer modulo 109 + 7.

+ +

 

+

Example 1:

+ +
Input: arr = [2,4]
+Output: 3
+Explanation: We can make these trees: [2], [4], [4, 2, 2]
+ +

Example 2:

+ +
Input: arr = [2,4,5,10]
+Output: 7
+Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].
+ +

 

+

Constraints:

+ +
    +
  • 1 <= arr.length <= 1000
  • +
  • 2 <= arr[i] <= 109
  • +
  • All the values of arr are unique.
  • +
+
\ No newline at end of file From b859f608a3c23e93c0eb66bcf12631ab166e9f49 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 26 Oct 2023 20:55:27 +0530 Subject: [PATCH 0248/1399] Attach NOTES - LeetHub --- 0823-binary-trees-with-factors/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0823-binary-trees-with-factors/NOTES.md diff --git a/0823-binary-trees-with-factors/NOTES.md b/0823-binary-trees-with-factors/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0823-binary-trees-with-factors/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From ddaad8c7ab9718bf219b829e30c8edb09eb74a65 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 26 Oct 2023 20:55:31 +0530 Subject: [PATCH 0249/1399] Time: 72 ms (25.98%), Space: 17.1 MB (11.81%) - LeetHub --- .../0823-binary-trees-with-factors.cpp | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 0823-binary-trees-with-factors/0823-binary-trees-with-factors.cpp diff --git a/0823-binary-trees-with-factors/0823-binary-trees-with-factors.cpp b/0823-binary-trees-with-factors/0823-binary-trees-with-factors.cpp new file mode 100644 index 00000000..d2d96ba0 --- /dev/null +++ b/0823-binary-trees-with-factors/0823-binary-trees-with-factors.cpp @@ -0,0 +1,39 @@ +class Solution { +public: + + const int mod = 1e9+7; + + int numFactoredBinaryTrees(vector& arr) { + + int n = arr.size(); + + unordered_map mp; + + for(auto& itr : arr) + ++mp[itr]; + + sort(arr.begin(), arr.end()); + + int ans = 0; + + for(int i = 1; i < n; ++i) + { + for(int j = 0; j < i; ++j) + { + if(arr[i] % arr[j] == 0) + { + long long count = (mp[arr[j]] * 1LL * mp[arr[i]/arr[j]]) % mod; + mp[arr[i]] = (mp[arr[i]] % mod + count)% mod; + } + } + } + + for(auto& itr : mp) + { + ans = (ans + itr.second) % mod; + } + + return ans; + + } +}; \ No newline at end of file From b775546062272a79a44785d0f4d6284e63b5ee77 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 27 Oct 2023 07:45:53 +0530 Subject: [PATCH 0250/1399] Create README - LeetHub --- Minimum Deletions - GFG/README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 Minimum Deletions - GFG/README.md diff --git a/Minimum Deletions - GFG/README.md b/Minimum Deletions - GFG/README.md new file mode 100644 index 00000000..c4358768 --- /dev/null +++ b/Minimum Deletions - GFG/README.md @@ -0,0 +1,13 @@ +# Minimum Deletions +## Medium +

Given a string of S as input. Your task is to write a program to delete the minimum number of characters from the string so that the resultant string is a palindrome.
Note: The order of characters in the string should be maintained.

+

Example 1:

+
Input: 
S = "aebcbda" +Output:
2 +Explanation:
Remove characters 'e' and 'd'.
+

Example 2:

+
Input: 
S = "geeksforgeeks" +Output:
8 +Explanation:
One of the possible result string can be "eefee", so answer is 13 - 5 = 8. +
+

Your Task:  
You don't need to read input or print anything. Your task is to complete the function minimumNumberOfDeletions() which takes the string S as inputs and returns the minimum number of deletions required to convert S into a pallindrome.

Expected Time Complexity: O(|S|2)
Expected Auxiliary Space: O(|S|2)

Constraints:
1 ≤ |S| ≤ 103

\ No newline at end of file From 261a824e651cd38bbb1034d7852f6cff091ac696 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 27 Oct 2023 07:45:54 +0530 Subject: [PATCH 0251/1399] Added solution - LeetHub --- Minimum Deletions - GFG/minimum-deletions.cpp | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 Minimum Deletions - GFG/minimum-deletions.cpp diff --git a/Minimum Deletions - GFG/minimum-deletions.cpp b/Minimum Deletions - GFG/minimum-deletions.cpp new file mode 100644 index 00000000..82c5f770 --- /dev/null +++ b/Minimum Deletions - GFG/minimum-deletions.cpp @@ -0,0 +1,60 @@ +//{ Driver Code Starts +#include +using namespace std; + + +// } Driver Code Ends +//User function template for C++ + +class Solution{ + public: + + int helper(int i , int j, int n, string& s, string& t, vector>& dp) + { + if(i == n and j == n) + return 0; + + if(i >= n or j >= n) + return 0; + + if(dp[i][j] != -1) + return dp[i][j]; + + if(s[i] == t[j]) + return dp[i][j] = 1 + helper(i+1, j+1, n, s, t, dp); + else + { + return dp[i][j] = max(helper(i+1, j, n, s, t, dp), helper(i, j+1, n, s, t, dp)); + } + } + + int minimumNumberOfDeletions(string S) { + // code here + + int n = S.size(); + + string t = S; + + reverse(t.begin(), t.end()); + + if(t == S) return 0; + + vector> dp(n+1, vector(n+1, -1)); + + return n - helper(0, 0, n, S, t, dp); + } +}; + +//{ Driver Code Starts. +int main(){ + int t; + cin >> t; + while(t--){ + string S; + cin >> S; + Solution obj; + cout << obj.minimumNumberOfDeletions(S) << endl; + } + return 0; +} +// } Driver Code Ends \ No newline at end of file From 5be25870a241a01d1c6d9362fd5097a4026e7847 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 27 Oct 2023 09:17:25 +0530 Subject: [PATCH 0252/1399] Create README - LeetHub --- .../README.md | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence35-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence18-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence5-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div5-longest-palindromic-substring/README.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence35-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence18-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence5-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div5-longest-palindromic-substring/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence35-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence18-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence5-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div5-longest-palindromic-substring/README.md new file mode 100644 index 00000000..2a2b8010 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence35-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence18-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence5-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div5-longest-palindromic-substring/README.md @@ -0,0 +1,24 @@ +

Amazon
35
Microsoft
18
Google
12
Adobe
12
Facebook
7
Apple
7
Oracle
7
Bloomberg
6
Goldman Sachs
5
5. Longest Palindromic Substring

Medium


Given a string s, return the longest palindromic substring in s.

+ +

 

+

Example 1:

+ +
Input: s = "babad"
+Output: "bab"
+Explanation: "aba" is also a valid answer.
+
+ +

Example 2:

+ +
Input: s = "cbbd"
+Output: "bb"
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 1000
  • +
  • s consist of only digits and English letters.
  • +
+
\ No newline at end of file From 88e1cc468a98b0d13d69f263bdb1b2f58df87c82 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 27 Oct 2023 09:17:25 +0530 Subject: [PATCH 0253/1399] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence35-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence18-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence5-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div5-longest-palindromic-substring/NOTES.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence35-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence18-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence5-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div5-longest-palindromic-substring/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence35-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence18-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence5-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div5-longest-palindromic-substring/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence35-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence18-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence5-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div5-longest-palindromic-substring/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 02b6da1b26fc43dbeb2547e274231ef5c4c8913a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 27 Oct 2023 09:17:29 +0530 Subject: [PATCH 0254/1399] Time: 326 ms (30.26%), Space: 302.9 MB (9.14%) - LeetHub --- ...div-div5-longest-palindromic-substring.cpp | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence35-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence18-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence5-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div5-longest-palindromic-substring/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence35-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence18-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence5-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div5-longest-palindromic-substring.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence35-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence18-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence5-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div5-longest-palindromic-substring/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence35-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence18-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence5-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div5-longest-palindromic-substring.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence35-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence18-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence5-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div5-longest-palindromic-substring/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence35-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence18-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence5-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div5-longest-palindromic-substring.cpp new file mode 100644 index 00000000..b9aeff39 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence35-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence18-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence5-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div5-longest-palindromic-substring/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence35-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence18-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divadobe-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence7-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoldman-sachs-divdiv-classcompanytagscontainer-tagoccurence5-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div5-longest-palindromic-substring.cpp @@ -0,0 +1,40 @@ +class Solution { +public: + string longestPalindrome(string s) { + + int n = s.size(); + + vector> dp(n+1, vector(n+1, 0)); + + int maxLength = 0; + + string ans; + + for(int diff = 0; diff < n; ++ diff) + { + for(int i = 0, j = diff; i < n and j < n; ++i, ++j) + { + if(i == j) + { + dp[i][j] = 1; + } + else if(diff == 1 and s[i] == s[j]) + { + dp[i][j] = 2; + } + else if(diff > 1 and s[i] == s[j] and dp[i+1][j-1]) + { + dp[i][j] = dp[i+1][j-1] + 2; + } + + if(dp[i][j] > maxLength) + { + maxLength = dp[i][j]; + ans = s.substr(i, j-i+1); + } + } + } + + return ans; + } +}; \ No newline at end of file From 4fdcec227b13cbf25be61be8b04639a13bb06f46 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 28 Oct 2023 23:43:06 +0530 Subject: [PATCH 0255/1399] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1220-count-vowels-permutation/NOTES.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1220-count-vowels-permutation/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1220-count-vowels-permutation/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1220-count-vowels-permutation/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 8e8b6c559a06d1481d383267e044fe9f8f4cf358 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 28 Oct 2023 23:43:09 +0530 Subject: [PATCH 0256/1399] Time: 111 ms (18.40%), Space: 91.3 MB (10.40%) - LeetHub --- ...v-div-div1220-count-vowels-permutation.cpp | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1220-count-vowels-permutation/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1220-count-vowels-permutation.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1220-count-vowels-permutation/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1220-count-vowels-permutation.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1220-count-vowels-permutation/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1220-count-vowels-permutation.cpp new file mode 100644 index 00000000..56bada35 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1220-count-vowels-permutation/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1220-count-vowels-permutation.cpp @@ -0,0 +1,55 @@ +class Solution { + unordered_map> mp; + int mod = 1e9+7; + +public: + + int helper(int n ,int i ,char prev, vector>& t) + { + if(i > n) + return 0; + + if(i == n) + { + switch(prev) + { + case 'a': + return 1; + case 'e': + return 2; + case 'i': + return 4; + case 'o': + return 2; + case 'u': + return 1; + default: + return 5; + } + } + + int idx = prev - 'a'; + if(t[i][idx] != -1) + return t[i][idx]; + + long long ans = 0; + for(auto next : mp[prev]) + ans += (helper(n,i+1,next,t) % mod); + + return t[i][idx] = ans % mod; + } + + int countVowelPermutation(int n) { + + mp['c'] = {'a','e','i','o','u'}; + mp['a'] = {'e'}; + mp['e'] = {'a','i'}; + mp['i'] = {'a','e','o','u'}; + mp['o'] = {'i','u'}; + mp['u'] = {'a'}; + + vector> t(n+2,vector(27,-1)); + return helper(n,1,'c',t); + + } +}; \ No newline at end of file From af23a702789dc5b11ff6101a7d1413781bba1b42 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 28 Oct 2023 23:43:49 +0530 Subject: [PATCH 0257/1399] Create README - LeetHub --- 1220-count-vowels-permutation/README.md | 40 +++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 1220-count-vowels-permutation/README.md diff --git a/1220-count-vowels-permutation/README.md b/1220-count-vowels-permutation/README.md new file mode 100644 index 00000000..0eaafd10 --- /dev/null +++ b/1220-count-vowels-permutation/README.md @@ -0,0 +1,40 @@ +

1220. Count Vowels Permutation

Hard


Given an integer n, your task is to count how many strings of length n can be formed under the following rules:

+ +
    +
  • Each character is a lower case vowel ('a', 'e', 'i', 'o', 'u')
  • +
  • Each vowel 'a' may only be followed by an 'e'.
  • +
  • Each vowel 'e' may only be followed by an 'a' or an 'i'.
  • +
  • Each vowel 'i' may not be followed by another 'i'.
  • +
  • Each vowel 'o' may only be followed by an 'i' or a 'u'.
  • +
  • Each vowel 'u' may only be followed by an 'a'.
  • +
+ +

Since the answer may be too large, return it modulo 10^9 + 7.

+ +

 

+

Example 1:

+ +
Input: n = 1
+Output: 5
+Explanation: All possible strings are: "a", "e", "i" , "o" and "u".
+
+ +

Example 2:

+ +
Input: n = 2
+Output: 10
+Explanation: All possible strings are: "ae", "ea", "ei", "ia", "ie", "io", "iu", "oi", "ou" and "ua".
+
+ +

Example 3: 

+ +
Input: n = 5
+Output: 68
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= 2 * 10^4
  • +
+
\ No newline at end of file From a5622e37ccc6fda7c247c3fdaecff39b55389e1b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 28 Oct 2023 23:43:49 +0530 Subject: [PATCH 0258/1399] Attach NOTES - LeetHub --- 1220-count-vowels-permutation/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1220-count-vowels-permutation/NOTES.md diff --git a/1220-count-vowels-permutation/NOTES.md b/1220-count-vowels-permutation/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1220-count-vowels-permutation/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From cc0a7d6238ac5fc4693d79edc5123a4ff107c9ff Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 28 Oct 2023 23:43:53 +0530 Subject: [PATCH 0259/1399] Time: 111 ms (18.40%), Space: 91.3 MB (10.40%) - LeetHub --- .../1220-count-vowels-permutation.cpp | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 1220-count-vowels-permutation/1220-count-vowels-permutation.cpp diff --git a/1220-count-vowels-permutation/1220-count-vowels-permutation.cpp b/1220-count-vowels-permutation/1220-count-vowels-permutation.cpp new file mode 100644 index 00000000..56bada35 --- /dev/null +++ b/1220-count-vowels-permutation/1220-count-vowels-permutation.cpp @@ -0,0 +1,55 @@ +class Solution { + unordered_map> mp; + int mod = 1e9+7; + +public: + + int helper(int n ,int i ,char prev, vector>& t) + { + if(i > n) + return 0; + + if(i == n) + { + switch(prev) + { + case 'a': + return 1; + case 'e': + return 2; + case 'i': + return 4; + case 'o': + return 2; + case 'u': + return 1; + default: + return 5; + } + } + + int idx = prev - 'a'; + if(t[i][idx] != -1) + return t[i][idx]; + + long long ans = 0; + for(auto next : mp[prev]) + ans += (helper(n,i+1,next,t) % mod); + + return t[i][idx] = ans % mod; + } + + int countVowelPermutation(int n) { + + mp['c'] = {'a','e','i','o','u'}; + mp['a'] = {'e'}; + mp['e'] = {'a','i'}; + mp['i'] = {'a','e','o','u'}; + mp['o'] = {'i','u'}; + mp['u'] = {'a'}; + + vector> t(n+2,vector(27,-1)); + return helper(n,1,'c',t); + + } +}; \ No newline at end of file From 3fe5112cd7fc9dffa6588ec818e53100fa702442 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 29 Oct 2023 09:57:07 +0530 Subject: [PATCH 0260/1399] Create README - LeetHub --- .../README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Check whether K-th bit is set or not - GFG/README.md diff --git a/Check whether K-th bit is set or not - GFG/README.md b/Check whether K-th bit is set or not - GFG/README.md new file mode 100644 index 00000000..89eef55a --- /dev/null +++ b/Check whether K-th bit is set or not - GFG/README.md @@ -0,0 +1,17 @@ +# Check whether K-th bit is set or not +## Easy +

Given a number N and a bit number K, check if Kth index bit of N is set or not. A bit is called set if it is 1. Position of set bit '1' should be indexed starting with 0 from LSB side in binary representation of the number.
Note: Index is starting from 0. You just need to return true or false, driver code will take care of printing "Yes" and "No".

+

Example 1:

+
Input: 
N = 4
K = 0 +Output:
No +Explanation:
Binary representation of 4 is 100, in which 0th index bit from LSB is not set. So, return false.
+

Example 2:

+
Input: 
N = 4
K = 2 +Output:
Yes +Explanation:
Binary representation of 4 is 100, in which 2nd index bit from LSB is set. So, return true.
+

Example 3:

+
Input: 
N = 500
K = 3 +Output:
No +Explanation:
Binary representation of 500 is 111110100, in which 3rd index bit from LSB is not set. So, return false.
+
Your task:
+
You don't have to read input or print anything. Your task is to complete the function checkKthbit that takes n and k as parameters and returns either true(if kth bit is set) or false(if kth bit is not set).

Expected Time Complexity:
 O(1).
Expected Auxiliary Space: O(1).


Constraints:
1 ≤ N ≤ 109
0 ≤ K ≤ 31
\ No newline at end of file From 14ef3daa87260bcced057aa7b2cc5fb48b820153 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 29 Oct 2023 09:57:08 +0530 Subject: [PATCH 0261/1399] Added solution - LeetHub --- .../check-whether-kth-bit-is-set-or-not.cpp | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 Check whether K-th bit is set or not - GFG/check-whether-kth-bit-is-set-or-not.cpp diff --git a/Check whether K-th bit is set or not - GFG/check-whether-kth-bit-is-set-or-not.cpp b/Check whether K-th bit is set or not - GFG/check-whether-kth-bit-is-set-or-not.cpp new file mode 100644 index 00000000..8a7874e7 --- /dev/null +++ b/Check whether K-th bit is set or not - GFG/check-whether-kth-bit-is-set-or-not.cpp @@ -0,0 +1,46 @@ +//{ Driver Code Starts +//Initial Template for C++ + +#include +using namespace std; + + +// } Driver Code Ends +//User function Template for C++ + + +class Solution +{ + public: + // Function to check if Kth bit is set or not. + bool checkKthBit(int n, int k) + { + // Your code here + // It can be a one liner logic!! Think of it!! + + return n & (1 << k); + } +}; + +//{ Driver Code Starts. + +// Driver Code +int main() +{ + int t; + cin>>t;//taking testcases + while(t--) + { + long long n; + cin>>n;//input n + int k; + cin>>k;//bit number k + Solution obj; + if(obj.checkKthBit(n, k)) + cout << "Yes" << endl; + else + cout << "No" << endl; + } + return 0; +} +// } Driver Code Ends \ No newline at end of file From bd454914046a2aa5a28fc0b45acb0a52650c0cdf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 29 Oct 2023 12:12:50 +0530 Subject: [PATCH 0262/1399] Create README - LeetHub --- .../README.md | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2919-minimum-increment-operations-to-make-array-beautiful/README.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2919-minimum-increment-operations-to-make-array-beautiful/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2919-minimum-increment-operations-to-make-array-beautiful/README.md new file mode 100644 index 00000000..c5ad89b8 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2919-minimum-increment-operations-to-make-array-beautiful/README.md @@ -0,0 +1,60 @@ +

No companies found for this problem
2919. Minimum Increment Operations to Make Array Beautiful

Medium


You are given a 0-indexed integer array nums having length n, and an integer k.

+ +

You can perform the following increment operation any number of times (including zero):

+ +
    +
  • Choose an index i in the range [0, n - 1], and increase nums[i] by 1.
  • +
+ +

An array is considered beautiful if, for any subarray with a size of 3 or more, its maximum element is greater than or equal to k.

+ +

Return an integer denoting the minimum number of increment operations needed to make nums beautiful.

+ +

A subarray is a contiguous non-empty sequence of elements within an array.

+ +

 

+

Example 1:

+ +
Input: nums = [2,3,0,0,2], k = 4
+Output: 3
+Explanation: We can perform the following increment operations to make nums beautiful:
+Choose index i = 1 and increase nums[1] by 1 -> [2,4,0,0,2].
+Choose index i = 4 and increase nums[4] by 1 -> [2,4,0,0,3].
+Choose index i = 4 and increase nums[4] by 1 -> [2,4,0,0,4].
+The subarrays with a size of 3 or more are: [2,4,0], [4,0,0], [0,0,4], [2,4,0,0], [4,0,0,4], [2,4,0,0,4].
+In all the subarrays, the maximum element is equal to k = 4, so nums is now beautiful.
+It can be shown that nums cannot be made beautiful with fewer than 3 increment operations.
+Hence, the answer is 3.
+
+ +

Example 2:

+ +
Input: nums = [0,1,3,3], k = 5
+Output: 2
+Explanation: We can perform the following increment operations to make nums beautiful:
+Choose index i = 2 and increase nums[2] by 1 -> [0,1,4,3].
+Choose index i = 2 and increase nums[2] by 1 -> [0,1,5,3].
+The subarrays with a size of 3 or more are: [0,1,5], [1,5,3], [0,1,5,3].
+In all the subarrays, the maximum element is equal to k = 5, so nums is now beautiful.
+It can be shown that nums cannot be made beautiful with fewer than 2 increment operations.
+Hence, the answer is 2.
+
+ +

Example 3:

+ +
Input: nums = [1,1,2], k = 1
+Output: 0
+Explanation: The only subarray with a size of 3 or more in this example is [1,1,2].
+The maximum element, 2, is already greater than k = 1, so we don't need any increment operation.
+Hence, the answer is 0.
+
+ +

 

+

Constraints:

+ +
    +
  • 3 <= n == nums.length <= 105
  • +
  • 0 <= nums[i] <= 109
  • +
  • 0 <= k <= 109
  • +
+
\ No newline at end of file From 732b20bb183d46067d717b9a3c14acb922a32b29 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 29 Oct 2023 12:12:50 +0530 Subject: [PATCH 0263/1399] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2919-minimum-increment-operations-to-make-array-beautiful/NOTES.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2919-minimum-increment-operations-to-make-array-beautiful/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2919-minimum-increment-operations-to-make-array-beautiful/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2919-minimum-increment-operations-to-make-array-beautiful/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 16e3e439c1815ac9b9df854a2c28eff15b08c13c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 29 Oct 2023 12:12:54 +0530 Subject: [PATCH 0264/1399] Time: 117 ms (100.00%), Space: 97.4 MB (50.00%) - LeetHub --- ...ent-operations-to-make-array-beautiful.cpp | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2919-minimum-increment-operations-to-make-array-beautiful/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2919-minimum-increment-operations-to-make-array-beautiful.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2919-minimum-increment-operations-to-make-array-beautiful/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2919-minimum-increment-operations-to-make-array-beautiful.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2919-minimum-increment-operations-to-make-array-beautiful/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2919-minimum-increment-operations-to-make-array-beautiful.cpp new file mode 100644 index 00000000..3bde932d --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2919-minimum-increment-operations-to-make-array-beautiful/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2919-minimum-increment-operations-to-make-array-beautiful.cpp @@ -0,0 +1,37 @@ +class Solution { +public: + + long long helper(int idx, int n, vector& nums, int k, vector& dp) + { + if(idx >= n) + return 0; + + long long ans = LONG_MAX; + + if(dp[idx] != -1) + return dp[idx]; + + for(int i = 0; i < 3 and idx + i < n; ++i) + { + long long take = (nums[idx] < k ? k - nums[idx] : 0) + helper(idx+i+1, n, nums, k , dp); + + ans = min(ans, take); + } + + return dp[idx] = ans; + } + + long long minIncrementOperations(vector& nums, int k) { + + int n = nums.size(); + + vector dp(n+1, -1); + + long long op1 = helper(0, n, nums, k, dp); + long long op2 = helper(1, n, nums, k, dp); + long long op3 = helper(2, n, nums, k, dp); + + return min({op1, op2, op3}); + + } +}; \ No newline at end of file From b14ba1439849f3b6768fbef299ce729d7889508c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 29 Oct 2023 12:17:21 +0530 Subject: [PATCH 0265/1399] Create README - LeetHub --- .../README.md | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 2919-minimum-increment-operations-to-make-array-beautiful/README.md diff --git a/2919-minimum-increment-operations-to-make-array-beautiful/README.md b/2919-minimum-increment-operations-to-make-array-beautiful/README.md new file mode 100644 index 00000000..ea73772b --- /dev/null +++ b/2919-minimum-increment-operations-to-make-array-beautiful/README.md @@ -0,0 +1,60 @@ +

2919. Minimum Increment Operations to Make Array Beautiful

Medium


You are given a 0-indexed integer array nums having length n, and an integer k.

+ +

You can perform the following increment operation any number of times (including zero):

+ +
    +
  • Choose an index i in the range [0, n - 1], and increase nums[i] by 1.
  • +
+ +

An array is considered beautiful if, for any subarray with a size of 3 or more, its maximum element is greater than or equal to k.

+ +

Return an integer denoting the minimum number of increment operations needed to make nums beautiful.

+ +

A subarray is a contiguous non-empty sequence of elements within an array.

+ +

 

+

Example 1:

+ +
Input: nums = [2,3,0,0,2], k = 4
+Output: 3
+Explanation: We can perform the following increment operations to make nums beautiful:
+Choose index i = 1 and increase nums[1] by 1 -> [2,4,0,0,2].
+Choose index i = 4 and increase nums[4] by 1 -> [2,4,0,0,3].
+Choose index i = 4 and increase nums[4] by 1 -> [2,4,0,0,4].
+The subarrays with a size of 3 or more are: [2,4,0], [4,0,0], [0,0,4], [2,4,0,0], [4,0,0,4], [2,4,0,0,4].
+In all the subarrays, the maximum element is equal to k = 4, so nums is now beautiful.
+It can be shown that nums cannot be made beautiful with fewer than 3 increment operations.
+Hence, the answer is 3.
+
+ +

Example 2:

+ +
Input: nums = [0,1,3,3], k = 5
+Output: 2
+Explanation: We can perform the following increment operations to make nums beautiful:
+Choose index i = 2 and increase nums[2] by 1 -> [0,1,4,3].
+Choose index i = 2 and increase nums[2] by 1 -> [0,1,5,3].
+The subarrays with a size of 3 or more are: [0,1,5], [1,5,3], [0,1,5,3].
+In all the subarrays, the maximum element is equal to k = 5, so nums is now beautiful.
+It can be shown that nums cannot be made beautiful with fewer than 2 increment operations.
+Hence, the answer is 2.
+
+ +

Example 3:

+ +
Input: nums = [1,1,2], k = 1
+Output: 0
+Explanation: The only subarray with a size of 3 or more in this example is [1,1,2].
+The maximum element, 2, is already greater than k = 1, so we don't need any increment operation.
+Hence, the answer is 0.
+
+ +

 

+

Constraints:

+ +
    +
  • 3 <= n == nums.length <= 105
  • +
  • 0 <= nums[i] <= 109
  • +
  • 0 <= k <= 109
  • +
+
\ No newline at end of file From bf3509c9489acaf30b08eaa2bab53f850c8b79ea Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 30 Oct 2023 01:04:08 +0530 Subject: [PATCH 0266/1399] Create README - LeetHub --- .../README.md | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div458-poor-pigs/README.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div458-poor-pigs/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div458-poor-pigs/README.md new file mode 100644 index 00000000..5b73b537 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div458-poor-pigs/README.md @@ -0,0 +1,48 @@ +

No companies found for this problem
458. Poor Pigs

Hard


There are buckets buckets of liquid, where exactly one of the buckets is poisonous. To figure out which one is poisonous, you feed some number of (poor) pigs the liquid to see whether they will die or not. Unfortunately, you only have minutesToTest minutes to determine which bucket is poisonous.

+ +

You can feed the pigs according to these steps:

+ +
    +
  1. Choose some live pigs to feed.
  2. +
  3. For each pig, choose which buckets to feed it. The pig will consume all the chosen buckets simultaneously and will take no time. Each pig can feed from any number of buckets, and each bucket can be fed from by any number of pigs.
  4. +
  5. Wait for minutesToDie minutes. You may not feed any other pigs during this time.
  6. +
  7. After minutesToDie minutes have passed, any pigs that have been fed the poisonous bucket will die, and all others will survive.
  8. +
  9. Repeat this process until you run out of time.
  10. +
+ +

Given buckets, minutesToDie, and minutesToTest, return the minimum number of pigs needed to figure out which bucket is poisonous within the allotted time.

+ +

 

+

Example 1:

+ +
Input: buckets = 4, minutesToDie = 15, minutesToTest = 15
+Output: 2
+Explanation: We can determine the poisonous bucket as follows:
+At time 0, feed the first pig buckets 1 and 2, and feed the second pig buckets 2 and 3.
+At time 15, there are 4 possible outcomes:
+- If only the first pig dies, then bucket 1 must be poisonous.
+- If only the second pig dies, then bucket 3 must be poisonous.
+- If both pigs die, then bucket 2 must be poisonous.
+- If neither pig dies, then bucket 4 must be poisonous.
+
+ +

Example 2:

+ +
Input: buckets = 4, minutesToDie = 15, minutesToTest = 30
+Output: 2
+Explanation: We can determine the poisonous bucket as follows:
+At time 0, feed the first pig bucket 1, and feed the second pig bucket 2.
+At time 15, there are 2 possible outcomes:
+- If either pig dies, then the poisonous bucket is the one it was fed.
+- If neither pig dies, then feed the first pig bucket 3, and feed the second pig bucket 4.
+At time 30, one of the two pigs must die, and the poisonous bucket is the one it was fed.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= buckets <= 1000
  • +
  • 1 <= minutesToDie <= minutesToTest <= 100
  • +
+
\ No newline at end of file From 7735275bea10eadc650f2268c626697eaf5150f7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 30 Oct 2023 01:04:09 +0530 Subject: [PATCH 0267/1399] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div458-poor-pigs/NOTES.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div458-poor-pigs/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div458-poor-pigs/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div458-poor-pigs/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 0f16aba7c3368dd53ea1d827a7d8e98049f69f76 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 30 Oct 2023 01:04:12 +0530 Subject: [PATCH 0268/1399] Time: 2 ms (52.97%), Space: 6.5 MB (26.69%) - LeetHub --- ...ht32-rect-svg-div-div-div-div458-poor-pigs.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div458-poor-pigs/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div458-poor-pigs.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div458-poor-pigs/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div458-poor-pigs.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div458-poor-pigs/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div458-poor-pigs.cpp new file mode 100644 index 00000000..001c90e8 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div458-poor-pigs/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div458-poor-pigs.cpp @@ -0,0 +1,15 @@ +class Solution { +public: + int poorPigs(int buckets, int minutesToDie, int minutesToTest) { + + int trial = (minutesToTest / minutesToDie) + 1; + + int pigs = 0; + + while(pow(trial, pigs) < buckets) + ++pigs; + + return pigs; + + } +}; \ No newline at end of file From ed91e30e3a206eaf8028c47e69450eda0e7c8cff Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 30 Oct 2023 01:09:12 +0530 Subject: [PATCH 0269/1399] Attach NOTES - LeetHub From 7b44b0ccd85ff56eb8e40a709147b957b6e35c97 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 30 Oct 2023 01:09:16 +0530 Subject: [PATCH 0270/1399] Time: 2 ms (52.97%), Space: 6.4 MB (58.05%) - LeetHub --- ...-height32-rect-svg-div-div-div-div458-poor-pigs.cpp | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div458-poor-pigs/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div458-poor-pigs.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div458-poor-pigs/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div458-poor-pigs.cpp index 001c90e8..c9a68d06 100644 --- a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div458-poor-pigs/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div458-poor-pigs.cpp +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div458-poor-pigs/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div458-poor-pigs.cpp @@ -2,14 +2,8 @@ class Solution { public: int poorPigs(int buckets, int minutesToDie, int minutesToTest) { - int trial = (minutesToTest / minutesToDie) + 1; - - int pigs = 0; - - while(pow(trial, pigs) < buckets) - ++pigs; - - return pigs; + // trial^pigs = buckets + return ceil(log2(buckets) / log2(minutesToTest / minutesToDie + 1)); } }; \ No newline at end of file From 915a9bd8323bdd004537c4a46137474a47da7abf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 30 Oct 2023 07:38:57 +0530 Subject: [PATCH 0271/1399] Create README - LeetHub --- .../README.md | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divinfosys-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1356-sort-integers-by-the-number-of-1-bits/README.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divinfosys-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1356-sort-integers-by-the-number-of-1-bits/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divinfosys-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1356-sort-integers-by-the-number-of-1-bits/README.md new file mode 100644 index 00000000..b5d4b137 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divinfosys-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1356-sort-integers-by-the-number-of-1-bits/README.md @@ -0,0 +1,31 @@ +

Infosys
2
1356. Sort Integers by The Number of 1 Bits

Easy


You are given an integer array arr. Sort the integers in the array in ascending order by the number of 1's in their binary representation and in case of two or more integers have the same number of 1's you have to sort them in ascending order.

+ +

Return the array after sorting it.

+ +

 

+

Example 1:

+ +
Input: arr = [0,1,2,3,4,5,6,7,8]
+Output: [0,1,2,4,8,3,5,6,7]
+Explantion: [0] is the only integer with 0 bits.
+[1,2,4,8] all have 1 bit.
+[3,5,6] have 2 bits.
+[7] has 3 bits.
+The sorted array by bits is [0,1,2,4,8,3,5,6,7]
+
+ +

Example 2:

+ +
Input: arr = [1024,512,256,128,64,32,16,8,4,2,1]
+Output: [1,2,4,8,16,32,64,128,256,512,1024]
+Explantion: All integers have 1 bit in the binary representation, you should just sort them in ascending order.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= arr.length <= 500
  • +
  • 0 <= arr[i] <= 104
  • +
+
\ No newline at end of file From 56c713c60722b98593735accb0eb50448cc9c5b6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 30 Oct 2023 07:39:00 +0530 Subject: [PATCH 0272/1399] Time: 9 ms (43.13%), Space: 10.5 MB (66.29%) - LeetHub --- ...356-sort-integers-by-the-number-of-1-bits.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divinfosys-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1356-sort-integers-by-the-number-of-1-bits/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divinfosys-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1356-sort-integers-by-the-number-of-1-bits.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divinfosys-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1356-sort-integers-by-the-number-of-1-bits/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divinfosys-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1356-sort-integers-by-the-number-of-1-bits.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divinfosys-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1356-sort-integers-by-the-number-of-1-bits/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divinfosys-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1356-sort-integers-by-the-number-of-1-bits.cpp new file mode 100644 index 00000000..efe6dfbd --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divinfosys-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1356-sort-integers-by-the-number-of-1-bits/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divinfosys-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1356-sort-integers-by-the-number-of-1-bits.cpp @@ -0,0 +1,16 @@ +class Solution { +public: + vector sortByBits(vector& arr) { + + sort(arr.begin(), arr.end(), [&](int& a, int & b) + { + int cntA = __builtin_popcount(a); + int cntB = __builtin_popcount(b); + + return (cntA == cntB ? a < b : cntA < cntB); + }); + + return arr; + + } +}; \ No newline at end of file From f85c8a12b29cb55981443891f92f82c537039dda Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 31 Oct 2023 18:02:41 +0530 Subject: [PATCH 0273/1399] Create README - LeetHub --- .../README.md | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 2433-find-the-original-array-of-prefix-xor/README.md diff --git a/2433-find-the-original-array-of-prefix-xor/README.md b/2433-find-the-original-array-of-prefix-xor/README.md new file mode 100644 index 00000000..380a3f99 --- /dev/null +++ b/2433-find-the-original-array-of-prefix-xor/README.md @@ -0,0 +1,38 @@ +

2433. Find The Original Array of Prefix Xor

Medium


You are given an integer array pref of size n. Find and return the array arr of size n that satisfies:

+ +
    +
  • pref[i] = arr[0] ^ arr[1] ^ ... ^ arr[i].
  • +
+ +

Note that ^ denotes the bitwise-xor operation.

+ +

It can be proven that the answer is unique.

+ +

 

+

Example 1:

+ +
Input: pref = [5,2,0,3,1]
+Output: [5,7,2,3,2]
+Explanation: From the array [5,7,2,3,2] we have the following:
+- pref[0] = 5.
+- pref[1] = 5 ^ 7 = 2.
+- pref[2] = 5 ^ 7 ^ 2 = 0.
+- pref[3] = 5 ^ 7 ^ 2 ^ 3 = 3.
+- pref[4] = 5 ^ 7 ^ 2 ^ 3 ^ 2 = 1.
+
+ +

Example 2:

+ +
Input: pref = [13]
+Output: [13]
+Explanation: We have pref[0] = arr[0] = 13.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= pref.length <= 105
  • +
  • 0 <= pref[i] <= 106
  • +
+
\ No newline at end of file From 005f92f844126ac878e7ac3e950093c0ea0ec25d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 31 Oct 2023 18:02:45 +0530 Subject: [PATCH 0274/1399] Time: 90 ms (41.60%), Space: 76.4 MB (69.22%) - LeetHub --- ...-find-the-original-array-of-prefix-xor.cpp | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 2433-find-the-original-array-of-prefix-xor/2433-find-the-original-array-of-prefix-xor.cpp diff --git a/2433-find-the-original-array-of-prefix-xor/2433-find-the-original-array-of-prefix-xor.cpp b/2433-find-the-original-array-of-prefix-xor/2433-find-the-original-array-of-prefix-xor.cpp new file mode 100644 index 00000000..d6984685 --- /dev/null +++ b/2433-find-the-original-array-of-prefix-xor/2433-find-the-original-array-of-prefix-xor.cpp @@ -0,0 +1,31 @@ +class Solution { +public: + vector findArray(vector& pref) { + + int n = pref.size(); + + vector ans(n); + + ans[0] = pref[0]; + + int currXor = pref[0]; + + for(int i = 1; i < n; ++i) + { + int mask = 0; + int newXor = pref[i]; + + for(int i = 0; i < 32; ++i) + { + if(!(currXor & (1 << i)) and (newXor & (1 << i))) + mask |= (1< Date: Tue, 31 Oct 2023 18:05:04 +0530 Subject: [PATCH 0275/1399] Attach NOTES - LeetHub --- 2433-find-the-original-array-of-prefix-xor/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2433-find-the-original-array-of-prefix-xor/NOTES.md diff --git a/2433-find-the-original-array-of-prefix-xor/NOTES.md b/2433-find-the-original-array-of-prefix-xor/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2433-find-the-original-array-of-prefix-xor/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From d28d4be808bac4dd69ea69aae83123662bd0ab39 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 31 Oct 2023 18:05:08 +0530 Subject: [PATCH 0276/1399] Time: 90 ms (41.60%), Space: 76.4 MB (69.22%) - LeetHub From bb2dfaabfb99ef3d61a6b3e7355e0a5352cbb677 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 31 Oct 2023 18:08:30 +0530 Subject: [PATCH 0277/1399] Create README - LeetHub --- .../README.md | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2433-find-the-original-array-of-prefix-xor/README.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2433-find-the-original-array-of-prefix-xor/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2433-find-the-original-array-of-prefix-xor/README.md new file mode 100644 index 00000000..a6752d89 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2433-find-the-original-array-of-prefix-xor/README.md @@ -0,0 +1,38 @@ +

No companies found for this problem
2433. Find The Original Array of Prefix Xor

Medium


You are given an integer array pref of size n. Find and return the array arr of size n that satisfies:

+ +
    +
  • pref[i] = arr[0] ^ arr[1] ^ ... ^ arr[i].
  • +
+ +

Note that ^ denotes the bitwise-xor operation.

+ +

It can be proven that the answer is unique.

+ +

 

+

Example 1:

+ +
Input: pref = [5,2,0,3,1]
+Output: [5,7,2,3,2]
+Explanation: From the array [5,7,2,3,2] we have the following:
+- pref[0] = 5.
+- pref[1] = 5 ^ 7 = 2.
+- pref[2] = 5 ^ 7 ^ 2 = 0.
+- pref[3] = 5 ^ 7 ^ 2 ^ 3 = 3.
+- pref[4] = 5 ^ 7 ^ 2 ^ 3 ^ 2 = 1.
+
+ +

Example 2:

+ +
Input: pref = [13]
+Output: [13]
+Explanation: We have pref[0] = arr[0] = 13.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= pref.length <= 105
  • +
  • 0 <= pref[i] <= 106
  • +
+
\ No newline at end of file From 3dbc23c4c40d1a9533ad2fbf0c41919cdb09fe21 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 31 Oct 2023 18:08:31 +0530 Subject: [PATCH 0278/1399] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2433-find-the-original-array-of-prefix-xor/NOTES.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2433-find-the-original-array-of-prefix-xor/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2433-find-the-original-array-of-prefix-xor/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2433-find-the-original-array-of-prefix-xor/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 2ed39c470ad5f6135b35b4560d6753a22b779b40 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 31 Oct 2023 18:08:34 +0530 Subject: [PATCH 0279/1399] Time: 96 ms (18.61%), Space: 76.3 MB (69.22%) - LeetHub --- ...433-find-the-original-array-of-prefix-xor.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2433-find-the-original-array-of-prefix-xor/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2433-find-the-original-array-of-prefix-xor.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2433-find-the-original-array-of-prefix-xor/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2433-find-the-original-array-of-prefix-xor.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2433-find-the-original-array-of-prefix-xor/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2433-find-the-original-array-of-prefix-xor.cpp new file mode 100644 index 00000000..1151b9bd --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2433-find-the-original-array-of-prefix-xor/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2433-find-the-original-array-of-prefix-xor.cpp @@ -0,0 +1,16 @@ +class Solution { +public: + vector findArray(vector& pref) { + + int n = pref.size(); + + vector ans(n); + + ans[0] = pref[0]; + + for(int i = 1; i < n; ++i) + ans[i] = pref[i] ^ pref[i-1]; + + return ans; + } +}; \ No newline at end of file From 9c966f5a7401d1dbb2fee4b1f3eef9f081c9841e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 31 Oct 2023 18:10:23 +0530 Subject: [PATCH 0280/1399] Attach NOTES - LeetHub From e056ea9ba6e4b195619689db8b4702b7ddb2812e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 1 Nov 2023 08:07:36 +0530 Subject: [PATCH 0281/1399] Create README - LeetHub --- .../README.md | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div501-find-mode-in-binary-search-tree/README.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div501-find-mode-in-binary-search-tree/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div501-find-mode-in-binary-search-tree/README.md new file mode 100644 index 00000000..184716c6 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div501-find-mode-in-binary-search-tree/README.md @@ -0,0 +1,35 @@ +

No companies found for this problem
501. Find Mode in Binary Search Tree

Easy


Given the root of a binary search tree (BST) with duplicates, return all the mode(s) (i.e., the most frequently occurred element) in it.

+ +

If the tree has more than one mode, return them in any order.

+ +

Assume a BST is defined as follows:

+ +
    +
  • The left subtree of a node contains only nodes with keys less than or equal to the node's key.
  • +
  • The right subtree of a node contains only nodes with keys greater than or equal to the node's key.
  • +
  • Both the left and right subtrees must also be binary search trees.
  • +
+ +

 

+

Example 1:

+ +
Input: root = [1,null,2,2]
+Output: [2]
+
+ +

Example 2:

+ +
Input: root = [0]
+Output: [0]
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is in the range [1, 104].
  • +
  • -105 <= Node.val <= 105
  • +
+ +

 

+Follow up: Could you do that without using any extra space? (Assume that the implicit stack space incurred due to recursion does not count).
\ No newline at end of file From 7e7b9ab58a7b1886b48be5729af42654c14b775a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 1 Nov 2023 08:07:40 +0530 Subject: [PATCH 0282/1399] Time: 8 ms (96.00%), Space: 25.6 MB (44.12%) - LeetHub --- ...div501-find-mode-in-binary-search-tree.cpp | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div501-find-mode-in-binary-search-tree/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div501-find-mode-in-binary-search-tree.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div501-find-mode-in-binary-search-tree/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div501-find-mode-in-binary-search-tree.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div501-find-mode-in-binary-search-tree/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div501-find-mode-in-binary-search-tree.cpp new file mode 100644 index 00000000..e9971d98 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div501-find-mode-in-binary-search-tree/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div501-find-mode-in-binary-search-tree.cpp @@ -0,0 +1,44 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + + void helper(TreeNode* root, unordered_map& mp, int& maxi) + { + if(root) + { + helper(root->left, mp, maxi); + ++mp[root->val]; + maxi = max(maxi, mp[root->val]); + helper(root->right, mp, maxi); + } + } + + vector findMode(TreeNode* root) { + + unordered_map mp; + + int maxi = 0; + + helper(root, mp, maxi); + + vector ans; + + for(auto& itr : mp) + { + if(itr.second == maxi) + ans.push_back(itr.first); + } + + return ans; + } +}; \ No newline at end of file From 03acbc1a85860502f984499f7b9e0b8889298593 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 1 Nov 2023 08:13:31 +0530 Subject: [PATCH 0283/1399] Create README - LeetHub --- .../README.md | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 0501-find-mode-in-binary-search-tree/README.md diff --git a/0501-find-mode-in-binary-search-tree/README.md b/0501-find-mode-in-binary-search-tree/README.md new file mode 100644 index 00000000..eaf7c94d --- /dev/null +++ b/0501-find-mode-in-binary-search-tree/README.md @@ -0,0 +1,35 @@ +

501. Find Mode in Binary Search Tree

Easy


Given the root of a binary search tree (BST) with duplicates, return all the mode(s) (i.e., the most frequently occurred element) in it.

+ +

If the tree has more than one mode, return them in any order.

+ +

Assume a BST is defined as follows:

+ +
    +
  • The left subtree of a node contains only nodes with keys less than or equal to the node's key.
  • +
  • The right subtree of a node contains only nodes with keys greater than or equal to the node's key.
  • +
  • Both the left and right subtrees must also be binary search trees.
  • +
+ +

 

+

Example 1:

+ +
Input: root = [1,null,2,2]
+Output: [2]
+
+ +

Example 2:

+ +
Input: root = [0]
+Output: [0]
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is in the range [1, 104].
  • +
  • -105 <= Node.val <= 105
  • +
+ +

 

+Follow up: Could you do that without using any extra space? (Assume that the implicit stack space incurred due to recursion does not count).
\ No newline at end of file From 9de76c53349f02eefecf5a6eacf15bc832500dd8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 1 Nov 2023 08:13:34 +0530 Subject: [PATCH 0284/1399] Time: 8 ms (96.00%), Space: 25.6 MB (44.12%) - LeetHub --- .../0501-find-mode-in-binary-search-tree.cpp | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 0501-find-mode-in-binary-search-tree/0501-find-mode-in-binary-search-tree.cpp diff --git a/0501-find-mode-in-binary-search-tree/0501-find-mode-in-binary-search-tree.cpp b/0501-find-mode-in-binary-search-tree/0501-find-mode-in-binary-search-tree.cpp new file mode 100644 index 00000000..e9971d98 --- /dev/null +++ b/0501-find-mode-in-binary-search-tree/0501-find-mode-in-binary-search-tree.cpp @@ -0,0 +1,44 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + + void helper(TreeNode* root, unordered_map& mp, int& maxi) + { + if(root) + { + helper(root->left, mp, maxi); + ++mp[root->val]; + maxi = max(maxi, mp[root->val]); + helper(root->right, mp, maxi); + } + } + + vector findMode(TreeNode* root) { + + unordered_map mp; + + int maxi = 0; + + helper(root, mp, maxi); + + vector ans; + + for(auto& itr : mp) + { + if(itr.second == maxi) + ans.push_back(itr.first); + } + + return ans; + } +}; \ No newline at end of file From c7124572bca7e0b00b1251323aca1c38aea3f5a8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 2 Nov 2023 08:38:01 +0530 Subject: [PATCH 0285/1399] Create README - LeetHub --- .../README.md | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2265-count-nodes-equal-to-average-of-subtree/README.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2265-count-nodes-equal-to-average-of-subtree/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2265-count-nodes-equal-to-average-of-subtree/README.md new file mode 100644 index 00000000..42ee90a0 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2265-count-nodes-equal-to-average-of-subtree/README.md @@ -0,0 +1,37 @@ +

No companies found for this problem
2265. Count Nodes Equal to Average of Subtree

Medium


Given the root of a binary tree, return the number of nodes where the value of the node is equal to the average of the values in its subtree.

+ +

Note:

+ +
    +
  • The average of n elements is the sum of the n elements divided by n and rounded down to the nearest integer.
  • +
  • A subtree of root is a tree consisting of root and all of its descendants.
  • +
+ +

 

+

Example 1:

+ +
Input: root = [4,8,5,0,1,null,6]
+Output: 5
+Explanation: 
+For the node with value 4: The average of its subtree is (4 + 8 + 5 + 0 + 1 + 6) / 6 = 24 / 6 = 4.
+For the node with value 5: The average of its subtree is (5 + 6) / 2 = 11 / 2 = 5.
+For the node with value 0: The average of its subtree is 0 / 1 = 0.
+For the node with value 1: The average of its subtree is 1 / 1 = 1.
+For the node with value 6: The average of its subtree is 6 / 1 = 6.
+
+ +

Example 2:

+ +
Input: root = [1]
+Output: 1
+Explanation: For the node with value 1: The average of its subtree is 1 / 1 = 1.
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is in the range [1, 1000].
  • +
  • 0 <= Node.val <= 1000
  • +
+
\ No newline at end of file From 033bb780ac7b19dbfd4a4ccf392d41eea74d5ca4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 2 Nov 2023 08:38:05 +0530 Subject: [PATCH 0286/1399] Time: 0 ms (100.00%), Space: 12.2 MB (71.89%) - LeetHub --- ...ount-nodes-equal-to-average-of-subtree.cpp | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2265-count-nodes-equal-to-average-of-subtree/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2265-count-nodes-equal-to-average-of-subtree.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2265-count-nodes-equal-to-average-of-subtree/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2265-count-nodes-equal-to-average-of-subtree.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2265-count-nodes-equal-to-average-of-subtree/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2265-count-nodes-equal-to-average-of-subtree.cpp new file mode 100644 index 00000000..19d7070b --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2265-count-nodes-equal-to-average-of-subtree/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2265-count-nodes-equal-to-average-of-subtree.cpp @@ -0,0 +1,47 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + + int ans = 0; + + pair helper(TreeNode* root) + { + if(!root) + { + return {0, 0}; + } + + auto left = helper(root->left); + auto right = helper(root->right); + + int sum = left.first + right.first + root->val; + int cnt = left.second + right.second + 1; + + // cout<val<<" " <val) + { + ++ans; + } + + return {sum, cnt}; + } + + int averageOfSubtree(TreeNode* root) { + + helper(root); + + return ans; + + } +}; \ No newline at end of file From 650c4efcecf6deb3cda41014cc87d4d58319f2ff Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 2 Nov 2023 08:41:27 +0530 Subject: [PATCH 0287/1399] Create README - LeetHub --- .../README.md | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 2265-count-nodes-equal-to-average-of-subtree/README.md diff --git a/2265-count-nodes-equal-to-average-of-subtree/README.md b/2265-count-nodes-equal-to-average-of-subtree/README.md new file mode 100644 index 00000000..d9972242 --- /dev/null +++ b/2265-count-nodes-equal-to-average-of-subtree/README.md @@ -0,0 +1,37 @@ +

2265. Count Nodes Equal to Average of Subtree

Medium


Given the root of a binary tree, return the number of nodes where the value of the node is equal to the average of the values in its subtree.

+ +

Note:

+ +
    +
  • The average of n elements is the sum of the n elements divided by n and rounded down to the nearest integer.
  • +
  • A subtree of root is a tree consisting of root and all of its descendants.
  • +
+ +

 

+

Example 1:

+ +
Input: root = [4,8,5,0,1,null,6]
+Output: 5
+Explanation: 
+For the node with value 4: The average of its subtree is (4 + 8 + 5 + 0 + 1 + 6) / 6 = 24 / 6 = 4.
+For the node with value 5: The average of its subtree is (5 + 6) / 2 = 11 / 2 = 5.
+For the node with value 0: The average of its subtree is 0 / 1 = 0.
+For the node with value 1: The average of its subtree is 1 / 1 = 1.
+For the node with value 6: The average of its subtree is 6 / 1 = 6.
+
+ +

Example 2:

+ +
Input: root = [1]
+Output: 1
+Explanation: For the node with value 1: The average of its subtree is 1 / 1 = 1.
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is in the range [1, 1000].
  • +
  • 0 <= Node.val <= 1000
  • +
+
\ No newline at end of file From 5e6a7280080edb9562e521ad11cbc67459292c49 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 2 Nov 2023 08:41:31 +0530 Subject: [PATCH 0288/1399] Time: 0 ms (100.00%), Space: 12.2 MB (71.89%) - LeetHub --- ...ount-nodes-equal-to-average-of-subtree.cpp | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 2265-count-nodes-equal-to-average-of-subtree/2265-count-nodes-equal-to-average-of-subtree.cpp diff --git a/2265-count-nodes-equal-to-average-of-subtree/2265-count-nodes-equal-to-average-of-subtree.cpp b/2265-count-nodes-equal-to-average-of-subtree/2265-count-nodes-equal-to-average-of-subtree.cpp new file mode 100644 index 00000000..19d7070b --- /dev/null +++ b/2265-count-nodes-equal-to-average-of-subtree/2265-count-nodes-equal-to-average-of-subtree.cpp @@ -0,0 +1,47 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + + int ans = 0; + + pair helper(TreeNode* root) + { + if(!root) + { + return {0, 0}; + } + + auto left = helper(root->left); + auto right = helper(root->right); + + int sum = left.first + right.first + root->val; + int cnt = left.second + right.second + 1; + + // cout<val<<" " <val) + { + ++ans; + } + + return {sum, cnt}; + } + + int averageOfSubtree(TreeNode* root) { + + helper(root); + + return ans; + + } +}; \ No newline at end of file From 74bb140e5d04285514e269890e08ed90d207bd75 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 3 Nov 2023 09:26:04 +0530 Subject: [PATCH 0289/1399] Create README - LeetHub --- .../README.md | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1441-build-an-array-with-stack-operations/README.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1441-build-an-array-with-stack-operations/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1441-build-an-array-with-stack-operations/README.md new file mode 100644 index 00000000..051d4f2d --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1441-build-an-array-with-stack-operations/README.md @@ -0,0 +1,64 @@ +

Google
2
1441. Build an Array With Stack Operations

Medium


You are given an integer array target and an integer n.

+ +

You have an empty stack with the two following operations:

+ +
    +
  • "Push": pushes an integer to the top of the stack.
  • +
  • "Pop": removes the integer on the top of the stack.
  • +
+ +

You also have a stream of the integers in the range [1, n].

+ +

Use the two stack operations to make the numbers in the stack (from the bottom to the top) equal to target. You should follow the following rules:

+ +
    +
  • If the stream of the integers is not empty, pick the next integer from the stream and push it to the top of the stack.
  • +
  • If the stack is not empty, pop the integer at the top of the stack.
  • +
  • If, at any moment, the elements in the stack (from the bottom to the top) are equal to target, do not read new integers from the stream and do not do more operations on the stack.
  • +
+ +

Return the stack operations needed to build target following the mentioned rules. If there are multiple valid answers, return any of them.

+ +

 

+

Example 1:

+ +
Input: target = [1,3], n = 3
+Output: ["Push","Push","Pop","Push"]
+Explanation: Initially the stack s is empty. The last element is the top of the stack.
+Read 1 from the stream and push it to the stack. s = [1].
+Read 2 from the stream and push it to the stack. s = [1,2].
+Pop the integer on the top of the stack. s = [1].
+Read 3 from the stream and push it to the stack. s = [1,3].
+
+ +

Example 2:

+ +
Input: target = [1,2,3], n = 3
+Output: ["Push","Push","Push"]
+Explanation: Initially the stack s is empty. The last element is the top of the stack.
+Read 1 from the stream and push it to the stack. s = [1].
+Read 2 from the stream and push it to the stack. s = [1,2].
+Read 3 from the stream and push it to the stack. s = [1,2,3].
+
+ +

Example 3:

+ +
Input: target = [1,2], n = 4
+Output: ["Push","Push"]
+Explanation: Initially the stack s is empty. The last element is the top of the stack.
+Read 1 from the stream and push it to the stack. s = [1].
+Read 2 from the stream and push it to the stack. s = [1,2].
+Since the stack (from the bottom to the top) is equal to target, we stop the stack operations.
+The answers that read integer 3 from the stream are not accepted.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= target.length <= 100
  • +
  • 1 <= n <= 100
  • +
  • 1 <= target[i] <= n
  • +
  • target is strictly increasing.
  • +
+
\ No newline at end of file From 6c129c1e685f156f1bcab93b16ecec8c60bf0cf5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 3 Nov 2023 09:26:08 +0530 Subject: [PATCH 0290/1399] Time: 0 ms (100.00%), Space: 8.4 MB (11.73%) - LeetHub --- ...1-build-an-array-with-stack-operations.cpp | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1441-build-an-array-with-stack-operations/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1441-build-an-array-with-stack-operations.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1441-build-an-array-with-stack-operations/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1441-build-an-array-with-stack-operations.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1441-build-an-array-with-stack-operations/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1441-build-an-array-with-stack-operations.cpp new file mode 100644 index 00000000..38db9baf --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1441-build-an-array-with-stack-operations/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1441-build-an-array-with-stack-operations.cpp @@ -0,0 +1,26 @@ +class Solution { +public: + vector buildArray(vector& target, int n) { + + vector ans; + + int k = 0; + + for(int i = 1; i <= n and k < target.size(); ++i) + { + if(target[k] == i) + { + ans.push_back("Push"); + ++k; + } + else + { + ans.push_back("Push"); + ans.push_back("Pop"); + } + } + + return ans; + + } +}; \ No newline at end of file From 0c74378e78668084ff8ed8b32084e3063966af0c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 4 Nov 2023 19:56:06 +0530 Subject: [PATCH 0291/1399] Create README - LeetHub --- .../README.md | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 1503-last-moment-before-all-ants-fall-out-of-a-plank/README.md diff --git a/1503-last-moment-before-all-ants-fall-out-of-a-plank/README.md b/1503-last-moment-before-all-ants-fall-out-of-a-plank/README.md new file mode 100644 index 00000000..0087fe01 --- /dev/null +++ b/1503-last-moment-before-all-ants-fall-out-of-a-plank/README.md @@ -0,0 +1,48 @@ +

1503. Last Moment Before All Ants Fall Out of a Plank

Medium


We have a wooden plank of the length n units. Some ants are walking on the plank, each ant moves with a speed of 1 unit per second. Some of the ants move to the left, the other move to the right.

+ +

When two ants moving in two different directions meet at some point, they change their directions and continue moving again. Assume changing directions does not take any additional time.

+ +

When an ant reaches one end of the plank at a time t, it falls out of the plank immediately.

+ +

Given an integer n and two integer arrays left and right, the positions of the ants moving to the left and the right, return the moment when the last ant(s) fall out of the plank.

+ +

 

+

Example 1:

+ +
Input: n = 4, left = [4,3], right = [0,1]
+Output: 4
+Explanation: In the image above:
+-The ant at index 0 is named A and going to the right.
+-The ant at index 1 is named B and going to the right.
+-The ant at index 3 is named C and going to the left.
+-The ant at index 4 is named D and going to the left.
+The last moment when an ant was on the plank is t = 4 seconds. After that, it falls immediately out of the plank. (i.e., We can say that at t = 4.0000000001, there are no ants on the plank).
+
+ +

Example 2:

+ +
Input: n = 7, left = [], right = [0,1,2,3,4,5,6,7]
+Output: 7
+Explanation: All ants are going to the right, the ant at index 0 needs 7 seconds to fall.
+
+ +

Example 3:

+ +
Input: n = 7, left = [0,1,2,3,4,5,6,7], right = []
+Output: 7
+Explanation: All ants are going to the left, the ant at index 7 needs 7 seconds to fall.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= 104
  • +
  • 0 <= left.length <= n + 1
  • +
  • 0 <= left[i] <= n
  • +
  • 0 <= right.length <= n + 1
  • +
  • 0 <= right[i] <= n
  • +
  • 1 <= left.length + right.length <= n + 1
  • +
  • All values of left and right are unique, and each value can appear only in one of the two arrays.
  • +
+
\ No newline at end of file From 124a82e0ce9a4c73068e3790355ec9f6e9e50423 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 4 Nov 2023 19:56:10 +0530 Subject: [PATCH 0292/1399] Time: 19 ms (48.28%), Space: 23.2 MB (31.47%) - LeetHub --- ...nt-before-all-ants-fall-out-of-a-plank.cpp | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 1503-last-moment-before-all-ants-fall-out-of-a-plank/1503-last-moment-before-all-ants-fall-out-of-a-plank.cpp diff --git a/1503-last-moment-before-all-ants-fall-out-of-a-plank/1503-last-moment-before-all-ants-fall-out-of-a-plank.cpp b/1503-last-moment-before-all-ants-fall-out-of-a-plank/1503-last-moment-before-all-ants-fall-out-of-a-plank.cpp new file mode 100644 index 00000000..fb29609d --- /dev/null +++ b/1503-last-moment-before-all-ants-fall-out-of-a-plank/1503-last-moment-before-all-ants-fall-out-of-a-plank.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + int getLastMoment(int n, vector& left, vector& right) { + + int maxi = 0; + + for(auto& itr : left) + { + maxi = max(maxi, itr); + } + + for(auto& itr : right) + { + maxi = max(maxi, n - itr); + } + + return maxi; + + } +}; \ No newline at end of file From 9b4895b277ef3f7967e5e5b457852c7bd591d680 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 4 Nov 2023 19:56:31 +0530 Subject: [PATCH 0293/1399] Attach NOTES - LeetHub --- 1503-last-moment-before-all-ants-fall-out-of-a-plank/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1503-last-moment-before-all-ants-fall-out-of-a-plank/NOTES.md diff --git a/1503-last-moment-before-all-ants-fall-out-of-a-plank/NOTES.md b/1503-last-moment-before-all-ants-fall-out-of-a-plank/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1503-last-moment-before-all-ants-fall-out-of-a-plank/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 87721749d27d363f48e26fa322e906ecd90e4b33 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 4 Nov 2023 19:56:35 +0530 Subject: [PATCH 0294/1399] Time: 19 ms (48.28%), Space: 23.2 MB (31.47%) - LeetHub From 4f36e6dc36190c30b5ae99bc16a42a47c8750fd4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 5 Nov 2023 22:42:21 +0530 Subject: [PATCH 0295/1399] Create README - LeetHub --- .../README.md | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdirecti-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1535-find-the-winner-of-an-array-game/README.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdirecti-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1535-find-the-winner-of-an-array-game/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdirecti-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1535-find-the-winner-of-an-array-game/README.md new file mode 100644 index 00000000..669d5500 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdirecti-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1535-find-the-winner-of-an-array-game/README.md @@ -0,0 +1,39 @@ +

Directi
1
1535. Find the Winner of an Array Game

Medium


Given an integer array arr of distinct integers and an integer k.

+ +

A game will be played between the first two elements of the array (i.e. arr[0] and arr[1]). In each round of the game, we compare arr[0] with arr[1], the larger integer wins and remains at position 0, and the smaller integer moves to the end of the array. The game ends when an integer wins k consecutive rounds.

+ +

Return the integer which will win the game.

+ +

It is guaranteed that there will be a winner of the game.

+ +

 

+

Example 1:

+ +
Input: arr = [2,1,3,5,4,6,7], k = 2
+Output: 5
+Explanation: Let's see the rounds of the game:
+Round |       arr       | winner | win_count
+  1   | [2,1,3,5,4,6,7] | 2      | 1
+  2   | [2,3,5,4,6,7,1] | 3      | 1
+  3   | [3,5,4,6,7,1,2] | 5      | 1
+  4   | [5,4,6,7,1,2,3] | 5      | 2
+So we can see that 4 rounds will be played and 5 is the winner because it wins 2 consecutive games.
+
+ +

Example 2:

+ +
Input: arr = [3,2,1], k = 10
+Output: 3
+Explanation: 3 will win the first 10 rounds consecutively.
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= arr.length <= 105
  • +
  • 1 <= arr[i] <= 106
  • +
  • arr contains distinct integers.
  • +
  • 1 <= k <= 109
  • +
+
\ No newline at end of file From 3403f6222c1fd310b8d35bdaef9078f5adf35f68 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 5 Nov 2023 22:42:24 +0530 Subject: [PATCH 0296/1399] Time: 88 ms (56.48%), Space: 63.6 MB (29.63%) - LeetHub --- ...v1535-find-the-winner-of-an-array-game.cpp | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdirecti-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1535-find-the-winner-of-an-array-game/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdirecti-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1535-find-the-winner-of-an-array-game.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdirecti-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1535-find-the-winner-of-an-array-game/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdirecti-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1535-find-the-winner-of-an-array-game.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdirecti-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1535-find-the-winner-of-an-array-game/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdirecti-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1535-find-the-winner-of-an-array-game.cpp new file mode 100644 index 00000000..3385d741 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdirecti-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1535-find-the-winner-of-an-array-game/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdirecti-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1535-find-the-winner-of-an-array-game.cpp @@ -0,0 +1,34 @@ +class Solution { +public: + int getWinner(vector& arr, int k) { + + int n = arr.size(); + + int maxi = *max_element(arr.begin(), arr.end()); + + if(k >= arr.size()) return maxi; + + int cnt = 0; + + int winner = arr[0]; + + for(int i = 1; i < n; ++i) + { + if(winner > arr[i]) + { + ++cnt; + } + else + { + winner = arr[i]; + cnt = 1; + } + + if(cnt == k or winner == maxi) + return winner; + } + + return winner; + + } +}; \ No newline at end of file From 53f81cea8e6a773e2f4fd3065721b8febb12e8e3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 6 Nov 2023 08:03:20 +0530 Subject: [PATCH 0297/1399] Create README - LeetHub --- .../README.md | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdropbox-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1845-seat-reservation-manager/README.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdropbox-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1845-seat-reservation-manager/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdropbox-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1845-seat-reservation-manager/README.md new file mode 100644 index 00000000..3c53089a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdropbox-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1845-seat-reservation-manager/README.md @@ -0,0 +1,42 @@ +

Dropbox
1
1845. Seat Reservation Manager

Medium


Design a system that manages the reservation state of n seats that are numbered from 1 to n.

+ +

Implement the SeatManager class:

+ +
    +
  • SeatManager(int n) Initializes a SeatManager object that will manage n seats numbered from 1 to n. All seats are initially available.
  • +
  • int reserve() Fetches the smallest-numbered unreserved seat, reserves it, and returns its number.
  • +
  • void unreserve(int seatNumber) Unreserves the seat with the given seatNumber.
  • +
+ +

 

+

Example 1:

+ +
Input
+["SeatManager", "reserve", "reserve", "unreserve", "reserve", "reserve", "reserve", "reserve", "unreserve"]
+[[5], [], [], [2], [], [], [], [], [5]]
+Output
+[null, 1, 2, null, 2, 3, 4, 5, null]
+
+Explanation
+SeatManager seatManager = new SeatManager(5); // Initializes a SeatManager with 5 seats.
+seatManager.reserve();    // All seats are available, so return the lowest numbered seat, which is 1.
+seatManager.reserve();    // The available seats are [2,3,4,5], so return the lowest of them, which is 2.
+seatManager.unreserve(2); // Unreserve seat 2, so now the available seats are [2,3,4,5].
+seatManager.reserve();    // The available seats are [2,3,4,5], so return the lowest of them, which is 2.
+seatManager.reserve();    // The available seats are [3,4,5], so return the lowest of them, which is 3.
+seatManager.reserve();    // The available seats are [4,5], so return the lowest of them, which is 4.
+seatManager.reserve();    // The only available seat is seat 5, so return 5.
+seatManager.unreserve(5); // Unreserve seat 5, so now the available seats are [5].
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= 105
  • +
  • 1 <= seatNumber <= n
  • +
  • For each call to reserve, it is guaranteed that there will be at least one unreserved seat.
  • +
  • For each call to unreserve, it is guaranteed that seatNumber will be reserved.
  • +
  • At most 105 calls in total will be made to reserve and unreserve.
  • +
+
\ No newline at end of file From 9a3ec4546702c53863ff2bba046a4ba5a97b7b24 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 6 Nov 2023 08:03:23 +0530 Subject: [PATCH 0298/1399] Time: 301 ms (66.43%), Space: 151.1 MB (28.55%) - LeetHub --- ...v-div-div1845-seat-reservation-manager.cpp | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdropbox-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1845-seat-reservation-manager/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdropbox-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1845-seat-reservation-manager.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdropbox-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1845-seat-reservation-manager/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdropbox-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1845-seat-reservation-manager.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdropbox-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1845-seat-reservation-manager/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdropbox-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1845-seat-reservation-manager.cpp new file mode 100644 index 00000000..1931d631 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdropbox-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1845-seat-reservation-manager/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdropbox-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1845-seat-reservation-manager.cpp @@ -0,0 +1,46 @@ +class SeatManager { +public: + + set st; + vector seat; + int counter = 1; + int N; + + SeatManager(int n) { + N = n; + seat.resize(n+1, 0); + } + + int reserve() { + + if(!st.empty()) + { + int least = *st.begin(); + st.erase(*st.begin()); + seat[least] = 1; + return least; + } + + if(counter <= N) + { + seat[counter] = 1; + int smallestUnreserved = counter; + ++counter; + return smallestUnreserved; + } + + return -1; + } + + void unreserve(int seatNumber) { + st.insert(seatNumber); + seat[seatNumber] = 0; + } +}; + +/** + * Your SeatManager object will be instantiated and called as such: + * SeatManager* obj = new SeatManager(n); + * int param_1 = obj->reserve(); + * obj->unreserve(seatNumber); + */ \ No newline at end of file From 91eca447b459097a2aa1493e9eec4eaccf1e666b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 6 Nov 2023 08:04:38 +0530 Subject: [PATCH 0299/1399] Create README - LeetHub --- 1845-seat-reservation-manager/README.md | 42 +++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 1845-seat-reservation-manager/README.md diff --git a/1845-seat-reservation-manager/README.md b/1845-seat-reservation-manager/README.md new file mode 100644 index 00000000..889dd702 --- /dev/null +++ b/1845-seat-reservation-manager/README.md @@ -0,0 +1,42 @@ +

1845. Seat Reservation Manager

Medium


Design a system that manages the reservation state of n seats that are numbered from 1 to n.

+ +

Implement the SeatManager class:

+ +
    +
  • SeatManager(int n) Initializes a SeatManager object that will manage n seats numbered from 1 to n. All seats are initially available.
  • +
  • int reserve() Fetches the smallest-numbered unreserved seat, reserves it, and returns its number.
  • +
  • void unreserve(int seatNumber) Unreserves the seat with the given seatNumber.
  • +
+ +

 

+

Example 1:

+ +
Input
+["SeatManager", "reserve", "reserve", "unreserve", "reserve", "reserve", "reserve", "reserve", "unreserve"]
+[[5], [], [], [2], [], [], [], [], [5]]
+Output
+[null, 1, 2, null, 2, 3, 4, 5, null]
+
+Explanation
+SeatManager seatManager = new SeatManager(5); // Initializes a SeatManager with 5 seats.
+seatManager.reserve();    // All seats are available, so return the lowest numbered seat, which is 1.
+seatManager.reserve();    // The available seats are [2,3,4,5], so return the lowest of them, which is 2.
+seatManager.unreserve(2); // Unreserve seat 2, so now the available seats are [2,3,4,5].
+seatManager.reserve();    // The available seats are [2,3,4,5], so return the lowest of them, which is 2.
+seatManager.reserve();    // The available seats are [3,4,5], so return the lowest of them, which is 3.
+seatManager.reserve();    // The available seats are [4,5], so return the lowest of them, which is 4.
+seatManager.reserve();    // The only available seat is seat 5, so return 5.
+seatManager.unreserve(5); // Unreserve seat 5, so now the available seats are [5].
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= 105
  • +
  • 1 <= seatNumber <= n
  • +
  • For each call to reserve, it is guaranteed that there will be at least one unreserved seat.
  • +
  • For each call to unreserve, it is guaranteed that seatNumber will be reserved.
  • +
  • At most 105 calls in total will be made to reserve and unreserve.
  • +
+
\ No newline at end of file From 2f09d7bd6b4017a9a7b30b8bf45518ca0f74eb83 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 6 Nov 2023 08:04:39 +0530 Subject: [PATCH 0300/1399] Attach NOTES - LeetHub --- 1845-seat-reservation-manager/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1845-seat-reservation-manager/NOTES.md diff --git a/1845-seat-reservation-manager/NOTES.md b/1845-seat-reservation-manager/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1845-seat-reservation-manager/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 4b32a569387eeb1c51c12c0ab392abb3b96577eb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 6 Nov 2023 08:04:42 +0530 Subject: [PATCH 0301/1399] Time: 301 ms (66.43%), Space: 151.1 MB (28.55%) - LeetHub --- .../1845-seat-reservation-manager.cpp | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 1845-seat-reservation-manager/1845-seat-reservation-manager.cpp diff --git a/1845-seat-reservation-manager/1845-seat-reservation-manager.cpp b/1845-seat-reservation-manager/1845-seat-reservation-manager.cpp new file mode 100644 index 00000000..1931d631 --- /dev/null +++ b/1845-seat-reservation-manager/1845-seat-reservation-manager.cpp @@ -0,0 +1,46 @@ +class SeatManager { +public: + + set st; + vector seat; + int counter = 1; + int N; + + SeatManager(int n) { + N = n; + seat.resize(n+1, 0); + } + + int reserve() { + + if(!st.empty()) + { + int least = *st.begin(); + st.erase(*st.begin()); + seat[least] = 1; + return least; + } + + if(counter <= N) + { + seat[counter] = 1; + int smallestUnreserved = counter; + ++counter; + return smallestUnreserved; + } + + return -1; + } + + void unreserve(int seatNumber) { + st.insert(seatNumber); + seat[seatNumber] = 0; + } +}; + +/** + * Your SeatManager object will be instantiated and called as such: + * SeatManager* obj = new SeatManager(n); + * int param_1 = obj->reserve(); + * obj->unreserve(seatNumber); + */ \ No newline at end of file From 3b09b9f5e34d65547498019f6a101ff621b6152a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 6 Nov 2023 08:11:53 +0530 Subject: [PATCH 0302/1399] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdropbox-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1845-seat-reservation-manager/NOTES.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdropbox-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1845-seat-reservation-manager/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdropbox-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1845-seat-reservation-manager/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdropbox-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1845-seat-reservation-manager/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 322747de3d2bcf0aaa2af30201065e8eccb8afcf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 6 Nov 2023 08:11:56 +0530 Subject: [PATCH 0303/1399] Time: 282 ms (89.24%), Space: 148.6 MB (30.56%) - LeetHub --- ...ct-svg-div-div-div-div1845-seat-reservation-manager.cpp | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdropbox-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1845-seat-reservation-manager/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdropbox-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1845-seat-reservation-manager.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdropbox-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1845-seat-reservation-manager/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdropbox-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1845-seat-reservation-manager.cpp index 1931d631..323ea613 100644 --- a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdropbox-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1845-seat-reservation-manager/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdropbox-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1845-seat-reservation-manager.cpp +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdropbox-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1845-seat-reservation-manager/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divdropbox-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1845-seat-reservation-manager.cpp @@ -2,13 +2,11 @@ class SeatManager { public: set st; - vector seat; int counter = 1; int N; SeatManager(int n) { - N = n; - seat.resize(n+1, 0); + N = n; } int reserve() { @@ -17,13 +15,11 @@ class SeatManager { { int least = *st.begin(); st.erase(*st.begin()); - seat[least] = 1; return least; } if(counter <= N) { - seat[counter] = 1; int smallestUnreserved = counter; ++counter; return smallestUnreserved; @@ -34,7 +30,6 @@ class SeatManager { void unreserve(int seatNumber) { st.insert(seatNumber); - seat[seatNumber] = 0; } }; From 5076555bb7425ade6f1e8ef3bfbf602a5b7921af Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 7 Nov 2023 19:22:13 +0530 Subject: [PATCH 0304/1399] Create README - LeetHub --- .../README.md | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1921-eliminate-maximum-number-of-monsters/README.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1921-eliminate-maximum-number-of-monsters/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1921-eliminate-maximum-number-of-monsters/README.md new file mode 100644 index 00000000..684cc6cc --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1921-eliminate-maximum-number-of-monsters/README.md @@ -0,0 +1,50 @@ +

No companies found for this problem
1921. Eliminate Maximum Number of Monsters

Medium


You are playing a video game where you are defending your city from a group of n monsters. You are given a 0-indexed integer array dist of size n, where dist[i] is the initial distance in kilometers of the ith monster from the city.

+ +

The monsters walk toward the city at a constant speed. The speed of each monster is given to you in an integer array speed of size n, where speed[i] is the speed of the ith monster in kilometers per minute.

+ +

You have a weapon that, once fully charged, can eliminate a single monster. However, the weapon takes one minute to charge. The weapon is fully charged at the very start.

+ +

You lose when any monster reaches your city. If a monster reaches the city at the exact moment the weapon is fully charged, it counts as a loss, and the game ends before you can use your weapon.

+ +

Return the maximum number of monsters that you can eliminate before you lose, or n if you can eliminate all the monsters before they reach the city.

+ +

 

+

Example 1:

+ +
Input: dist = [1,3,4], speed = [1,1,1]
+Output: 3
+Explanation:
+In the beginning, the distances of the monsters are [1,3,4]. You eliminate the first monster.
+After a minute, the distances of the monsters are [X,2,3]. You eliminate the second monster.
+After a minute, the distances of the monsters are [X,X,2]. You eliminate the thrid monster.
+All 3 monsters can be eliminated.
+ +

Example 2:

+ +
Input: dist = [1,1,2,3], speed = [1,1,1,1]
+Output: 1
+Explanation:
+In the beginning, the distances of the monsters are [1,1,2,3]. You eliminate the first monster.
+After a minute, the distances of the monsters are [X,0,1,2], so you lose.
+You can only eliminate 1 monster.
+
+ +

Example 3:

+ +
Input: dist = [3,2,4], speed = [5,3,2]
+Output: 1
+Explanation:
+In the beginning, the distances of the monsters are [3,2,4]. You eliminate the first monster.
+After a minute, the distances of the monsters are [X,0,2], so you lose.
+You can only eliminate 1 monster.
+
+ +

 

+

Constraints:

+ +
    +
  • n == dist.length == speed.length
  • +
  • 1 <= n <= 105
  • +
  • 1 <= dist[i], speed[i] <= 105
  • +
+
\ No newline at end of file From 5051da412f03b983e73a957b164776ace56e7004 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 7 Nov 2023 19:22:17 +0530 Subject: [PATCH 0305/1399] Time: 108 ms (65.60%), Space: 85.3 MB (49.33%) - LeetHub --- ...1-eliminate-maximum-number-of-monsters.cpp | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1921-eliminate-maximum-number-of-monsters/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1921-eliminate-maximum-number-of-monsters.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1921-eliminate-maximum-number-of-monsters/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1921-eliminate-maximum-number-of-monsters.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1921-eliminate-maximum-number-of-monsters/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1921-eliminate-maximum-number-of-monsters.cpp new file mode 100644 index 00000000..e7a702a0 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1921-eliminate-maximum-number-of-monsters/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1921-eliminate-maximum-number-of-monsters.cpp @@ -0,0 +1,29 @@ +class Solution { +public: + int eliminateMaximum(vector& dist, vector& speed) { + + int n = dist.size(); + + vector vec; + + for(int i = 0; i < n; ++i) + { + vec.push_back((float)dist[i]/speed[i]); + } + + sort(vec.begin(), vec.end()); + + int counter = 1, ans = 1; + + for(int i = 1; i < n; ++i) + { + if(counter < vec[i]) + ++ans; + else + break; + ++counter; + } + + return ans; + } +}; \ No newline at end of file From 8e2396d708d1079fa6829d2c0a850a4166194f5e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 7 Nov 2023 19:30:36 +0530 Subject: [PATCH 0306/1399] Create README - LeetHub --- .../README.md | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 1921-eliminate-maximum-number-of-monsters/README.md diff --git a/1921-eliminate-maximum-number-of-monsters/README.md b/1921-eliminate-maximum-number-of-monsters/README.md new file mode 100644 index 00000000..53ce3337 --- /dev/null +++ b/1921-eliminate-maximum-number-of-monsters/README.md @@ -0,0 +1,50 @@ +

1921. Eliminate Maximum Number of Monsters

Medium


You are playing a video game where you are defending your city from a group of n monsters. You are given a 0-indexed integer array dist of size n, where dist[i] is the initial distance in kilometers of the ith monster from the city.

+ +

The monsters walk toward the city at a constant speed. The speed of each monster is given to you in an integer array speed of size n, where speed[i] is the speed of the ith monster in kilometers per minute.

+ +

You have a weapon that, once fully charged, can eliminate a single monster. However, the weapon takes one minute to charge. The weapon is fully charged at the very start.

+ +

You lose when any monster reaches your city. If a monster reaches the city at the exact moment the weapon is fully charged, it counts as a loss, and the game ends before you can use your weapon.

+ +

Return the maximum number of monsters that you can eliminate before you lose, or n if you can eliminate all the monsters before they reach the city.

+ +

 

+

Example 1:

+ +
Input: dist = [1,3,4], speed = [1,1,1]
+Output: 3
+Explanation:
+In the beginning, the distances of the monsters are [1,3,4]. You eliminate the first monster.
+After a minute, the distances of the monsters are [X,2,3]. You eliminate the second monster.
+After a minute, the distances of the monsters are [X,X,2]. You eliminate the thrid monster.
+All 3 monsters can be eliminated.
+ +

Example 2:

+ +
Input: dist = [1,1,2,3], speed = [1,1,1,1]
+Output: 1
+Explanation:
+In the beginning, the distances of the monsters are [1,1,2,3]. You eliminate the first monster.
+After a minute, the distances of the monsters are [X,0,1,2], so you lose.
+You can only eliminate 1 monster.
+
+ +

Example 3:

+ +
Input: dist = [3,2,4], speed = [5,3,2]
+Output: 1
+Explanation:
+In the beginning, the distances of the monsters are [3,2,4]. You eliminate the first monster.
+After a minute, the distances of the monsters are [X,0,2], so you lose.
+You can only eliminate 1 monster.
+
+ +

 

+

Constraints:

+ +
    +
  • n == dist.length == speed.length
  • +
  • 1 <= n <= 105
  • +
  • 1 <= dist[i], speed[i] <= 105
  • +
+
\ No newline at end of file From 9c7804266b3faecab6992e321e7f8ec6b5b86c95 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 7 Nov 2023 19:30:40 +0530 Subject: [PATCH 0307/1399] Time: 108 ms (65.60%), Space: 85.3 MB (49.33%) - LeetHub --- ...1-eliminate-maximum-number-of-monsters.cpp | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 1921-eliminate-maximum-number-of-monsters/1921-eliminate-maximum-number-of-monsters.cpp diff --git a/1921-eliminate-maximum-number-of-monsters/1921-eliminate-maximum-number-of-monsters.cpp b/1921-eliminate-maximum-number-of-monsters/1921-eliminate-maximum-number-of-monsters.cpp new file mode 100644 index 00000000..e7a702a0 --- /dev/null +++ b/1921-eliminate-maximum-number-of-monsters/1921-eliminate-maximum-number-of-monsters.cpp @@ -0,0 +1,29 @@ +class Solution { +public: + int eliminateMaximum(vector& dist, vector& speed) { + + int n = dist.size(); + + vector vec; + + for(int i = 0; i < n; ++i) + { + vec.push_back((float)dist[i]/speed[i]); + } + + sort(vec.begin(), vec.end()); + + int counter = 1, ans = 1; + + for(int i = 1; i < n; ++i) + { + if(counter < vec[i]) + ++ans; + else + break; + ++counter; + } + + return ans; + } +}; \ No newline at end of file From f661d19620f0df29779f1c4768fe078b6d989efd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 8 Nov 2023 08:51:38 +0530 Subject: [PATCH 0308/1399] Create README - LeetHub --- .../README.md | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2849-determine-if-a-cell-is-reachable-at-a-given-time/README.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2849-determine-if-a-cell-is-reachable-at-a-given-time/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2849-determine-if-a-cell-is-reachable-at-a-given-time/README.md new file mode 100644 index 00000000..13a5881b --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2849-determine-if-a-cell-is-reachable-at-a-given-time/README.md @@ -0,0 +1,31 @@ +

No companies found for this problem
2849. Determine if a Cell Is Reachable at a Given Time

Medium


You are given four integers sx, sy, fx, fy, and a non-negative integer t.

+ +

In an infinite 2D grid, you start at the cell (sx, sy). Each second, you must move to any of its adjacent cells.

+ +

Return true if you can reach cell (fx, fy) after exactly t seconds, or false otherwise.

+ +

A cell's adjacent cells are the 8 cells around it that share at least one corner with it. You can visit the same cell several times.

+ +

 

+

Example 1:

+ +
Input: sx = 2, sy = 4, fx = 7, fy = 7, t = 6
+Output: true
+Explanation: Starting at cell (2, 4), we can reach cell (7, 7) in exactly 6 seconds by going through the cells depicted in the picture above. 
+
+ +

Example 2:

+ +
Input: sx = 3, sy = 1, fx = 7, fy = 3, t = 3
+Output: false
+Explanation: Starting at cell (3, 1), it takes at least 4 seconds to reach cell (7, 3) by going through the cells depicted in the picture above. Hence, we cannot reach cell (7, 3) at the third second.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= sx, sy, fx, fy <= 109
  • +
  • 0 <= t <= 109
  • +
+
\ No newline at end of file From 724c019949c5b3c38da2600206431c0cfedc10fa Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 8 Nov 2023 08:51:38 +0530 Subject: [PATCH 0309/1399] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2849-determine-if-a-cell-is-reachable-at-a-given-time/NOTES.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2849-determine-if-a-cell-is-reachable-at-a-given-time/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2849-determine-if-a-cell-is-reachable-at-a-given-time/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2849-determine-if-a-cell-is-reachable-at-a-given-time/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 95667ddc97c170495adcc86a26827e4ed136f8bf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 8 Nov 2023 08:51:41 +0530 Subject: [PATCH 0310/1399] Time: 0 ms (100.00%), Space: 6.2 MB (10.74%) - LeetHub --- ...e-if-a-cell-is-reachable-at-a-given-time.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2849-determine-if-a-cell-is-reachable-at-a-given-time/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2849-determine-if-a-cell-is-reachable-at-a-given-time.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2849-determine-if-a-cell-is-reachable-at-a-given-time/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2849-determine-if-a-cell-is-reachable-at-a-given-time.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2849-determine-if-a-cell-is-reachable-at-a-given-time/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2849-determine-if-a-cell-is-reachable-at-a-given-time.cpp new file mode 100644 index 00000000..e87e582f --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2849-determine-if-a-cell-is-reachable-at-a-given-time/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2849-determine-if-a-cell-is-reachable-at-a-given-time.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + bool isReachableAtTime(int sx, int sy, int fx, int fy, int t) { + + int xDiff = abs(sx - fx); + int yDiff = abs(sy - fy); + + int diagonalDist = min(xDiff, yDiff); + + if(sx == fx and sy == fy and t == 1) + return false; + + int minDist = diagonalDist + abs(xDiff - yDiff); + + return minDist <= t; + } +}; \ No newline at end of file From 47e9df963b165538816da564d82544c41ce0e6a6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 8 Nov 2023 08:53:00 +0530 Subject: [PATCH 0311/1399] Create README - LeetHub --- .../README.md | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 2849-determine-if-a-cell-is-reachable-at-a-given-time/README.md diff --git a/2849-determine-if-a-cell-is-reachable-at-a-given-time/README.md b/2849-determine-if-a-cell-is-reachable-at-a-given-time/README.md new file mode 100644 index 00000000..b806424d --- /dev/null +++ b/2849-determine-if-a-cell-is-reachable-at-a-given-time/README.md @@ -0,0 +1,31 @@ +

2849. Determine if a Cell Is Reachable at a Given Time

Medium


You are given four integers sx, sy, fx, fy, and a non-negative integer t.

+ +

In an infinite 2D grid, you start at the cell (sx, sy). Each second, you must move to any of its adjacent cells.

+ +

Return true if you can reach cell (fx, fy) after exactly t seconds, or false otherwise.

+ +

A cell's adjacent cells are the 8 cells around it that share at least one corner with it. You can visit the same cell several times.

+ +

 

+

Example 1:

+ +
Input: sx = 2, sy = 4, fx = 7, fy = 7, t = 6
+Output: true
+Explanation: Starting at cell (2, 4), we can reach cell (7, 7) in exactly 6 seconds by going through the cells depicted in the picture above. 
+
+ +

Example 2:

+ +
Input: sx = 3, sy = 1, fx = 7, fy = 3, t = 3
+Output: false
+Explanation: Starting at cell (3, 1), it takes at least 4 seconds to reach cell (7, 3) by going through the cells depicted in the picture above. Hence, we cannot reach cell (7, 3) at the third second.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= sx, sy, fx, fy <= 109
  • +
  • 0 <= t <= 109
  • +
+
\ No newline at end of file From 524e8be405eedfcc91939413d0947a36f56fde81 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 8 Nov 2023 08:53:01 +0530 Subject: [PATCH 0312/1399] Attach NOTES - LeetHub --- 2849-determine-if-a-cell-is-reachable-at-a-given-time/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2849-determine-if-a-cell-is-reachable-at-a-given-time/NOTES.md diff --git a/2849-determine-if-a-cell-is-reachable-at-a-given-time/NOTES.md b/2849-determine-if-a-cell-is-reachable-at-a-given-time/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2849-determine-if-a-cell-is-reachable-at-a-given-time/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 2f3d6a490bf5cf50b67cd716dce088ae66e5c047 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 8 Nov 2023 08:53:04 +0530 Subject: [PATCH 0313/1399] Time: 0 ms (100.00%), Space: 6.2 MB (10.74%) - LeetHub --- ...e-if-a-cell-is-reachable-at-a-given-time.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 2849-determine-if-a-cell-is-reachable-at-a-given-time/2849-determine-if-a-cell-is-reachable-at-a-given-time.cpp diff --git a/2849-determine-if-a-cell-is-reachable-at-a-given-time/2849-determine-if-a-cell-is-reachable-at-a-given-time.cpp b/2849-determine-if-a-cell-is-reachable-at-a-given-time/2849-determine-if-a-cell-is-reachable-at-a-given-time.cpp new file mode 100644 index 00000000..e87e582f --- /dev/null +++ b/2849-determine-if-a-cell-is-reachable-at-a-given-time/2849-determine-if-a-cell-is-reachable-at-a-given-time.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + bool isReachableAtTime(int sx, int sy, int fx, int fy, int t) { + + int xDiff = abs(sx - fx); + int yDiff = abs(sy - fy); + + int diagonalDist = min(xDiff, yDiff); + + if(sx == fx and sy == fy and t == 1) + return false; + + int minDist = diagonalDist + abs(xDiff - yDiff); + + return minDist <= t; + } +}; \ No newline at end of file From d4ad503ce816ff80edebbd33029abe4cd0a9de99 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 9 Nov 2023 08:25:42 +0530 Subject: [PATCH 0314/1399] Create README - LeetHub --- .../README.md | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvirtu-financial-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1759-count-number-of-homogenous-substrings/README.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvirtu-financial-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1759-count-number-of-homogenous-substrings/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvirtu-financial-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1759-count-number-of-homogenous-substrings/README.md new file mode 100644 index 00000000..f0e7b65d --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvirtu-financial-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1759-count-number-of-homogenous-substrings/README.md @@ -0,0 +1,40 @@ +

Virtu Financial
1
1759. Count Number of Homogenous Substrings

Medium


Given a string s, return the number of homogenous substrings of s. Since the answer may be too large, return it modulo 109 + 7.

+ +

A string is homogenous if all the characters of the string are the same.

+ +

A substring is a contiguous sequence of characters within a string.

+ +

 

+

Example 1:

+ +
Input: s = "abbcccaa"
+Output: 13
+Explanation: The homogenous substrings are listed as below:
+"a"   appears 3 times.
+"aa"  appears 1 time.
+"b"   appears 2 times.
+"bb"  appears 1 time.
+"c"   appears 3 times.
+"cc"  appears 2 times.
+"ccc" appears 1 time.
+3 + 1 + 2 + 1 + 3 + 2 + 1 = 13.
+ +

Example 2:

+ +
Input: s = "xy"
+Output: 2
+Explanation: The homogenous substrings are "x" and "y".
+ +

Example 3:

+ +
Input: s = "zzzzz"
+Output: 15
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 105
  • +
  • s consists of lowercase letters.
  • +
\ No newline at end of file From 2dec9bba804ae555d6dfe6c5b8ca406e290b67a0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 9 Nov 2023 08:25:45 +0530 Subject: [PATCH 0315/1399] Time: 25 ms (63.26%), Space: 12 MB (85.65%) - LeetHub --- ...-count-number-of-homogenous-substrings.cpp | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvirtu-financial-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1759-count-number-of-homogenous-substrings/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvirtu-financial-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1759-count-number-of-homogenous-substrings.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvirtu-financial-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1759-count-number-of-homogenous-substrings/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvirtu-financial-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1759-count-number-of-homogenous-substrings.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvirtu-financial-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1759-count-number-of-homogenous-substrings/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvirtu-financial-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1759-count-number-of-homogenous-substrings.cpp new file mode 100644 index 00000000..3ac68cd1 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvirtu-financial-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1759-count-number-of-homogenous-substrings/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvirtu-financial-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1759-count-number-of-homogenous-substrings.cpp @@ -0,0 +1,42 @@ +class Solution { +public: + int countHomogenous(string s) { + + int n = s.size(); + + const int mod = 1e9 + 7; + + long long cnt = 1; + + long long ans = 0; + + for(int i = 1; i < n; ++i) + { + if(s[i] == s[i-1]) + { + ++cnt; + } + + else + { + int curr = ((cnt * 1LL* (cnt+1))/2) % mod; + + ans = (ans + curr) % mod; + + cnt = 1; + } + } + + if(cnt) + { + long long curr = ((cnt * 1LL *(cnt+1))/2)%mod ; + + ans = (ans + curr) % mod; + + cnt = 1; + } + + return ans; + + } +}; \ No newline at end of file From cba40117b58a7cf01c4ca92bdfe66be342738d7a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 9 Nov 2023 08:27:11 +0530 Subject: [PATCH 0316/1399] Create README - LeetHub --- .../README.md | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 1759-count-number-of-homogenous-substrings/README.md diff --git a/1759-count-number-of-homogenous-substrings/README.md b/1759-count-number-of-homogenous-substrings/README.md new file mode 100644 index 00000000..2e82ed3b --- /dev/null +++ b/1759-count-number-of-homogenous-substrings/README.md @@ -0,0 +1,40 @@ +

1759. Count Number of Homogenous Substrings

Medium


Given a string s, return the number of homogenous substrings of s. Since the answer may be too large, return it modulo 109 + 7.

+ +

A string is homogenous if all the characters of the string are the same.

+ +

A substring is a contiguous sequence of characters within a string.

+ +

 

+

Example 1:

+ +
Input: s = "abbcccaa"
+Output: 13
+Explanation: The homogenous substrings are listed as below:
+"a"   appears 3 times.
+"aa"  appears 1 time.
+"b"   appears 2 times.
+"bb"  appears 1 time.
+"c"   appears 3 times.
+"cc"  appears 2 times.
+"ccc" appears 1 time.
+3 + 1 + 2 + 1 + 3 + 2 + 1 = 13.
+ +

Example 2:

+ +
Input: s = "xy"
+Output: 2
+Explanation: The homogenous substrings are "x" and "y".
+ +

Example 3:

+ +
Input: s = "zzzzz"
+Output: 15
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 105
  • +
  • s consists of lowercase letters.
  • +
\ No newline at end of file From afe48ff9cf94f927630602d6becbde3e9915138e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 9 Nov 2023 08:27:15 +0530 Subject: [PATCH 0317/1399] Time: 25 ms (63.26%), Space: 12 MB (85.65%) - LeetHub --- ...-count-number-of-homogenous-substrings.cpp | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 1759-count-number-of-homogenous-substrings/1759-count-number-of-homogenous-substrings.cpp diff --git a/1759-count-number-of-homogenous-substrings/1759-count-number-of-homogenous-substrings.cpp b/1759-count-number-of-homogenous-substrings/1759-count-number-of-homogenous-substrings.cpp new file mode 100644 index 00000000..3ac68cd1 --- /dev/null +++ b/1759-count-number-of-homogenous-substrings/1759-count-number-of-homogenous-substrings.cpp @@ -0,0 +1,42 @@ +class Solution { +public: + int countHomogenous(string s) { + + int n = s.size(); + + const int mod = 1e9 + 7; + + long long cnt = 1; + + long long ans = 0; + + for(int i = 1; i < n; ++i) + { + if(s[i] == s[i-1]) + { + ++cnt; + } + + else + { + int curr = ((cnt * 1LL* (cnt+1))/2) % mod; + + ans = (ans + curr) % mod; + + cnt = 1; + } + } + + if(cnt) + { + long long curr = ((cnt * 1LL *(cnt+1))/2)%mod ; + + ans = (ans + curr) % mod; + + cnt = 1; + } + + return ans; + + } +}; \ No newline at end of file From 2a09fa5b0be998b6c1703b8f8d46f6a10a853eb3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 9 Nov 2023 08:27:41 +0530 Subject: [PATCH 0318/1399] Attach NOTES - LeetHub --- 1759-count-number-of-homogenous-substrings/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1759-count-number-of-homogenous-substrings/NOTES.md diff --git a/1759-count-number-of-homogenous-substrings/NOTES.md b/1759-count-number-of-homogenous-substrings/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1759-count-number-of-homogenous-substrings/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 84a0b79c87cae84d72dbf693c6f64d87f15ba839 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 9 Nov 2023 08:28:03 +0530 Subject: [PATCH 0319/1399] Attach NOTES - LeetHub From 5eb3673c653ed7a910bb33b763dbaf83a7d0af57 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 9 Nov 2023 08:28:06 +0530 Subject: [PATCH 0320/1399] Time: 25 ms (63.26%), Space: 12 MB (85.65%) - LeetHub From b8ce966867e59e31f357f8f866583935ca41e6e2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 9 Nov 2023 08:28:34 +0530 Subject: [PATCH 0321/1399] Attach NOTES - LeetHub From 0ffb2adb6721b32c261655453e542cb158468963 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 9 Nov 2023 08:28:37 +0530 Subject: [PATCH 0322/1399] Time: 25 ms (63.26%), Space: 12 MB (85.65%) - LeetHub From 039c7605598b1d5c432ec058bb0e55d2947cef9f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 9 Nov 2023 08:32:11 +0530 Subject: [PATCH 0323/1399] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvirtu-financial-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1759-count-number-of-homogenous-substrings/NOTES.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvirtu-financial-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1759-count-number-of-homogenous-substrings/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvirtu-financial-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1759-count-number-of-homogenous-substrings/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvirtu-financial-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1759-count-number-of-homogenous-substrings/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 42a2899eb86729482ccfdd45b80ccb6c7343b2ac Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 9 Nov 2023 08:32:15 +0530 Subject: [PATCH 0324/1399] Time: 24 ms (71.96%), Space: 12.2 MB (22.39%) - LeetHub --- ...-count-number-of-homogenous-substrings.cpp | 35 ++++--------------- 1 file changed, 7 insertions(+), 28 deletions(-) diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvirtu-financial-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1759-count-number-of-homogenous-substrings/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvirtu-financial-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1759-count-number-of-homogenous-substrings.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvirtu-financial-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1759-count-number-of-homogenous-substrings/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvirtu-financial-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1759-count-number-of-homogenous-substrings.cpp index 3ac68cd1..02b8bc5c 100644 --- a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvirtu-financial-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1759-count-number-of-homogenous-substrings/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvirtu-financial-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1759-count-number-of-homogenous-substrings.cpp +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvirtu-financial-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1759-count-number-of-homogenous-substrings/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divvirtu-financial-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1759-count-number-of-homogenous-substrings.cpp @@ -2,41 +2,20 @@ class Solution { public: int countHomogenous(string s) { - int n = s.size(); + int n = s.size(), cnt = 0, prev = 0, res = 0; const int mod = 1e9 + 7; - long long cnt = 1; - - long long ans = 0; - - for(int i = 1; i < n; ++i) + for(auto& itr : s) { - if(s[i] == s[i-1]) - { - ++cnt; - } + cnt = (itr == prev ? cnt+1 : 1); - else - { - int curr = ((cnt * 1LL* (cnt+1))/2) % mod; - - ans = (ans + curr) % mod; - - cnt = 1; - } - } - - if(cnt) - { - long long curr = ((cnt * 1LL *(cnt+1))/2)%mod ; - - ans = (ans + curr) % mod; - - cnt = 1; + prev = itr; + + res = (res + cnt) % mod; } - return ans; + return res; } }; \ No newline at end of file From a7d00a8e2b60f030ea89b6b0d6cdc70911dd2935 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 9 Nov 2023 08:32:42 +0530 Subject: [PATCH 0325/1399] Attach NOTES - LeetHub From c8006cf4c0515b350546eecff01022780407369a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 9 Nov 2023 08:32:45 +0530 Subject: [PATCH 0326/1399] Time: 24 ms (71.96%), Space: 12.2 MB (22.39%) - LeetHub --- ...-count-number-of-homogenous-substrings.cpp | 35 ++++--------------- 1 file changed, 7 insertions(+), 28 deletions(-) diff --git a/1759-count-number-of-homogenous-substrings/1759-count-number-of-homogenous-substrings.cpp b/1759-count-number-of-homogenous-substrings/1759-count-number-of-homogenous-substrings.cpp index 3ac68cd1..02b8bc5c 100644 --- a/1759-count-number-of-homogenous-substrings/1759-count-number-of-homogenous-substrings.cpp +++ b/1759-count-number-of-homogenous-substrings/1759-count-number-of-homogenous-substrings.cpp @@ -2,41 +2,20 @@ class Solution { public: int countHomogenous(string s) { - int n = s.size(); + int n = s.size(), cnt = 0, prev = 0, res = 0; const int mod = 1e9 + 7; - long long cnt = 1; - - long long ans = 0; - - for(int i = 1; i < n; ++i) + for(auto& itr : s) { - if(s[i] == s[i-1]) - { - ++cnt; - } + cnt = (itr == prev ? cnt+1 : 1); - else - { - int curr = ((cnt * 1LL* (cnt+1))/2) % mod; - - ans = (ans + curr) % mod; - - cnt = 1; - } - } - - if(cnt) - { - long long curr = ((cnt * 1LL *(cnt+1))/2)%mod ; - - ans = (ans + curr) % mod; - - cnt = 1; + prev = itr; + + res = (res + cnt) % mod; } - return ans; + return res; } }; \ No newline at end of file From 102626fe64586c95cccf742289b872b41505983d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 10 Nov 2023 08:30:08 +0530 Subject: [PATCH 0327/1399] Create README - LeetHub --- .../README.md | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divcapital-one-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1743-restore-the-array-from-adjacent-pairs/README.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divcapital-one-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1743-restore-the-array-from-adjacent-pairs/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divcapital-one-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1743-restore-the-array-from-adjacent-pairs/README.md new file mode 100644 index 00000000..dd43c1b0 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divcapital-one-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1743-restore-the-array-from-adjacent-pairs/README.md @@ -0,0 +1,43 @@ +

Capital One
2
1743. Restore the Array From Adjacent Pairs

Medium


There is an integer array nums that consists of n unique elements, but you have forgotten it. However, you do remember every pair of adjacent elements in nums.

+ +

You are given a 2D integer array adjacentPairs of size n - 1 where each adjacentPairs[i] = [ui, vi] indicates that the elements ui and vi are adjacent in nums.

+ +

It is guaranteed that every adjacent pair of elements nums[i] and nums[i+1] will exist in adjacentPairs, either as [nums[i], nums[i+1]] or [nums[i+1], nums[i]]. The pairs can appear in any order.

+ +

Return the original array nums. If there are multiple solutions, return any of them.

+ +

 

+

Example 1:

+ +
Input: adjacentPairs = [[2,1],[3,4],[3,2]]
+Output: [1,2,3,4]
+Explanation: This array has all its adjacent pairs in adjacentPairs.
+Notice that adjacentPairs[i] may not be in left-to-right order.
+
+ +

Example 2:

+ +
Input: adjacentPairs = [[4,-2],[1,4],[-3,1]]
+Output: [-2,4,1,-3]
+Explanation: There can be negative numbers.
+Another solution is [-3,1,4,-2], which would also be accepted.
+
+ +

Example 3:

+ +
Input: adjacentPairs = [[100000,-100000]]
+Output: [100000,-100000]
+
+ +

 

+

Constraints:

+ +
    +
  • nums.length == n
  • +
  • adjacentPairs.length == n - 1
  • +
  • adjacentPairs[i].length == 2
  • +
  • 2 <= n <= 105
  • +
  • -105 <= nums[i], ui, vi <= 105
  • +
  • There exists some nums that has adjacentPairs as its pairs.
  • +
+
\ No newline at end of file From a468e9a9541463523a9ae1ce42b6e197155c001e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 10 Nov 2023 08:30:12 +0530 Subject: [PATCH 0328/1399] Time: 478 ms (39.09%), Space: 192.1 MB (5.15%) - LeetHub --- ...-restore-the-array-from-adjacent-pairs.cpp | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divcapital-one-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1743-restore-the-array-from-adjacent-pairs/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divcapital-one-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1743-restore-the-array-from-adjacent-pairs.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divcapital-one-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1743-restore-the-array-from-adjacent-pairs/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divcapital-one-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1743-restore-the-array-from-adjacent-pairs.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divcapital-one-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1743-restore-the-array-from-adjacent-pairs/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divcapital-one-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1743-restore-the-array-from-adjacent-pairs.cpp new file mode 100644 index 00000000..b0bb50ab --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divcapital-one-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1743-restore-the-array-from-adjacent-pairs/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divcapital-one-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1743-restore-the-array-from-adjacent-pairs.cpp @@ -0,0 +1,48 @@ +class Solution { +public: + vector restoreArray(vector>& adjacentPairs) { + + unordered_map> adj; + unordered_map mp; + + int sv = -1; + + for(auto& itr : adjacentPairs) + { + adj[itr[0]].push_back(itr[1]); + adj[itr[1]].push_back(itr[0]); + + ++mp[itr[0]]; + ++mp[itr[1]]; + } + + for(auto& [f, s] : mp) + { + if(s == 1) + sv = f; + } + + vector ans; + + unordered_set visited; + + function dfs = [&](int sv) + { + ans.push_back(sv); + visited.insert(sv); + + for(auto& itr : adj[sv]) + { + if(!visited.count(itr)) + dfs(itr); + } + }; + + cout< Date: Fri, 10 Nov 2023 08:32:38 +0530 Subject: [PATCH 0329/1399] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divcapital-one-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1743-restore-the-array-from-adjacent-pairs/NOTES.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divcapital-one-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1743-restore-the-array-from-adjacent-pairs/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divcapital-one-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1743-restore-the-array-from-adjacent-pairs/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divcapital-one-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1743-restore-the-array-from-adjacent-pairs/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From ccbbeb6295949d781688a9dfa5df322c3f5393f2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 10 Nov 2023 08:34:44 +0530 Subject: [PATCH 0330/1399] Attach NOTES - LeetHub --- 1743-restore-the-array-from-adjacent-pairs/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1743-restore-the-array-from-adjacent-pairs/NOTES.md diff --git a/1743-restore-the-array-from-adjacent-pairs/NOTES.md b/1743-restore-the-array-from-adjacent-pairs/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1743-restore-the-array-from-adjacent-pairs/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 76fe8898fc5b76e20e9c09be9cd3319332a6339a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 10 Nov 2023 08:35:42 +0530 Subject: [PATCH 0331/1399] Create README - LeetHub --- .../README.md | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 1743-restore-the-array-from-adjacent-pairs/README.md diff --git a/1743-restore-the-array-from-adjacent-pairs/README.md b/1743-restore-the-array-from-adjacent-pairs/README.md new file mode 100644 index 00000000..0c83a514 --- /dev/null +++ b/1743-restore-the-array-from-adjacent-pairs/README.md @@ -0,0 +1,43 @@ +

1743. Restore the Array From Adjacent Pairs

Medium


There is an integer array nums that consists of n unique elements, but you have forgotten it. However, you do remember every pair of adjacent elements in nums.

+ +

You are given a 2D integer array adjacentPairs of size n - 1 where each adjacentPairs[i] = [ui, vi] indicates that the elements ui and vi are adjacent in nums.

+ +

It is guaranteed that every adjacent pair of elements nums[i] and nums[i+1] will exist in adjacentPairs, either as [nums[i], nums[i+1]] or [nums[i+1], nums[i]]. The pairs can appear in any order.

+ +

Return the original array nums. If there are multiple solutions, return any of them.

+ +

 

+

Example 1:

+ +
Input: adjacentPairs = [[2,1],[3,4],[3,2]]
+Output: [1,2,3,4]
+Explanation: This array has all its adjacent pairs in adjacentPairs.
+Notice that adjacentPairs[i] may not be in left-to-right order.
+
+ +

Example 2:

+ +
Input: adjacentPairs = [[4,-2],[1,4],[-3,1]]
+Output: [-2,4,1,-3]
+Explanation: There can be negative numbers.
+Another solution is [-3,1,4,-2], which would also be accepted.
+
+ +

Example 3:

+ +
Input: adjacentPairs = [[100000,-100000]]
+Output: [100000,-100000]
+
+ +

 

+

Constraints:

+ +
    +
  • nums.length == n
  • +
  • adjacentPairs.length == n - 1
  • +
  • adjacentPairs[i].length == 2
  • +
  • 2 <= n <= 105
  • +
  • -105 <= nums[i], ui, vi <= 105
  • +
  • There exists some nums that has adjacentPairs as its pairs.
  • +
+
\ No newline at end of file From 3abd6cd1fff29a7f714bc24e412e17e337142588 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 10 Nov 2023 08:35:42 +0530 Subject: [PATCH 0332/1399] Attach NOTES - LeetHub From d4c8f4a7fa17b5845f4720f16c75021fee223fd5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 10 Nov 2023 08:35:46 +0530 Subject: [PATCH 0333/1399] Time: 450 ms (46.97%), Space: 192 MB (5.15%) - LeetHub --- ...-restore-the-array-from-adjacent-pairs.cpp | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 1743-restore-the-array-from-adjacent-pairs/1743-restore-the-array-from-adjacent-pairs.cpp diff --git a/1743-restore-the-array-from-adjacent-pairs/1743-restore-the-array-from-adjacent-pairs.cpp b/1743-restore-the-array-from-adjacent-pairs/1743-restore-the-array-from-adjacent-pairs.cpp new file mode 100644 index 00000000..483ec42c --- /dev/null +++ b/1743-restore-the-array-from-adjacent-pairs/1743-restore-the-array-from-adjacent-pairs.cpp @@ -0,0 +1,48 @@ +class Solution { +public: + vector restoreArray(vector>& adjacentPairs) { + + unordered_map> adj; + unordered_map mp; + + int sv = -1; + + for(auto& itr : adjacentPairs) + { + adj[itr[0]].push_back(itr[1]); + adj[itr[1]].push_back(itr[0]); + + ++mp[itr[0]]; + ++mp[itr[1]]; + } + + for(auto& [f, s] : mp) + { + if(s == 1) + sv = f; + } + + vector ans; + + unordered_set visited; + + function dfs = [&](int sv) + { + ans.push_back(sv); + visited.insert(sv); + + for(auto& itr : adj[sv]) + { + if(!visited.count(itr)) + dfs(itr); + } + }; + + // cout< Date: Sat, 11 Nov 2023 14:45:09 +0530 Subject: [PATCH 0334/1399] Create README - LeetHub --- .../README.md | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2642-design-graph-with-shortest-path-calculator/README.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2642-design-graph-with-shortest-path-calculator/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2642-design-graph-with-shortest-path-calculator/README.md new file mode 100644 index 00000000..09d240cb --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2642-design-graph-with-shortest-path-calculator/README.md @@ -0,0 +1,41 @@ +

No companies found for this problem
2642. Design Graph With Shortest Path Calculator

Hard


There is a directed weighted graph that consists of n nodes numbered from 0 to n - 1. The edges of the graph are initially represented by the given array edges where edges[i] = [fromi, toi, edgeCosti] meaning that there is an edge from fromi to toi with the cost edgeCosti.

+ +

Implement the Graph class:

+ +
    +
  • Graph(int n, int[][] edges) initializes the object with n nodes and the given edges.
  • +
  • addEdge(int[] edge) adds an edge to the list of edges where edge = [from, to, edgeCost]. It is guaranteed that there is no edge between the two nodes before adding this one.
  • +
  • int shortestPath(int node1, int node2) returns the minimum cost of a path from node1 to node2. If no path exists, return -1. The cost of a path is the sum of the costs of the edges in the path.
  • +
+ +

 

+

Example 1:

+ +
Input
+["Graph", "shortestPath", "shortestPath", "addEdge", "shortestPath"]
+[[4, [[0, 2, 5], [0, 1, 2], [1, 2, 1], [3, 0, 3]]], [3, 2], [0, 3], [[1, 3, 4]], [0, 3]]
+Output
+[null, 6, -1, null, 6]
+
+Explanation
+Graph g = new Graph(4, [[0, 2, 5], [0, 1, 2], [1, 2, 1], [3, 0, 3]]);
+g.shortestPath(3, 2); // return 6. The shortest path from 3 to 2 in the first diagram above is 3 -> 0 -> 1 -> 2 with a total cost of 3 + 2 + 1 = 6.
+g.shortestPath(0, 3); // return -1. There is no path from 0 to 3.
+g.addEdge([1, 3, 4]); // We add an edge from node 1 to node 3, and we get the second diagram above.
+g.shortestPath(0, 3); // return 6. The shortest path from 0 to 3 now is 0 -> 1 -> 3 with a total cost of 2 + 4 = 6.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= 100
  • +
  • 0 <= edges.length <= n * (n - 1)
  • +
  • edges[i].length == edge.length == 3
  • +
  • 0 <= fromi, toi, from, to, node1, node2 <= n - 1
  • +
  • 1 <= edgeCosti, edgeCost <= 106
  • +
  • There are no repeated edges and no self-loops in the graph at any point.
  • +
  • At most 100 calls will be made for addEdge.
  • +
  • At most 100 calls will be made for shortestPath.
  • +
+
\ No newline at end of file From e6d74a1d1e216e7095baf8cef81b7cd360541118 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 11 Nov 2023 14:45:13 +0530 Subject: [PATCH 0335/1399] Time: 140 ms (94.12%), Space: 76.4 MB (73.39%) - LeetHub --- ...gn-graph-with-shortest-path-calculator.cpp | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2642-design-graph-with-shortest-path-calculator/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2642-design-graph-with-shortest-path-calculator.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2642-design-graph-with-shortest-path-calculator/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2642-design-graph-with-shortest-path-calculator.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2642-design-graph-with-shortest-path-calculator/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2642-design-graph-with-shortest-path-calculator.cpp new file mode 100644 index 00000000..b46998f3 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2642-design-graph-with-shortest-path-calculator/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2642-design-graph-with-shortest-path-calculator.cpp @@ -0,0 +1,76 @@ +class Graph { +public: + + vector > adj[105]; + + int dijistras(int src, int dest) + { + vector dist(105, INT_MAX); + + priority_queue, vector>, greater> > pq; + + dist[src] = 0; + + pq.push({dist[src], src}); + + while(!pq.empty()) + { + pair curr = pq.top(); + pq.pop(); + + int distance = curr.first; + int node = curr.second; + + if(node == dest) + { + return distance; + } + + for(auto& child : adj[node]) + { + if(distance + child.second < dist[child.first]) + { + dist[child.first] = distance + child.second; + + pq.push({dist[child.first], child.first}); + } + } + } + return -1; + } + + Graph(int n, vector>& edges) { + + for(auto& edge : edges) + { + int u = edge[0]; + int v = edge[1]; + int wt = edge[2]; + + adj[u].push_back({v, wt}); + } + } + + void addEdge(vector edge) { + + int u = edge[0]; + int v = edge[1]; + int wt = edge[2]; + + adj[u].push_back({v, wt}); + + } + + int shortestPath(int node1, int node2) { + + return dijistras(node1, node2); + + } +}; + +/** + * Your Graph object will be instantiated and called as such: + * Graph* obj = new Graph(n, edges); + * obj->addEdge(edge); + * int param_2 = obj->shortestPath(node1,node2); + */ \ No newline at end of file From 19a1e1098604729d4ed79189d712b10d04ccb89c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 11 Nov 2023 14:46:31 +0530 Subject: [PATCH 0336/1399] Create README - LeetHub --- .../README.md | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 2642-design-graph-with-shortest-path-calculator/README.md diff --git a/2642-design-graph-with-shortest-path-calculator/README.md b/2642-design-graph-with-shortest-path-calculator/README.md new file mode 100644 index 00000000..151185c6 --- /dev/null +++ b/2642-design-graph-with-shortest-path-calculator/README.md @@ -0,0 +1,41 @@ +

2642. Design Graph With Shortest Path Calculator

Hard


There is a directed weighted graph that consists of n nodes numbered from 0 to n - 1. The edges of the graph are initially represented by the given array edges where edges[i] = [fromi, toi, edgeCosti] meaning that there is an edge from fromi to toi with the cost edgeCosti.

+ +

Implement the Graph class:

+ +
    +
  • Graph(int n, int[][] edges) initializes the object with n nodes and the given edges.
  • +
  • addEdge(int[] edge) adds an edge to the list of edges where edge = [from, to, edgeCost]. It is guaranteed that there is no edge between the two nodes before adding this one.
  • +
  • int shortestPath(int node1, int node2) returns the minimum cost of a path from node1 to node2. If no path exists, return -1. The cost of a path is the sum of the costs of the edges in the path.
  • +
+ +

 

+

Example 1:

+ +
Input
+["Graph", "shortestPath", "shortestPath", "addEdge", "shortestPath"]
+[[4, [[0, 2, 5], [0, 1, 2], [1, 2, 1], [3, 0, 3]]], [3, 2], [0, 3], [[1, 3, 4]], [0, 3]]
+Output
+[null, 6, -1, null, 6]
+
+Explanation
+Graph g = new Graph(4, [[0, 2, 5], [0, 1, 2], [1, 2, 1], [3, 0, 3]]);
+g.shortestPath(3, 2); // return 6. The shortest path from 3 to 2 in the first diagram above is 3 -> 0 -> 1 -> 2 with a total cost of 3 + 2 + 1 = 6.
+g.shortestPath(0, 3); // return -1. There is no path from 0 to 3.
+g.addEdge([1, 3, 4]); // We add an edge from node 1 to node 3, and we get the second diagram above.
+g.shortestPath(0, 3); // return 6. The shortest path from 0 to 3 now is 0 -> 1 -> 3 with a total cost of 2 + 4 = 6.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= 100
  • +
  • 0 <= edges.length <= n * (n - 1)
  • +
  • edges[i].length == edge.length == 3
  • +
  • 0 <= fromi, toi, from, to, node1, node2 <= n - 1
  • +
  • 1 <= edgeCosti, edgeCost <= 106
  • +
  • There are no repeated edges and no self-loops in the graph at any point.
  • +
  • At most 100 calls will be made for addEdge.
  • +
  • At most 100 calls will be made for shortestPath.
  • +
+
\ No newline at end of file From faee9ab74e0950c5faca6b54f4894bb5c15a4b33 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 11 Nov 2023 14:46:31 +0530 Subject: [PATCH 0337/1399] Attach NOTES - LeetHub --- 2642-design-graph-with-shortest-path-calculator/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2642-design-graph-with-shortest-path-calculator/NOTES.md diff --git a/2642-design-graph-with-shortest-path-calculator/NOTES.md b/2642-design-graph-with-shortest-path-calculator/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2642-design-graph-with-shortest-path-calculator/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 1f58940787f1bb6903745ecaae1a97b58f7410b8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 11 Nov 2023 14:46:35 +0530 Subject: [PATCH 0338/1399] Time: 140 ms (94.12%), Space: 76.4 MB (73.39%) - LeetHub --- ...gn-graph-with-shortest-path-calculator.cpp | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 2642-design-graph-with-shortest-path-calculator/2642-design-graph-with-shortest-path-calculator.cpp diff --git a/2642-design-graph-with-shortest-path-calculator/2642-design-graph-with-shortest-path-calculator.cpp b/2642-design-graph-with-shortest-path-calculator/2642-design-graph-with-shortest-path-calculator.cpp new file mode 100644 index 00000000..b46998f3 --- /dev/null +++ b/2642-design-graph-with-shortest-path-calculator/2642-design-graph-with-shortest-path-calculator.cpp @@ -0,0 +1,76 @@ +class Graph { +public: + + vector > adj[105]; + + int dijistras(int src, int dest) + { + vector dist(105, INT_MAX); + + priority_queue, vector>, greater> > pq; + + dist[src] = 0; + + pq.push({dist[src], src}); + + while(!pq.empty()) + { + pair curr = pq.top(); + pq.pop(); + + int distance = curr.first; + int node = curr.second; + + if(node == dest) + { + return distance; + } + + for(auto& child : adj[node]) + { + if(distance + child.second < dist[child.first]) + { + dist[child.first] = distance + child.second; + + pq.push({dist[child.first], child.first}); + } + } + } + return -1; + } + + Graph(int n, vector>& edges) { + + for(auto& edge : edges) + { + int u = edge[0]; + int v = edge[1]; + int wt = edge[2]; + + adj[u].push_back({v, wt}); + } + } + + void addEdge(vector edge) { + + int u = edge[0]; + int v = edge[1]; + int wt = edge[2]; + + adj[u].push_back({v, wt}); + + } + + int shortestPath(int node1, int node2) { + + return dijistras(node1, node2); + + } +}; + +/** + * Your Graph object will be instantiated and called as such: + * Graph* obj = new Graph(n, edges); + * obj->addEdge(edge); + * int param_2 = obj->shortestPath(node1,node2); + */ \ No newline at end of file From a68e363aa8c1fda242838e5c41f4d7be84ff8e86 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 11 Nov 2023 14:47:01 +0530 Subject: [PATCH 0339/1399] Attach NOTES - LeetHub From f3238a28b57c15fbe21880bcf4d39493e57d246c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 11 Nov 2023 14:47:04 +0530 Subject: [PATCH 0340/1399] Time: 140 ms (94.12%), Space: 76.4 MB (73.39%) - LeetHub From 5de984e2dbb0eccc1ffe7f01dac21c0a2f4cb89c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 13 Nov 2023 01:51:25 +0530 Subject: [PATCH 0341/1399] Create README - LeetHub --- .../README.md | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div815-bus-routes/README.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div815-bus-routes/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div815-bus-routes/README.md new file mode 100644 index 00000000..6d9ea192 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div815-bus-routes/README.md @@ -0,0 +1,36 @@ +

No companies found for this problem
815. Bus Routes

Hard


You are given an array routes representing bus routes where routes[i] is a bus route that the ith bus repeats forever.

+ +
    +
  • For example, if routes[0] = [1, 5, 7], this means that the 0th bus travels in the sequence 1 -> 5 -> 7 -> 1 -> 5 -> 7 -> 1 -> ... forever.
  • +
+ +

You will start at the bus stop source (You are not on any bus initially), and you want to go to the bus stop target. You can travel between bus stops by buses only.

+ +

Return the least number of buses you must take to travel from source to target. Return -1 if it is not possible.

+ +

 

+

Example 1:

+ +
Input: routes = [[1,2,7],[3,6,7]], source = 1, target = 6
+Output: 2
+Explanation: The best strategy is take the first bus to the bus stop 7, then take the second bus to the bus stop 6.
+
+ +

Example 2:

+ +
Input: routes = [[7,12],[4,5,15],[6],[15,19],[9,12,13]], source = 15, target = 12
+Output: -1
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= routes.length <= 500.
  • +
  • 1 <= routes[i].length <= 105
  • +
  • All the values of routes[i] are unique.
  • +
  • sum(routes[i].length) <= 105
  • +
  • 0 <= routes[i][j] < 106
  • +
  • 0 <= source, target < 106
  • +
+
\ No newline at end of file From dd8fe08512826eef41eb82607e7f332897fce31f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 13 Nov 2023 01:51:29 +0530 Subject: [PATCH 0342/1399] Time: 608 ms (38.08%), Space: 55.5 MB (89.95%) - LeetHub --- ...rect-svg-div-div-div-div815-bus-routes.cpp | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div815-bus-routes/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div815-bus-routes.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div815-bus-routes/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div815-bus-routes.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div815-bus-routes/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div815-bus-routes.cpp new file mode 100644 index 00000000..fd1cd0af --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div815-bus-routes/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div815-bus-routes.cpp @@ -0,0 +1,58 @@ +class Solution { +public: + int numBusesToDestination(vector>& routes, int source, int target) { + + if(source == target) + return 0; + + unordered_map> adj; + + for(int route = 0; route < routes.size(); ++route) + { + for(auto& stop : routes[route]) + { + adj[stop].push_back(route); + } + } + + queue q; + + unordered_set visited; + + for(auto& route : adj[source]) + { + q.push(route); + visited.insert(route); + } + + int bus = 1; + + while(!q.empty()) + { + int size = q.size(); + + while(size--) + { + int route = q.front(); + q.pop(); + + for(auto& stop : routes[route]) + { + if(stop == target) + return bus; + + for(auto& nextRoute : adj[stop]) + { + if(visited.count(nextRoute)) + continue; + q.push(nextRoute); + visited.insert(nextRoute); + } + } + } + ++bus; + } + + return -1; + } +}; \ No newline at end of file From 6f04bc39348661435e7729af6505d8e2272ddc7d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 13 Nov 2023 08:47:17 +0530 Subject: [PATCH 0343/1399] Create README - LeetHub --- .../README.md | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2785-sort-vowels-in-a-string/README.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2785-sort-vowels-in-a-string/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2785-sort-vowels-in-a-string/README.md new file mode 100644 index 00000000..c4634ae8 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2785-sort-vowels-in-a-string/README.md @@ -0,0 +1,34 @@ +

No companies found for this problem
2785. Sort Vowels in a String

Medium


Given a 0-indexed string s, permute s to get a new string t such that:

+ +
    +
  • All consonants remain in their original places. More formally, if there is an index i with 0 <= i < s.length such that s[i] is a consonant, then t[i] = s[i].
  • +
  • The vowels must be sorted in the nondecreasing order of their ASCII values. More formally, for pairs of indices i, j with 0 <= i < j < s.length such that s[i] and s[j] are vowels, then t[i] must not have a higher ASCII value than t[j].
  • +
+ +

Return the resulting string.

+ +

The vowels are 'a', 'e', 'i', 'o', and 'u', and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.

+ +

 

+

Example 1:

+ +
Input: s = "lEetcOde"
+Output: "lEOtcede"
+Explanation: 'E', 'O', and 'e' are the vowels in s; 'l', 't', 'c', and 'd' are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places.
+
+ +

Example 2:

+ +
Input: s = "lYmpH"
+Output: "lYmpH"
+Explanation: There are no vowels in s (all characters in s are consonants), so we return "lYmpH".
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 105
  • +
  • s consists only of letters of the English alphabet in uppercase and lowercase.
  • +
+
\ No newline at end of file From 1ebb5fdb9490724f36ca50f19ca1c189dfc3d879 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 13 Nov 2023 08:47:21 +0530 Subject: [PATCH 0344/1399] Time: 43 ms (47.30%), Space: 12.1 MB (85.13%) - LeetHub --- ...iv-div-div2785-sort-vowels-in-a-string.cpp | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2785-sort-vowels-in-a-string/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2785-sort-vowels-in-a-string.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2785-sort-vowels-in-a-string/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2785-sort-vowels-in-a-string.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2785-sort-vowels-in-a-string/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2785-sort-vowels-in-a-string.cpp new file mode 100644 index 00000000..f6265c5a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2785-sort-vowels-in-a-string/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2785-sort-vowels-in-a-string.cpp @@ -0,0 +1,30 @@ +class Solution { +public: + string sortVowels(string s) { + + string vowels; + + auto isVowel = [&](char ch) + { + return (ch == 'a' or ch == 'e' or ch == 'i' or ch == 'o' or ch == 'u' or ch == 'A' or ch == 'E' or ch == 'I' or ch == 'O' or ch == 'U'); + }; + + for(auto& ch : s) + { + if(isVowel(ch)) + vowels += ch; + } + + sort(vowels.begin(), vowels.end()); + + int i = 0; + + for(auto& ch : s) + { + if(isVowel(ch)) + ch = vowels[i++]; + } + + return s; + } +}; \ No newline at end of file From 15ed7d6260612321887567f937ec41bd3f68e70d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 13 Nov 2023 08:50:19 +0530 Subject: [PATCH 0345/1399] Create README - LeetHub --- 2785-sort-vowels-in-a-string/README.md | 34 ++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 2785-sort-vowels-in-a-string/README.md diff --git a/2785-sort-vowels-in-a-string/README.md b/2785-sort-vowels-in-a-string/README.md new file mode 100644 index 00000000..e6652348 --- /dev/null +++ b/2785-sort-vowels-in-a-string/README.md @@ -0,0 +1,34 @@ +

2785. Sort Vowels in a String

Medium


Given a 0-indexed string s, permute s to get a new string t such that:

+ +
    +
  • All consonants remain in their original places. More formally, if there is an index i with 0 <= i < s.length such that s[i] is a consonant, then t[i] = s[i].
  • +
  • The vowels must be sorted in the nondecreasing order of their ASCII values. More formally, for pairs of indices i, j with 0 <= i < j < s.length such that s[i] and s[j] are vowels, then t[i] must not have a higher ASCII value than t[j].
  • +
+ +

Return the resulting string.

+ +

The vowels are 'a', 'e', 'i', 'o', and 'u', and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.

+ +

 

+

Example 1:

+ +
Input: s = "lEetcOde"
+Output: "lEOtcede"
+Explanation: 'E', 'O', and 'e' are the vowels in s; 'l', 't', 'c', and 'd' are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places.
+
+ +

Example 2:

+ +
Input: s = "lYmpH"
+Output: "lYmpH"
+Explanation: There are no vowels in s (all characters in s are consonants), so we return "lYmpH".
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 105
  • +
  • s consists only of letters of the English alphabet in uppercase and lowercase.
  • +
+
\ No newline at end of file From e5b97df88ffe0ba3c4a51caa03788bdfa2ae70aa Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 13 Nov 2023 08:50:19 +0530 Subject: [PATCH 0346/1399] Attach NOTES - LeetHub --- 2785-sort-vowels-in-a-string/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2785-sort-vowels-in-a-string/NOTES.md diff --git a/2785-sort-vowels-in-a-string/NOTES.md b/2785-sort-vowels-in-a-string/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2785-sort-vowels-in-a-string/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From c40c83c69ba9a812a83f1a47ea79236b91849c64 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 14 Nov 2023 10:22:05 +0530 Subject: [PATCH 0347/1399] Create README - LeetHub --- .../README.md | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 1930-unique-length-3-palindromic-subsequences/README.md diff --git a/1930-unique-length-3-palindromic-subsequences/README.md b/1930-unique-length-3-palindromic-subsequences/README.md new file mode 100644 index 00000000..099bc405 --- /dev/null +++ b/1930-unique-length-3-palindromic-subsequences/README.md @@ -0,0 +1,49 @@ +

1930. Unique Length-3 Palindromic Subsequences

Medium


Given a string s, return the number of unique palindromes of length three that are a subsequence of s.

+ +

Note that even if there are multiple ways to obtain the same subsequence, it is still only counted once.

+ +

A palindrome is a string that reads the same forwards and backwards.

+ +

A subsequence of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters.

+ +
    +
  • For example, "ace" is a subsequence of "abcde".
  • +
+ +

 

+

Example 1:

+ +
Input: s = "aabca"
+Output: 3
+Explanation: The 3 palindromic subsequences of length 3 are:
+- "aba" (subsequence of "aabca")
+- "aaa" (subsequence of "aabca")
+- "aca" (subsequence of "aabca")
+
+ +

Example 2:

+ +
Input: s = "adc"
+Output: 0
+Explanation: There are no palindromic subsequences of length 3 in "adc".
+
+ +

Example 3:

+ +
Input: s = "bbcbaba"
+Output: 4
+Explanation: The 4 palindromic subsequences of length 3 are:
+- "bbb" (subsequence of "bbcbaba")
+- "bcb" (subsequence of "bbcbaba")
+- "bab" (subsequence of "bbcbaba")
+- "aba" (subsequence of "bbcbaba")
+
+ +

 

+

Constraints:

+ +
    +
  • 3 <= s.length <= 105
  • +
  • s consists of only lowercase English letters.
  • +
+
\ No newline at end of file From 08b1c8dff145f41ec721fde23225d85d25361231 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 14 Nov 2023 10:22:06 +0530 Subject: [PATCH 0348/1399] Attach NOTES - LeetHub --- 1930-unique-length-3-palindromic-subsequences/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1930-unique-length-3-palindromic-subsequences/NOTES.md diff --git a/1930-unique-length-3-palindromic-subsequences/NOTES.md b/1930-unique-length-3-palindromic-subsequences/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1930-unique-length-3-palindromic-subsequences/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 39e810817684222d66c8d76c83d202d58c27b0fe Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 14 Nov 2023 10:22:09 +0530 Subject: [PATCH 0349/1399] Time: 67 ms (93.61%), Space: 23.6 MB (20.09%) - LeetHub --- ...ique-length-3-palindromic-subsequences.cpp | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 1930-unique-length-3-palindromic-subsequences/1930-unique-length-3-palindromic-subsequences.cpp diff --git a/1930-unique-length-3-palindromic-subsequences/1930-unique-length-3-palindromic-subsequences.cpp b/1930-unique-length-3-palindromic-subsequences/1930-unique-length-3-palindromic-subsequences.cpp new file mode 100644 index 00000000..48915358 --- /dev/null +++ b/1930-unique-length-3-palindromic-subsequences/1930-unique-length-3-palindromic-subsequences.cpp @@ -0,0 +1,50 @@ +class Solution { +public: + int countPalindromicSubsequence(string s) { + + int n = s.size(); + + unordered_map> mp; + unordered_map firstOccurence, lastOccurence; + int palindrome = 0; + + for(int i = 0; i < n; ++i) + { + if(firstOccurence.find(s[i]) == firstOccurence.end()) + { + firstOccurence[s[i]] = i; + } + + lastOccurence[s[i]] = i; + + mp[s[i]].push_back(i); + } + + for(int ch = 'a'; ch <= 'z'; ++ch) + { + int left = firstOccurence[ch]; + int right = lastOccurence[ch]; + + if(right - left <= 1) + continue; + + for(char x = 'a'; x <= 'z'; ++x) + { + if(mp.find(x) != mp.end()) + { + int ub = upper_bound(mp[x].begin(), mp[x].end(), left) - mp[x].begin(); + + if(ub == mp[x].size()) + continue; + + int idx = mp[x][ub]; + + if(idx < right) + ++palindrome; + } + } + } + + return palindrome; + } +}; \ No newline at end of file From 94eb90ef71fa109edf0fa21ef5a0eb0c0e08180d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 14 Nov 2023 10:27:51 +0530 Subject: [PATCH 0350/1399] Attach NOTES - LeetHub From 194fc9ec853078faa0168ddeb999305a7ae45d23 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 14 Nov 2023 10:27:54 +0530 Subject: [PATCH 0351/1399] Time: 67 ms (93.61%), Space: 23.6 MB (20.09%) - LeetHub From 7f45de4b5091e41ed8058bc7d1cbd7e726ab58c3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 14 Nov 2023 10:32:05 +0530 Subject: [PATCH 0352/1399] Attach NOTES - LeetHub From b7aa11e953f3f7817bc13a5a8e9b6bac00782330 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 14 Nov 2023 10:32:09 +0530 Subject: [PATCH 0353/1399] Time: 67 ms (93.61%), Space: 23.6 MB (20.09%) - LeetHub From f85b34a0569394c92bcf8e3e8fb745cd67333711 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 15 Nov 2023 18:27:25 +0530 Subject: [PATCH 0354/1399] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1846-maximum-element-after-decreasing-and-rearranging/NOTES.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1846-maximum-element-after-decreasing-and-rearranging/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1846-maximum-element-after-decreasing-and-rearranging/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1846-maximum-element-after-decreasing-and-rearranging/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 8307c12dfbf7906ac949d4aff6780106b933cb4e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 15 Nov 2023 18:27:29 +0530 Subject: [PATCH 0355/1399] Time: 68 ms (94.40%), Space: 51.5 MB (56.72%) - LeetHub --- ...ement-after-decreasing-and-rearranging.cpp | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1846-maximum-element-after-decreasing-and-rearranging/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1846-maximum-element-after-decreasing-and-rearranging.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1846-maximum-element-after-decreasing-and-rearranging/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1846-maximum-element-after-decreasing-and-rearranging.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1846-maximum-element-after-decreasing-and-rearranging/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1846-maximum-element-after-decreasing-and-rearranging.cpp new file mode 100644 index 00000000..62f90fac --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1846-maximum-element-after-decreasing-and-rearranging/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1846-maximum-element-after-decreasing-and-rearranging.cpp @@ -0,0 +1,29 @@ +class Solution { +public: + int maximumElementAfterDecrementingAndRearranging(vector& arr) { + + int n = arr.size(); + + sort(arr.begin(), arr.end()); + + int operation = 0; + + if(arr[0] != 1) + { + arr[0] = 1; + } + + int ans = arr[0]; + + for(int i = 1; i < n; ++i) + { + if(abs(arr[i] - arr[i-1]) > 1) + { + arr[i] = arr[i-1] + 1; + ++operation; + } + ans = max(ans, arr[i]); + } + return ans; + } +}; \ No newline at end of file From 3f391853eb004ecc04e0b77ceca50382e121b3be Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Nov 2023 18:37:30 +0530 Subject: [PATCH 0356/1399] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1980-find-unique-binary-string/NOTES.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1980-find-unique-binary-string/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1980-find-unique-binary-string/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1980-find-unique-binary-string/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 47ca52f3157c11171db16236b14e1f816cc2d6a8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Nov 2023 18:37:33 +0530 Subject: [PATCH 0357/1399] Time: 1384 ms (10.27%), Space: 250.6 MB (8.76%) - LeetHub --- ...-div-div1980-find-unique-binary-string.cpp | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1980-find-unique-binary-string/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1980-find-unique-binary-string.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1980-find-unique-binary-string/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1980-find-unique-binary-string.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1980-find-unique-binary-string/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1980-find-unique-binary-string.cpp new file mode 100644 index 00000000..e5ab733e --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1980-find-unique-binary-string/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1980-find-unique-binary-string.cpp @@ -0,0 +1,30 @@ +class Solution { +public: + + string ans; + + void helper(int n, string curr, unordered_set& st) + { + if(curr.size() == n) + { + if(!st.count(curr)) + ans = curr; + return; + } + curr += '0'; + helper(n, curr, st); + curr.pop_back(); + curr += '1'; + helper(n, curr, st); + } + + string findDifferentBinaryString(vector& nums) { + + unordered_set st(nums.begin(), nums.end()); + + helper(nums.size(), "", st); + + return ans; + + } +}; \ No newline at end of file From ed41da85bb9e43931ce63d8c117d63077eaa9699 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Nov 2023 18:45:24 +0530 Subject: [PATCH 0358/1399] Create README - LeetHub --- 1980-find-unique-binary-string/README.md | 35 ++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 1980-find-unique-binary-string/README.md diff --git a/1980-find-unique-binary-string/README.md b/1980-find-unique-binary-string/README.md new file mode 100644 index 00000000..8cdd0a25 --- /dev/null +++ b/1980-find-unique-binary-string/README.md @@ -0,0 +1,35 @@ +

1980. Find Unique Binary String

Medium


Given an array of strings nums containing n unique binary strings each of length n, return a binary string of length n that does not appear in nums. If there are multiple answers, you may return any of them.

+ +

 

+

Example 1:

+ +
Input: nums = ["01","10"]
+Output: "11"
+Explanation: "11" does not appear in nums. "00" would also be correct.
+
+ +

Example 2:

+ +
Input: nums = ["00","01"]
+Output: "11"
+Explanation: "11" does not appear in nums. "10" would also be correct.
+
+ +

Example 3:

+ +
Input: nums = ["111","011","001"]
+Output: "101"
+Explanation: "101" does not appear in nums. "000", "010", "100", and "110" would also be correct.
+
+ +

 

+

Constraints:

+ +
    +
  • n == nums.length
  • +
  • 1 <= n <= 16
  • +
  • nums[i].length == n
  • +
  • nums[i] is either '0' or '1'.
  • +
  • All the strings of nums are unique.
  • +
+
\ No newline at end of file From 26d9ddd86a9db60f7aa6f1936fc05e9580faf8dd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Nov 2023 18:45:27 +0530 Subject: [PATCH 0359/1399] Time: 1384 ms (10.27%), Space: 250.6 MB (8.76%) - LeetHub --- .../1980-find-unique-binary-string.cpp | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 1980-find-unique-binary-string/1980-find-unique-binary-string.cpp diff --git a/1980-find-unique-binary-string/1980-find-unique-binary-string.cpp b/1980-find-unique-binary-string/1980-find-unique-binary-string.cpp new file mode 100644 index 00000000..e5ab733e --- /dev/null +++ b/1980-find-unique-binary-string/1980-find-unique-binary-string.cpp @@ -0,0 +1,30 @@ +class Solution { +public: + + string ans; + + void helper(int n, string curr, unordered_set& st) + { + if(curr.size() == n) + { + if(!st.count(curr)) + ans = curr; + return; + } + curr += '0'; + helper(n, curr, st); + curr.pop_back(); + curr += '1'; + helper(n, curr, st); + } + + string findDifferentBinaryString(vector& nums) { + + unordered_set st(nums.begin(), nums.end()); + + helper(nums.size(), "", st); + + return ans; + + } +}; \ No newline at end of file From 1fcee2662d915917d237d08dc8d22d6cef8186dc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Nov 2023 18:46:11 +0530 Subject: [PATCH 0360/1399] Attach NOTES - LeetHub --- 1980-find-unique-binary-string/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1980-find-unique-binary-string/NOTES.md diff --git a/1980-find-unique-binary-string/NOTES.md b/1980-find-unique-binary-string/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1980-find-unique-binary-string/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 28eeffa97120f3edaea4ecc0583399b9e7b09f94 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Nov 2023 18:46:14 +0530 Subject: [PATCH 0361/1399] Time: 1384 ms (10.27%), Space: 250.6 MB (8.76%) - LeetHub From 6a961ff266bd3b8a569aea569d57a965f9b1f84a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Nov 2023 18:48:13 +0530 Subject: [PATCH 0362/1399] Attach NOTES - LeetHub From 923510768ddae96144e47af34d0f1a37a8117b75 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Nov 2023 18:48:16 +0530 Subject: [PATCH 0363/1399] Time: 1384 ms (10.27%), Space: 250.6 MB (8.76%) - LeetHub From 5d600782f2a01e25d017ce482feff1f13e701c84 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Nov 2023 18:48:32 +0530 Subject: [PATCH 0364/1399] Attach NOTES - LeetHub From c4fac30b9ec11ec207e454b4ed4aa954f680794d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Nov 2023 18:48:35 +0530 Subject: [PATCH 0365/1399] Time: 1384 ms (10.27%), Space: 250.6 MB (8.76%) - LeetHub From 56e1813e74bb8d29bffcffab9a4e8e66afec1264 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Nov 2023 18:49:46 +0530 Subject: [PATCH 0366/1399] Attach NOTES - LeetHub From 919ca1a1e6ef764e8c5a689701c2d256734cb760 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Nov 2023 18:49:49 +0530 Subject: [PATCH 0367/1399] Time: 1384 ms (10.27%), Space: 250.6 MB (8.76%) - LeetHub From 56f80536bc32fdf609bb838574b100e297d819e9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Nov 2023 18:50:54 +0530 Subject: [PATCH 0368/1399] Attach NOTES - LeetHub From 127bbf43f4b9edb62e7726e4fff1ebfb5c4ed8f8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Nov 2023 18:50:58 +0530 Subject: [PATCH 0369/1399] Time: 1384 ms (10.27%), Space: 250.6 MB (8.76%) - LeetHub From ddc300f97b0849f9bc58c2360711a0eca1ecd898 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Nov 2023 18:51:00 +0530 Subject: [PATCH 0370/1399] Attach NOTES - LeetHub From 46b371f90ea6035ae39cfb44bbf3fe4bdd3d5e5c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Nov 2023 18:51:04 +0530 Subject: [PATCH 0371/1399] Time: 1384 ms (10.27%), Space: 250.6 MB (8.76%) - LeetHub From 9d0589647c95d6dd2ce1773266ae77b2ff6183af Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Nov 2023 18:51:46 +0530 Subject: [PATCH 0372/1399] Attach NOTES - LeetHub From d720897369c8a409475d1b29d417d9a229a2ff76 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Nov 2023 18:51:50 +0530 Subject: [PATCH 0373/1399] Time: 6 ms (42.30%), Space: 10.4 MB (86.71%) - LeetHub --- ...-div-div1980-find-unique-binary-string.cpp | 28 ++++--------------- 1 file changed, 6 insertions(+), 22 deletions(-) diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1980-find-unique-binary-string/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1980-find-unique-binary-string.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1980-find-unique-binary-string/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1980-find-unique-binary-string.cpp index e5ab733e..776c6783 100644 --- a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1980-find-unique-binary-string/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1980-find-unique-binary-string.cpp +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1980-find-unique-binary-string/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1980-find-unique-binary-string.cpp @@ -1,30 +1,14 @@ class Solution { public: - - string ans; - - void helper(int n, string curr, unordered_set& st) - { - if(curr.size() == n) - { - if(!st.count(curr)) - ans = curr; - return; - } - curr += '0'; - helper(n, curr, st); - curr.pop_back(); - curr += '1'; - helper(n, curr, st); - } - string findDifferentBinaryString(vector& nums) { - unordered_set st(nums.begin(), nums.end()); + int n = nums.size(); - helper(nums.size(), "", st); - - return ans; + for(int i = 0; i < n; ++i) + { + if(nums[0][i] = nums[i][i] == '0' ? '1' : '0'); + } + return nums[0]; } }; \ No newline at end of file From 460566c33d95592783e98374760f5592b1c9f87d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Nov 2023 18:53:52 +0530 Subject: [PATCH 0374/1399] Attach NOTES - LeetHub From 464587185f29a1273adcbe37cb7237842d3063f7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 Nov 2023 18:53:56 +0530 Subject: [PATCH 0375/1399] Time: 6 ms (42.30%), Space: 10.4 MB (86.71%) - LeetHub --- .../1980-find-unique-binary-string.cpp | 28 ++++--------------- 1 file changed, 6 insertions(+), 22 deletions(-) diff --git a/1980-find-unique-binary-string/1980-find-unique-binary-string.cpp b/1980-find-unique-binary-string/1980-find-unique-binary-string.cpp index e5ab733e..776c6783 100644 --- a/1980-find-unique-binary-string/1980-find-unique-binary-string.cpp +++ b/1980-find-unique-binary-string/1980-find-unique-binary-string.cpp @@ -1,30 +1,14 @@ class Solution { public: - - string ans; - - void helper(int n, string curr, unordered_set& st) - { - if(curr.size() == n) - { - if(!st.count(curr)) - ans = curr; - return; - } - curr += '0'; - helper(n, curr, st); - curr.pop_back(); - curr += '1'; - helper(n, curr, st); - } - string findDifferentBinaryString(vector& nums) { - unordered_set st(nums.begin(), nums.end()); + int n = nums.size(); - helper(nums.size(), "", st); - - return ans; + for(int i = 0; i < n; ++i) + { + if(nums[0][i] = nums[i][i] == '0' ? '1' : '0'); + } + return nums[0]; } }; \ No newline at end of file From 6c4330be0bdced964e9e5294306f22dbde22eef5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 17 Nov 2023 08:14:59 +0530 Subject: [PATCH 0376/1399] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1877-minimize-maximum-pair-sum-in-array/NOTES.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1877-minimize-maximum-pair-sum-in-array/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1877-minimize-maximum-pair-sum-in-array/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1877-minimize-maximum-pair-sum-in-array/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From e0d15f75e49e747a8da1b4431af4664b30bde7da Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 17 Nov 2023 08:15:04 +0530 Subject: [PATCH 0377/1399] Time: 198 ms (21.02%), Space: 96.6 MB (61.58%) - LeetHub --- ...877-minimize-maximum-pair-sum-in-array.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1877-minimize-maximum-pair-sum-in-array/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1877-minimize-maximum-pair-sum-in-array.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1877-minimize-maximum-pair-sum-in-array/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1877-minimize-maximum-pair-sum-in-array.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1877-minimize-maximum-pair-sum-in-array/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1877-minimize-maximum-pair-sum-in-array.cpp new file mode 100644 index 00000000..12c01f52 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1877-minimize-maximum-pair-sum-in-array/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1877-minimize-maximum-pair-sum-in-array.cpp @@ -0,0 +1,19 @@ +class Solution { +public: + int minPairSum(vector& nums) { + + int n = nums.size(); + + int ans = 0; + + sort(nums.begin(), nums.end()); + + for(int i = 0; i <= n/2; ++i) + { + ans = max(ans, nums[i] + nums[n-i-1]); + } + + return ans; + + } +}; \ No newline at end of file From 3eab55a86f1c75315624d78c8fb9beb7bb80663b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 17 Nov 2023 08:15:51 +0530 Subject: [PATCH 0378/1399] Create README - LeetHub --- .../README.md | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 1877-minimize-maximum-pair-sum-in-array/README.md diff --git a/1877-minimize-maximum-pair-sum-in-array/README.md b/1877-minimize-maximum-pair-sum-in-array/README.md new file mode 100644 index 00000000..f49c9aa1 --- /dev/null +++ b/1877-minimize-maximum-pair-sum-in-array/README.md @@ -0,0 +1,41 @@ +

1877. Minimize Maximum Pair Sum in Array

Medium


The pair sum of a pair (a,b) is equal to a + b. The maximum pair sum is the largest pair sum in a list of pairs.

+ +
    +
  • For example, if we have pairs (1,5), (2,3), and (4,4), the maximum pair sum would be max(1+5, 2+3, 4+4) = max(6, 5, 8) = 8.
  • +
+ +

Given an array nums of even length n, pair up the elements of nums into n / 2 pairs such that:

+ +
    +
  • Each element of nums is in exactly one pair, and
  • +
  • The maximum pair sum is minimized.
  • +
+ +

Return the minimized maximum pair sum after optimally pairing up the elements.

+ +

 

+

Example 1:

+ +
Input: nums = [3,5,2,3]
+Output: 7
+Explanation: The elements can be paired up into pairs (3,3) and (5,2).
+The maximum pair sum is max(3+3, 5+2) = max(6, 7) = 7.
+
+ +

Example 2:

+ +
Input: nums = [3,5,4,2,4,6]
+Output: 8
+Explanation: The elements can be paired up into pairs (3,5), (4,4), and (6,2).
+The maximum pair sum is max(3+5, 4+4, 6+2) = max(8, 8, 8) = 8.
+
+ +

 

+

Constraints:

+ +
    +
  • n == nums.length
  • +
  • 2 <= n <= 105
  • +
  • n is even.
  • +
  • 1 <= nums[i] <= 105
  • +
\ No newline at end of file From 3094fa728bf9f0bbe42f14c5b8f589ed7cf42dcb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 17 Nov 2023 08:15:51 +0530 Subject: [PATCH 0379/1399] Attach NOTES - LeetHub --- 1877-minimize-maximum-pair-sum-in-array/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1877-minimize-maximum-pair-sum-in-array/NOTES.md diff --git a/1877-minimize-maximum-pair-sum-in-array/NOTES.md b/1877-minimize-maximum-pair-sum-in-array/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1877-minimize-maximum-pair-sum-in-array/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From f218b5b9183903c511f244a022aa2dcf8520c884 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 Nov 2023 21:40:01 +0530 Subject: [PATCH 0380/1399] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-hidden-flex-wrap-wrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondiv-styletransform-rotate180deg-svg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1838-frequency-of-the-most-frequent-element/NOTES.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-hidden-flex-wrap-wrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondiv-styletransform-rotate180deg-svg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1838-frequency-of-the-most-frequent-element/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-hidden-flex-wrap-wrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondiv-styletransform-rotate180deg-svg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1838-frequency-of-the-most-frequent-element/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-hidden-flex-wrap-wrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondiv-styletransform-rotate180deg-svg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1838-frequency-of-the-most-frequent-element/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 3e72d706065c65b8d72c917e1e26389c50c603c7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 Nov 2023 21:40:04 +0530 Subject: [PATCH 0381/1399] Time: 297 ms (5.15%), Space: 108 MB (5.28%) - LeetHub --- ...frequency-of-the-most-frequent-element.cpp | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-hidden-flex-wrap-wrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondiv-styletransform-rotate180deg-svg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1838-frequency-of-the-most-frequent-element/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-hidden-flex-wrap-wrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondiv-styletransform-rotate180deg-svg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1838-frequency-of-the-most-frequent-element.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-hidden-flex-wrap-wrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondiv-styletransform-rotate180deg-svg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1838-frequency-of-the-most-frequent-element/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-hidden-flex-wrap-wrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondiv-styletransform-rotate180deg-svg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1838-frequency-of-the-most-frequent-element.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-hidden-flex-wrap-wrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondiv-styletransform-rotate180deg-svg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1838-frequency-of-the-most-frequent-element/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-hidden-flex-wrap-wrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondiv-styletransform-rotate180deg-svg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1838-frequency-of-the-most-frequent-element.cpp new file mode 100644 index 00000000..d401154d --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-hidden-flex-wrap-wrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondiv-styletransform-rotate180deg-svg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1838-frequency-of-the-most-frequent-element/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-hidden-flex-wrap-wrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondiv-styletransform-rotate180deg-svg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1838-frequency-of-the-most-frequent-element.cpp @@ -0,0 +1,55 @@ +#define ll long long int + +class Solution { +public: + int maxFrequency(vector& nums, int k) { + + int n = nums.size(); + + sort(nums.begin(), nums.end()); + + vector pref(n+1, 0); + + for(int i = 1; i <= n; ++i) + { + pref[i] = (pref[i-1] + nums[i-1]); + } + + function calculate = [&](int targetIdx) + { + int ans = 0, val = nums[targetIdx]; + + int low = 0, high = targetIdx; + + while(low <= high) + { + int mid = (low + high) >> 1; + + int cnt = targetIdx - mid + 1; + + ll windowSum = val * 1LL * cnt; + + ll currSum = pref[targetIdx+1] - pref[mid]; + + if((windowSum - currSum) <= k) + { + ans = max(ans, cnt); + high = mid-1; + } + else + low = mid+1; + } + + return ans; + }; + + int res = 0; + + for(int i = 0; i < n; ++i) + { + res = max(res, calculate(i)); + } + + return res; + } +}; \ No newline at end of file From 3468d5f78047f1d7541ce38ce6dc8b6bc61d17ea Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 Nov 2023 22:18:34 +0530 Subject: [PATCH 0382/1399] Create README - LeetHub --- .../README.md | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1838-frequency-of-the-most-frequent-element/README.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1838-frequency-of-the-most-frequent-element/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1838-frequency-of-the-most-frequent-element/README.md new file mode 100644 index 00000000..bdb094f6 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1838-frequency-of-the-most-frequent-element/README.md @@ -0,0 +1,39 @@ +

No companies found for this problem
1838. Frequency of the Most Frequent Element

Medium


The frequency of an element is the number of times it occurs in an array.

+ +

You are given an integer array nums and an integer k. In one operation, you can choose an index of nums and increment the element at that index by 1.

+ +

Return the maximum possible frequency of an element after performing at most k operations.

+ +

 

+

Example 1:

+ +
Input: nums = [1,2,4], k = 5
+Output: 3
+Explanation: Increment the first element three times and the second element two times to make nums = [4,4,4].
+4 has a frequency of 3.
+ +

Example 2:

+ +
Input: nums = [1,4,8,13], k = 5
+Output: 2
+Explanation: There are multiple optimal solutions:
+- Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2.
+- Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2.
+- Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2.
+
+ +

Example 3:

+ +
Input: nums = [3,9,6], k = 2
+Output: 1
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 105
  • +
  • 1 <= nums[i] <= 105
  • +
  • 1 <= k <= 105
  • +
+
\ No newline at end of file From e6ff2911ca41d04d23793b121a9cf6243fb934f5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 Nov 2023 22:18:35 +0530 Subject: [PATCH 0383/1399] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1838-frequency-of-the-most-frequent-element/NOTES.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1838-frequency-of-the-most-frequent-element/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1838-frequency-of-the-most-frequent-element/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1838-frequency-of-the-most-frequent-element/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 95d5383217b26f854680d2e1fafe55101bd0d433 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 Nov 2023 22:18:38 +0530 Subject: [PATCH 0384/1399] Time: 169 ms (60.76%), Space: 99.3 MB (85.20%) - LeetHub --- ...frequency-of-the-most-frequent-element.cpp | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1838-frequency-of-the-most-frequent-element/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1838-frequency-of-the-most-frequent-element.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1838-frequency-of-the-most-frequent-element/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1838-frequency-of-the-most-frequent-element.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1838-frequency-of-the-most-frequent-element/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1838-frequency-of-the-most-frequent-element.cpp new file mode 100644 index 00000000..191717d7 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1838-frequency-of-the-most-frequent-element/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1838-frequency-of-the-most-frequent-element.cpp @@ -0,0 +1,34 @@ +class Solution { +public: + int maxFrequency(vector& nums, int k) { + + long long currSum = 0; + + int i = 0, j = 0, n = nums.size(); + + int ans = 0; + + sort(nums.begin(), nums.end()); + + while(j < n) + { + currSum += nums[j]; + + int cnt = j - i + 1; + + long long windowSum = cnt * 1LL * nums[j]; + + while(windowSum - currSum > k) + { + currSum -= nums[i]; + windowSum -= nums[j]; + ++i; + } + + ans = max(ans, (j-i+1)); + + ++j; + } + return ans; + } +}; \ No newline at end of file From 71d7ddfc56eebb0e436701184518dc4e40868494 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 Nov 2023 22:23:35 +0530 Subject: [PATCH 0385/1399] Attach NOTES - LeetHub From 9961fef62063877f4dddec855331ffc461079e91 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 Nov 2023 22:23:39 +0530 Subject: [PATCH 0386/1399] Time: 174 ms (44.80%), Space: 99.3 MB (43.97%) - LeetHub --- ...iv-div-div1838-frequency-of-the-most-frequent-element.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1838-frequency-of-the-most-frequent-element/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1838-frequency-of-the-most-frequent-element.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1838-frequency-of-the-most-frequent-element/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1838-frequency-of-the-most-frequent-element.cpp index 191717d7..39056bf9 100644 --- a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1838-frequency-of-the-most-frequent-element/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1838-frequency-of-the-most-frequent-element.cpp +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1838-frequency-of-the-most-frequent-element/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1838-frequency-of-the-most-frequent-element.cpp @@ -18,11 +18,10 @@ class Solution { long long windowSum = cnt * 1LL * nums[j]; - while(windowSum - currSum > k) + if(windowSum - currSum > k) { - currSum -= nums[i]; windowSum -= nums[j]; - ++i; + currSum -= nums[i++]; } ans = max(ans, (j-i+1)); From 5240a0de260fdf3e94f005cae267e61ab36f751a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 19 Nov 2023 07:51:15 +0530 Subject: [PATCH 0387/1399] Create README - LeetHub --- .../README.md | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1887-reduction-operations-to-make-the-array-elements-equal/README.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1887-reduction-operations-to-make-the-array-elements-equal/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1887-reduction-operations-to-make-the-array-elements-equal/README.md new file mode 100644 index 00000000..2cd89aae --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1887-reduction-operations-to-make-the-array-elements-equal/README.md @@ -0,0 +1,47 @@ +

No companies found for this problem
1887. Reduction Operations to Make the Array Elements Equal

Medium


Given an integer array nums, your goal is to make all elements in nums equal. To complete one operation, follow these steps:

+ +
    +
  1. Find the largest value in nums. Let its index be i (0-indexed) and its value be largest. If there are multiple elements with the largest value, pick the smallest i.
  2. +
  3. Find the next largest value in nums strictly smaller than largest. Let its value be nextLargest.
  4. +
  5. Reduce nums[i] to nextLargest.
  6. +
+ +

Return the number of operations to make all elements in nums equal.

+ +

 

+

Example 1:

+ +
Input: nums = [5,1,3]
+Output: 3
+Explanation: It takes 3 operations to make all elements in nums equal:
+1. largest = 5 at index 0. nextLargest = 3. Reduce nums[0] to 3. nums = [3,1,3].
+2. largest = 3 at index 0. nextLargest = 1. Reduce nums[0] to 1. nums = [1,1,3].
+3. largest = 3 at index 2. nextLargest = 1. Reduce nums[2] to 1. nums = [1,1,1].
+
+ +

Example 2:

+ +
Input: nums = [1,1,1]
+Output: 0
+Explanation: All elements in nums are already equal.
+
+ +

Example 3:

+ +
Input: nums = [1,1,2,2,3]
+Output: 4
+Explanation: It takes 4 operations to make all elements in nums equal:
+1. largest = 3 at index 4. nextLargest = 2. Reduce nums[4] to 2. nums = [1,1,2,2,2].
+2. largest = 2 at index 2. nextLargest = 1. Reduce nums[2] to 1. nums = [1,1,1,2,2].
+3. largest = 2 at index 3. nextLargest = 1. Reduce nums[3] to 1. nums = [1,1,1,1,2].
+4. largest = 2 at index 4. nextLargest = 1. Reduce nums[4] to 1. nums = [1,1,1,1,1].
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 5 * 104
  • +
  • 1 <= nums[i] <= 5 * 104
  • +
+
\ No newline at end of file From 895249de0ac1295460da3e7de5614c0b26856041 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 19 Nov 2023 07:51:15 +0530 Subject: [PATCH 0388/1399] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1887-reduction-operations-to-make-the-array-elements-equal/NOTES.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1887-reduction-operations-to-make-the-array-elements-equal/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1887-reduction-operations-to-make-the-array-elements-equal/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1887-reduction-operations-to-make-the-array-elements-equal/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From deff160a5fa2fa1a758c7ebfa91bc0cee4a90d7a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 19 Nov 2023 07:51:19 +0530 Subject: [PATCH 0389/1399] Time: 330 ms (31.90%), Space: 132.1 MB (9.20%) - LeetHub --- ...tions-to-make-the-array-elements-equal.cpp | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1887-reduction-operations-to-make-the-array-elements-equal/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1887-reduction-operations-to-make-the-array-elements-equal.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1887-reduction-operations-to-make-the-array-elements-equal/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1887-reduction-operations-to-make-the-array-elements-equal.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1887-reduction-operations-to-make-the-array-elements-equal/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1887-reduction-operations-to-make-the-array-elements-equal.cpp new file mode 100644 index 00000000..2c986caa --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1887-reduction-operations-to-make-the-array-elements-equal/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1887-reduction-operations-to-make-the-array-elements-equal.cpp @@ -0,0 +1,30 @@ +class Solution { +public: + int reductionOperations(vector& nums) { + + int n = nums.size(); + + unordered_map mp; + + for(auto& itr : nums) + { + ++mp[itr]; + } + + vector> vp; + + copy(mp.begin(),mp.end(), back_inserter(vp)); + + sort(vp.rbegin(),vp.rend()); + + int ans = 0; + + for(int i = 0; i < vp.size()-1; ++i) + { + ans += vp[i].second; + vp[i+1].second += vp[i].second; + } + + return ans; + } +}; \ No newline at end of file From 8c4f40dccf45de485736c9a017136543b0bc5267 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 19 Nov 2023 07:57:08 +0530 Subject: [PATCH 0390/1399] Attach NOTES - LeetHub From 18d29835f55aa26d2edfe9784716cb4e979dcdf0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 19 Nov 2023 07:57:11 +0530 Subject: [PATCH 0391/1399] Time: 163 ms (60.43%), Space: 83.2 MB (59.20%) - LeetHub --- ...tions-to-make-the-array-elements-equal.cpp | 26 ++++++------------- 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1887-reduction-operations-to-make-the-array-elements-equal/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1887-reduction-operations-to-make-the-array-elements-equal.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1887-reduction-operations-to-make-the-array-elements-equal/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1887-reduction-operations-to-make-the-array-elements-equal.cpp index 2c986caa..894f7ffb 100644 --- a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1887-reduction-operations-to-make-the-array-elements-equal/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1887-reduction-operations-to-make-the-array-elements-equal.cpp +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1887-reduction-operations-to-make-the-array-elements-equal/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1887-reduction-operations-to-make-the-array-elements-equal.cpp @@ -2,29 +2,19 @@ class Solution { public: int reductionOperations(vector& nums) { - int n = nums.size(); + int n = nums.size(), ans = 0; - unordered_map mp; + sort(nums.begin(), nums.end()); - for(auto& itr : nums) + for(int i = n-1; i > 0; --i) { - ++mp[itr]; - } - - vector> vp; - - copy(mp.begin(),mp.end(), back_inserter(vp)); - - sort(vp.rbegin(),vp.rend()); - - int ans = 0; - - for(int i = 0; i < vp.size()-1; ++i) - { - ans += vp[i].second; - vp[i+1].second += vp[i].second; + if(nums[i] != nums[i-1]) + { + ans += (n - i); + } } return ans; + } }; \ No newline at end of file From bd0ac2be25aae23a3c797c8a4f60dca482bdb068 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 19 Nov 2023 07:59:43 +0530 Subject: [PATCH 0392/1399] Create README - LeetHub --- .../README.md | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 1887-reduction-operations-to-make-the-array-elements-equal/README.md diff --git a/1887-reduction-operations-to-make-the-array-elements-equal/README.md b/1887-reduction-operations-to-make-the-array-elements-equal/README.md new file mode 100644 index 00000000..a2eb1b38 --- /dev/null +++ b/1887-reduction-operations-to-make-the-array-elements-equal/README.md @@ -0,0 +1,47 @@ +

1887. Reduction Operations to Make the Array Elements Equal

Medium


Given an integer array nums, your goal is to make all elements in nums equal. To complete one operation, follow these steps:

+ +
    +
  1. Find the largest value in nums. Let its index be i (0-indexed) and its value be largest. If there are multiple elements with the largest value, pick the smallest i.
  2. +
  3. Find the next largest value in nums strictly smaller than largest. Let its value be nextLargest.
  4. +
  5. Reduce nums[i] to nextLargest.
  6. +
+ +

Return the number of operations to make all elements in nums equal.

+ +

 

+

Example 1:

+ +
Input: nums = [5,1,3]
+Output: 3
+Explanation: It takes 3 operations to make all elements in nums equal:
+1. largest = 5 at index 0. nextLargest = 3. Reduce nums[0] to 3. nums = [3,1,3].
+2. largest = 3 at index 0. nextLargest = 1. Reduce nums[0] to 1. nums = [1,1,3].
+3. largest = 3 at index 2. nextLargest = 1. Reduce nums[2] to 1. nums = [1,1,1].
+
+ +

Example 2:

+ +
Input: nums = [1,1,1]
+Output: 0
+Explanation: All elements in nums are already equal.
+
+ +

Example 3:

+ +
Input: nums = [1,1,2,2,3]
+Output: 4
+Explanation: It takes 4 operations to make all elements in nums equal:
+1. largest = 3 at index 4. nextLargest = 2. Reduce nums[4] to 2. nums = [1,1,2,2,2].
+2. largest = 2 at index 2. nextLargest = 1. Reduce nums[2] to 1. nums = [1,1,1,2,2].
+3. largest = 2 at index 3. nextLargest = 1. Reduce nums[3] to 1. nums = [1,1,1,1,2].
+4. largest = 2 at index 4. nextLargest = 1. Reduce nums[4] to 1. nums = [1,1,1,1,1].
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 5 * 104
  • +
  • 1 <= nums[i] <= 5 * 104
  • +
+
\ No newline at end of file From e204f4946e25812294d26169d264c7db004241fa Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 19 Nov 2023 07:59:46 +0530 Subject: [PATCH 0393/1399] Time: 163 ms (60.43%), Space: 83.2 MB (59.20%) - LeetHub --- ...tions-to-make-the-array-elements-equal.cpp | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 1887-reduction-operations-to-make-the-array-elements-equal/1887-reduction-operations-to-make-the-array-elements-equal.cpp diff --git a/1887-reduction-operations-to-make-the-array-elements-equal/1887-reduction-operations-to-make-the-array-elements-equal.cpp b/1887-reduction-operations-to-make-the-array-elements-equal/1887-reduction-operations-to-make-the-array-elements-equal.cpp new file mode 100644 index 00000000..894f7ffb --- /dev/null +++ b/1887-reduction-operations-to-make-the-array-elements-equal/1887-reduction-operations-to-make-the-array-elements-equal.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + int reductionOperations(vector& nums) { + + int n = nums.size(), ans = 0; + + sort(nums.begin(), nums.end()); + + for(int i = n-1; i > 0; --i) + { + if(nums[i] != nums[i-1]) + { + ans += (n - i); + } + } + + return ans; + + } +}; \ No newline at end of file From f69004082831643272da3ba20602bfa20a9623f7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 20 Nov 2023 08:56:53 +0530 Subject: [PATCH 0394/1399] Create README - LeetHub --- .../README.md | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2391-minimum-amount-of-time-to-collect-garbage/README.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2391-minimum-amount-of-time-to-collect-garbage/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2391-minimum-amount-of-time-to-collect-garbage/README.md new file mode 100644 index 00000000..b4706728 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2391-minimum-amount-of-time-to-collect-garbage/README.md @@ -0,0 +1,56 @@ +

No companies found for this problem
2391. Minimum Amount of Time to Collect Garbage

Medium


You are given a 0-indexed array of strings garbage where garbage[i] represents the assortment of garbage at the ith house. garbage[i] consists only of the characters 'M', 'P' and 'G' representing one unit of metal, paper and glass garbage respectively. Picking up one unit of any type of garbage takes 1 minute.

+ +

You are also given a 0-indexed integer array travel where travel[i] is the number of minutes needed to go from house i to house i + 1.

+ +

There are three garbage trucks in the city, each responsible for picking up one type of garbage. Each garbage truck starts at house 0 and must visit each house in order; however, they do not need to visit every house.

+ +

Only one garbage truck may be used at any given moment. While one truck is driving or picking up garbage, the other two trucks cannot do anything.

+ +

Return the minimum number of minutes needed to pick up all the garbage.

+ +

 

+

Example 1:

+ +
Input: garbage = ["G","P","GP","GG"], travel = [2,4,3]
+Output: 21
+Explanation:
+The paper garbage truck:
+1. Travels from house 0 to house 1
+2. Collects the paper garbage at house 1
+3. Travels from house 1 to house 2
+4. Collects the paper garbage at house 2
+Altogether, it takes 8 minutes to pick up all the paper garbage.
+The glass garbage truck:
+1. Collects the glass garbage at house 0
+2. Travels from house 0 to house 1
+3. Travels from house 1 to house 2
+4. Collects the glass garbage at house 2
+5. Travels from house 2 to house 3
+6. Collects the glass garbage at house 3
+Altogether, it takes 13 minutes to pick up all the glass garbage.
+Since there is no metal garbage, we do not need to consider the metal garbage truck.
+Therefore, it takes a total of 8 + 13 = 21 minutes to collect all the garbage.
+
+ +

Example 2:

+ +
Input: garbage = ["MMM","PGM","GP"], travel = [3,10]
+Output: 37
+Explanation:
+The metal garbage truck takes 7 minutes to pick up all the metal garbage.
+The paper garbage truck takes 15 minutes to pick up all the paper garbage.
+The glass garbage truck takes 15 minutes to pick up all the glass garbage.
+It takes a total of 7 + 15 + 15 = 37 minutes to collect all the garbage.
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= garbage.length <= 105
  • +
  • garbage[i] consists of only the letters 'M', 'P', and 'G'.
  • +
  • 1 <= garbage[i].length <= 10
  • +
  • travel.length == garbage.length - 1
  • +
  • 1 <= travel[i] <= 100
  • +
+
\ No newline at end of file From a309d30433059d34f37c514f0dcdad2d4df5761e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 20 Nov 2023 08:56:57 +0530 Subject: [PATCH 0395/1399] Time: 231 ms (7.05%), Space: 102.5 MB (29.91%) - LeetHub --- ...imum-amount-of-time-to-collect-garbage.cpp | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2391-minimum-amount-of-time-to-collect-garbage/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2391-minimum-amount-of-time-to-collect-garbage.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2391-minimum-amount-of-time-to-collect-garbage/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2391-minimum-amount-of-time-to-collect-garbage.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2391-minimum-amount-of-time-to-collect-garbage/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2391-minimum-amount-of-time-to-collect-garbage.cpp new file mode 100644 index 00000000..80607765 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2391-minimum-amount-of-time-to-collect-garbage/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2391-minimum-amount-of-time-to-collect-garbage.cpp @@ -0,0 +1,56 @@ +class Solution { +public: + int garbageCollection(vector& garbage, vector& travel) { + + int n = garbage.size(); + + int totalTime = 0; + + unordered_map mp; + + for(int i = 0; i < n; ++i) + { + for(auto& gar : garbage[i]) + ++mp[gar]; + } + + for(int i = 0; i < n; ++i) + { + int p = 0, g = 0, m = 0; + + for(auto& gar : garbage[i]) + { + if(gar == 'P') + ++p; + else if(gar == 'G') + ++g; + else + ++m; + } + + totalTime += (p+g+m); + + if(i-1 >= 0) + { + if(mp['P']) totalTime += travel[i-1]; + if(mp['G']) totalTime += travel[i-1]; + if(mp['M']) totalTime += travel[i-1]; + } + + mp['P'] -= p; + mp['M'] -= m; + mp['G'] -= g; + + if(mp['P'] == 0) + mp.erase('P'); + if(mp['G'] == 0) + mp.erase('G'); + if(mp['M'] == 0) + mp.erase('M'); + + } + + return totalTime; + + } +}; \ No newline at end of file From 76be25c36e68ec8094c3cf32e83c48e846b922e7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 20 Nov 2023 08:59:52 +0530 Subject: [PATCH 0396/1399] Create README - LeetHub --- .../README.md | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 2391-minimum-amount-of-time-to-collect-garbage/README.md diff --git a/2391-minimum-amount-of-time-to-collect-garbage/README.md b/2391-minimum-amount-of-time-to-collect-garbage/README.md new file mode 100644 index 00000000..8a44ad8c --- /dev/null +++ b/2391-minimum-amount-of-time-to-collect-garbage/README.md @@ -0,0 +1,56 @@ +

2391. Minimum Amount of Time to Collect Garbage

Medium


You are given a 0-indexed array of strings garbage where garbage[i] represents the assortment of garbage at the ith house. garbage[i] consists only of the characters 'M', 'P' and 'G' representing one unit of metal, paper and glass garbage respectively. Picking up one unit of any type of garbage takes 1 minute.

+ +

You are also given a 0-indexed integer array travel where travel[i] is the number of minutes needed to go from house i to house i + 1.

+ +

There are three garbage trucks in the city, each responsible for picking up one type of garbage. Each garbage truck starts at house 0 and must visit each house in order; however, they do not need to visit every house.

+ +

Only one garbage truck may be used at any given moment. While one truck is driving or picking up garbage, the other two trucks cannot do anything.

+ +

Return the minimum number of minutes needed to pick up all the garbage.

+ +

 

+

Example 1:

+ +
Input: garbage = ["G","P","GP","GG"], travel = [2,4,3]
+Output: 21
+Explanation:
+The paper garbage truck:
+1. Travels from house 0 to house 1
+2. Collects the paper garbage at house 1
+3. Travels from house 1 to house 2
+4. Collects the paper garbage at house 2
+Altogether, it takes 8 minutes to pick up all the paper garbage.
+The glass garbage truck:
+1. Collects the glass garbage at house 0
+2. Travels from house 0 to house 1
+3. Travels from house 1 to house 2
+4. Collects the glass garbage at house 2
+5. Travels from house 2 to house 3
+6. Collects the glass garbage at house 3
+Altogether, it takes 13 minutes to pick up all the glass garbage.
+Since there is no metal garbage, we do not need to consider the metal garbage truck.
+Therefore, it takes a total of 8 + 13 = 21 minutes to collect all the garbage.
+
+ +

Example 2:

+ +
Input: garbage = ["MMM","PGM","GP"], travel = [3,10]
+Output: 37
+Explanation:
+The metal garbage truck takes 7 minutes to pick up all the metal garbage.
+The paper garbage truck takes 15 minutes to pick up all the paper garbage.
+The glass garbage truck takes 15 minutes to pick up all the glass garbage.
+It takes a total of 7 + 15 + 15 = 37 minutes to collect all the garbage.
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= garbage.length <= 105
  • +
  • garbage[i] consists of only the letters 'M', 'P', and 'G'.
  • +
  • 1 <= garbage[i].length <= 10
  • +
  • travel.length == garbage.length - 1
  • +
  • 1 <= travel[i] <= 100
  • +
+
\ No newline at end of file From 2327ca4de674fb59b2ee47b08ff20fbd00184fa0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 20 Nov 2023 08:59:52 +0530 Subject: [PATCH 0397/1399] Attach NOTES - LeetHub --- 2391-minimum-amount-of-time-to-collect-garbage/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2391-minimum-amount-of-time-to-collect-garbage/NOTES.md diff --git a/2391-minimum-amount-of-time-to-collect-garbage/NOTES.md b/2391-minimum-amount-of-time-to-collect-garbage/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2391-minimum-amount-of-time-to-collect-garbage/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From b52ef92cfd05183e34f76ce434540e4af8bc606e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 20 Nov 2023 08:59:55 +0530 Subject: [PATCH 0398/1399] Time: 231 ms (7.05%), Space: 102.5 MB (29.91%) - LeetHub --- ...imum-amount-of-time-to-collect-garbage.cpp | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 2391-minimum-amount-of-time-to-collect-garbage/2391-minimum-amount-of-time-to-collect-garbage.cpp diff --git a/2391-minimum-amount-of-time-to-collect-garbage/2391-minimum-amount-of-time-to-collect-garbage.cpp b/2391-minimum-amount-of-time-to-collect-garbage/2391-minimum-amount-of-time-to-collect-garbage.cpp new file mode 100644 index 00000000..80607765 --- /dev/null +++ b/2391-minimum-amount-of-time-to-collect-garbage/2391-minimum-amount-of-time-to-collect-garbage.cpp @@ -0,0 +1,56 @@ +class Solution { +public: + int garbageCollection(vector& garbage, vector& travel) { + + int n = garbage.size(); + + int totalTime = 0; + + unordered_map mp; + + for(int i = 0; i < n; ++i) + { + for(auto& gar : garbage[i]) + ++mp[gar]; + } + + for(int i = 0; i < n; ++i) + { + int p = 0, g = 0, m = 0; + + for(auto& gar : garbage[i]) + { + if(gar == 'P') + ++p; + else if(gar == 'G') + ++g; + else + ++m; + } + + totalTime += (p+g+m); + + if(i-1 >= 0) + { + if(mp['P']) totalTime += travel[i-1]; + if(mp['G']) totalTime += travel[i-1]; + if(mp['M']) totalTime += travel[i-1]; + } + + mp['P'] -= p; + mp['M'] -= m; + mp['G'] -= g; + + if(mp['P'] == 0) + mp.erase('P'); + if(mp['G'] == 0) + mp.erase('G'); + if(mp['M'] == 0) + mp.erase('M'); + + } + + return totalTime; + + } +}; \ No newline at end of file From 428ba85347150cd206adabb60597f0909fcd0436 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 25 Nov 2023 13:37:54 +0530 Subject: [PATCH 0399/1399] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1685-sum-of-absolute-differences-in-a-sorted-array/NOTES.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1685-sum-of-absolute-differences-in-a-sorted-array/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1685-sum-of-absolute-differences-in-a-sorted-array/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1685-sum-of-absolute-differences-in-a-sorted-array/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 2cbec1e71aae9b328b3ad81f79a28a5e4ee30feb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 25 Nov 2023 13:37:57 +0530 Subject: [PATCH 0400/1399] Time: 87 ms (79.73%), Space: 90 MB (16.49%) - LeetHub --- ...absolute-differences-in-a-sorted-array.cpp | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1685-sum-of-absolute-differences-in-a-sorted-array/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1685-sum-of-absolute-differences-in-a-sorted-array.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1685-sum-of-absolute-differences-in-a-sorted-array/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1685-sum-of-absolute-differences-in-a-sorted-array.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1685-sum-of-absolute-differences-in-a-sorted-array/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1685-sum-of-absolute-differences-in-a-sorted-array.cpp new file mode 100644 index 00000000..e25a2d5b --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1685-sum-of-absolute-differences-in-a-sorted-array/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1685-sum-of-absolute-differences-in-a-sorted-array.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + vector getSumAbsoluteDifferences(vector& nums) { + + int n = nums.size(); + + vector pref(n+1,0); + + for(int i = 1; i <= n; ++i) + pref[i] = pref[i-1] + nums[i-1]; + + vector ans; + + for(int i = 1; i <= n; ++i) + { + long long leftSum = nums[i-1] * 1LL * i; + long long left = leftSum - pref[i]; + + long long rightSum = nums[i-1] * 1LL * (n-i); + long long right = pref[n] - pref[i] - rightSum; + + ans.push_back(left + right); + } + + return ans; + } +}; \ No newline at end of file From 58efefd8ed77313028707c5415187c20e3e672c2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 25 Nov 2023 13:41:24 +0530 Subject: [PATCH 0401/1399] Attach NOTES - LeetHub --- 1685-sum-of-absolute-differences-in-a-sorted-array/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1685-sum-of-absolute-differences-in-a-sorted-array/NOTES.md diff --git a/1685-sum-of-absolute-differences-in-a-sorted-array/NOTES.md b/1685-sum-of-absolute-differences-in-a-sorted-array/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1685-sum-of-absolute-differences-in-a-sorted-array/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 260e7aa102069bc9b74aef5e317cac405921514d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 25 Nov 2023 13:41:28 +0530 Subject: [PATCH 0402/1399] Time: 87 ms (79.73%), Space: 90 MB (16.49%) - LeetHub --- ...absolute-differences-in-a-sorted-array.cpp | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 1685-sum-of-absolute-differences-in-a-sorted-array/1685-sum-of-absolute-differences-in-a-sorted-array.cpp diff --git a/1685-sum-of-absolute-differences-in-a-sorted-array/1685-sum-of-absolute-differences-in-a-sorted-array.cpp b/1685-sum-of-absolute-differences-in-a-sorted-array/1685-sum-of-absolute-differences-in-a-sorted-array.cpp new file mode 100644 index 00000000..e25a2d5b --- /dev/null +++ b/1685-sum-of-absolute-differences-in-a-sorted-array/1685-sum-of-absolute-differences-in-a-sorted-array.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + vector getSumAbsoluteDifferences(vector& nums) { + + int n = nums.size(); + + vector pref(n+1,0); + + for(int i = 1; i <= n; ++i) + pref[i] = pref[i-1] + nums[i-1]; + + vector ans; + + for(int i = 1; i <= n; ++i) + { + long long leftSum = nums[i-1] * 1LL * i; + long long left = leftSum - pref[i]; + + long long rightSum = nums[i-1] * 1LL * (n-i); + long long right = pref[n] - pref[i] - rightSum; + + ans.push_back(left + right); + } + + return ans; + } +}; \ No newline at end of file From fccb090cd1687e5e4acd73f0a5402a66533b5ad6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 25 Nov 2023 13:42:29 +0530 Subject: [PATCH 0403/1399] Attach NOTES - LeetHub From 8d2d63aa43b5b90cd5ac9a8cd4ea288545131061 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 25 Nov 2023 13:42:32 +0530 Subject: [PATCH 0404/1399] Time: 87 ms (79.73%), Space: 90 MB (16.49%) - LeetHub From 33f015a87128e320f07bf5ff7ac2239a77a77ce2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 25 Nov 2023 13:42:51 +0530 Subject: [PATCH 0405/1399] Attach NOTES - LeetHub From 07b544a7b3cedde6522699ffbf0ceaf7a5812118 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 25 Nov 2023 13:42:55 +0530 Subject: [PATCH 0406/1399] Time: 87 ms (79.73%), Space: 90 MB (16.49%) - LeetHub From 7247690fcae53aa80f543349f403d78618163b30 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 26 Nov 2023 22:28:25 +0530 Subject: [PATCH 0407/1399] Create README - LeetHub --- .../README.md | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1727-largest-submatrix-with-rearrangements/README.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1727-largest-submatrix-with-rearrangements/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1727-largest-submatrix-with-rearrangements/README.md new file mode 100644 index 00000000..258022d0 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1727-largest-submatrix-with-rearrangements/README.md @@ -0,0 +1,38 @@ +

Google
1
1727. Largest Submatrix With Rearrangements

Medium


You are given a binary matrix matrix of size m x n, and you are allowed to rearrange the columns of the matrix in any order.

+ +

Return the area of the largest submatrix within matrix where every element of the submatrix is 1 after reordering the columns optimally.

+ +

 

+

Example 1:

+ +
Input: matrix = [[0,0,1],[1,1,1],[1,0,1]]
+Output: 4
+Explanation: You can rearrange the columns as shown above.
+The largest submatrix of 1s, in bold, has an area of 4.
+
+ +

Example 2:

+ +
Input: matrix = [[1,0,1,0,1]]
+Output: 3
+Explanation: You can rearrange the columns as shown above.
+The largest submatrix of 1s, in bold, has an area of 3.
+
+ +

Example 3:

+ +
Input: matrix = [[1,1,0],[1,0,1]]
+Output: 2
+Explanation: Notice that you must rearrange entire columns, and there is no way to make a submatrix of 1s larger than an area of 2.
+
+ +

 

+

Constraints:

+ +
    +
  • m == matrix.length
  • +
  • n == matrix[i].length
  • +
  • 1 <= m * n <= 105
  • +
  • matrix[i][j] is either 0 or 1.
  • +
+
\ No newline at end of file From 4ff0896b444b756b0ceacd74a54e87b727fe8f0b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 26 Nov 2023 22:28:26 +0530 Subject: [PATCH 0408/1399] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1727-largest-submatrix-with-rearrangements/NOTES.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1727-largest-submatrix-with-rearrangements/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1727-largest-submatrix-with-rearrangements/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1727-largest-submatrix-with-rearrangements/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 94fe42e83825b928bfe7b75217d863538030f143 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 26 Nov 2023 22:28:28 +0530 Subject: [PATCH 0409/1399] Time: 155 ms (65.34%), Space: 66.2 MB (96.02%) - LeetHub --- ...-largest-submatrix-with-rearrangements.cpp | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1727-largest-submatrix-with-rearrangements/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1727-largest-submatrix-with-rearrangements.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1727-largest-submatrix-with-rearrangements/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1727-largest-submatrix-with-rearrangements.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1727-largest-submatrix-with-rearrangements/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1727-largest-submatrix-with-rearrangements.cpp new file mode 100644 index 00000000..3eed51f2 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1727-largest-submatrix-with-rearrangements/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1727-largest-submatrix-with-rearrangements.cpp @@ -0,0 +1,28 @@ +class Solution { +public: + int largestSubmatrix(vector>& matrix) { + + int i, j, ans = 0, n = matrix.size(), m = matrix[0].size(); + + for(i = 0; i < m; i++) + { + for(j = 1; j < n; j++) + { + if(matrix[j][i] == 1) + matrix[j][i] = matrix[j-1][i] + matrix[j][i]; + else + matrix[j][i] = 0; + } + } + + for(i = 0; i < n; i++) + { + sort(matrix[i].begin(), matrix[i].end(), greater()); + + for(j = 0; j < m; j++) + ans = max(ans, matrix[i][j] * (j + 1)); + } + + return ans; + } +}; \ No newline at end of file From 32bcc108d11b4cc4a833e4d93315db4417b60b6f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 26 Nov 2023 22:30:22 +0530 Subject: [PATCH 0410/1399] Create README - LeetHub --- .../README.md | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 1727-largest-submatrix-with-rearrangements/README.md diff --git a/1727-largest-submatrix-with-rearrangements/README.md b/1727-largest-submatrix-with-rearrangements/README.md new file mode 100644 index 00000000..e30720f4 --- /dev/null +++ b/1727-largest-submatrix-with-rearrangements/README.md @@ -0,0 +1,38 @@ +

1727. Largest Submatrix With Rearrangements

Medium


You are given a binary matrix matrix of size m x n, and you are allowed to rearrange the columns of the matrix in any order.

+ +

Return the area of the largest submatrix within matrix where every element of the submatrix is 1 after reordering the columns optimally.

+ +

 

+

Example 1:

+ +
Input: matrix = [[0,0,1],[1,1,1],[1,0,1]]
+Output: 4
+Explanation: You can rearrange the columns as shown above.
+The largest submatrix of 1s, in bold, has an area of 4.
+
+ +

Example 2:

+ +
Input: matrix = [[1,0,1,0,1]]
+Output: 3
+Explanation: You can rearrange the columns as shown above.
+The largest submatrix of 1s, in bold, has an area of 3.
+
+ +

Example 3:

+ +
Input: matrix = [[1,1,0],[1,0,1]]
+Output: 2
+Explanation: Notice that you must rearrange entire columns, and there is no way to make a submatrix of 1s larger than an area of 2.
+
+ +

 

+

Constraints:

+ +
    +
  • m == matrix.length
  • +
  • n == matrix[i].length
  • +
  • 1 <= m * n <= 105
  • +
  • matrix[i][j] is either 0 or 1.
  • +
+
\ No newline at end of file From 77ae3d992cdb6bd04e61646406eba3f84e44f378 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 26 Nov 2023 22:30:22 +0530 Subject: [PATCH 0411/1399] Attach NOTES - LeetHub --- 1727-largest-submatrix-with-rearrangements/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1727-largest-submatrix-with-rearrangements/NOTES.md diff --git a/1727-largest-submatrix-with-rearrangements/NOTES.md b/1727-largest-submatrix-with-rearrangements/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1727-largest-submatrix-with-rearrangements/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 19e3fd849552c3ef487b14b738e2b0ecb84c5259 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 26 Nov 2023 22:30:26 +0530 Subject: [PATCH 0412/1399] Time: 155 ms (65.34%), Space: 66.2 MB (96.02%) - LeetHub --- ...-largest-submatrix-with-rearrangements.cpp | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 1727-largest-submatrix-with-rearrangements/1727-largest-submatrix-with-rearrangements.cpp diff --git a/1727-largest-submatrix-with-rearrangements/1727-largest-submatrix-with-rearrangements.cpp b/1727-largest-submatrix-with-rearrangements/1727-largest-submatrix-with-rearrangements.cpp new file mode 100644 index 00000000..3eed51f2 --- /dev/null +++ b/1727-largest-submatrix-with-rearrangements/1727-largest-submatrix-with-rearrangements.cpp @@ -0,0 +1,28 @@ +class Solution { +public: + int largestSubmatrix(vector>& matrix) { + + int i, j, ans = 0, n = matrix.size(), m = matrix[0].size(); + + for(i = 0; i < m; i++) + { + for(j = 1; j < n; j++) + { + if(matrix[j][i] == 1) + matrix[j][i] = matrix[j-1][i] + matrix[j][i]; + else + matrix[j][i] = 0; + } + } + + for(i = 0; i < n; i++) + { + sort(matrix[i].begin(), matrix[i].end(), greater()); + + for(j = 0; j < m; j++) + ans = max(ans, matrix[i][j] * (j + 1)); + } + + return ans; + } +}; \ No newline at end of file From a755b623949632d0e55c20c4f3dd74c1a30fb129 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 26 Nov 2023 22:30:39 +0530 Subject: [PATCH 0413/1399] Attach NOTES - LeetHub From a1fdb924630a6280b594b896f5616184d0cfbc0a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 26 Nov 2023 22:30:43 +0530 Subject: [PATCH 0414/1399] Time: 155 ms (65.34%), Space: 66.2 MB (96.02%) - LeetHub From 10e9bdf501148dc3e9de58e46cc0c048833c20e9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 27 Nov 2023 13:52:59 +0530 Subject: [PATCH 0415/1399] Attach NOTES - LeetHub From 7b8f250d221c6eb743274251b6c501bb5f783da4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 27 Nov 2023 13:53:01 +0530 Subject: [PATCH 0416/1399] Time: 145 ms (85.55%), Space: 66.2 MB (95.95%) - LeetHub --- ...-largest-submatrix-with-rearrangements.cpp | 30 +++++++++++-------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1727-largest-submatrix-with-rearrangements/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1727-largest-submatrix-with-rearrangements.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1727-largest-submatrix-with-rearrangements/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1727-largest-submatrix-with-rearrangements.cpp index 3eed51f2..9183417e 100644 --- a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1727-largest-submatrix-with-rearrangements/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1727-largest-submatrix-with-rearrangements.cpp +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1727-largest-submatrix-with-rearrangements/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1727-largest-submatrix-with-rearrangements.cpp @@ -2,27 +2,33 @@ class Solution { public: int largestSubmatrix(vector>& matrix) { - int i, j, ans = 0, n = matrix.size(), m = matrix[0].size(); + int n = matrix.size(); + int m = matrix[0].size(); - for(i = 0; i < m; i++) + for(int i = 1; i < n; ++i) { - for(j = 1; j < n; j++) + for(int j = 0; j < m; ++j) { - if(matrix[j][i] == 1) - matrix[j][i] = matrix[j-1][i] + matrix[j][i]; + if(matrix[i][j] == 1) + matrix[i][j] += matrix[i-1][j]; else - matrix[j][i] = 0; + matrix[i][j] = 0; } } - - for(i = 0; i < n; i++) + + int ans = 0; + + for(int i = 0; i < n; ++i) { - sort(matrix[i].begin(), matrix[i].end(), greater()); - - for(j = 0; j < m; j++) - ans = max(ans, matrix[i][j] * (j + 1)); + sort(matrix[i].begin(), matrix[i].end()); + + for(int j = 0; j < m; ++j) + { + ans = max(ans, matrix[i][j] * (m-j)); + } } return ans; + } }; \ No newline at end of file From ef29064f886c44c857debf5d6691a2dd4f60cccc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 27 Nov 2023 18:32:14 +0530 Subject: [PATCH 0417/1399] Attach NOTES - LeetHub From f4f900dae97433ad22ea8b11f9edf45d2b20d9b1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 27 Nov 2023 18:32:17 +0530 Subject: [PATCH 0418/1399] Time: 198 ms (8.09%), Space: 96.1 MB (5.20%) - LeetHub --- ...-largest-submatrix-with-rearrangements.cpp | 43 ++++++++++++------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1727-largest-submatrix-with-rearrangements/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1727-largest-submatrix-with-rearrangements.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1727-largest-submatrix-with-rearrangements/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1727-largest-submatrix-with-rearrangements.cpp index 9183417e..de864305 100644 --- a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1727-largest-submatrix-with-rearrangements/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1727-largest-submatrix-with-rearrangements.cpp +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1727-largest-submatrix-with-rearrangements/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1727-largest-submatrix-with-rearrangements.cpp @@ -5,30 +5,41 @@ class Solution { int n = matrix.size(); int m = matrix[0].size(); - for(int i = 1; i < n; ++i) - { - for(int j = 0; j < m; ++j) - { - if(matrix[i][j] == 1) - matrix[i][j] += matrix[i-1][j]; - else - matrix[i][j] = 0; - } - } - int ans = 0; - for(int i = 0; i < n; ++i) + vector> prevHeights; + + for(int row = 0; row < n; ++row) { - sort(matrix[i].begin(), matrix[i].end()); + vector> currHeights; + vector visited(m, false); - for(int j = 0; j < m; ++j) + for(auto& [height, col] : prevHeights) { - ans = max(ans, matrix[i][j] * (m-j)); + if(matrix[row][col] == 1) + { + visited[col] = true; + currHeights.push_back({height+1, col}); + } } + + for(int col = 0; col < m; ++col) + { + if(!visited[col] and matrix[row][col] == 1) + currHeights.push_back({1, col}); + } + + + for(int i = 0; i < currHeights.size(); ++i) + { + int height = currHeights[i].first; + int base = i+1; + ans = max(ans, height * base); + } + + prevHeights = currHeights; } return ans; - } }; \ No newline at end of file From 4a1f318b4ef4b0dc0e92a516f244cd22f5e3f0d4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 27 Nov 2023 22:56:49 +0530 Subject: [PATCH 0419/1399] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence10-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwilio-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsnapchat-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div935-knight-dialer/NOTES.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence10-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwilio-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsnapchat-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div935-knight-dialer/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence10-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwilio-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsnapchat-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div935-knight-dialer/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence10-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwilio-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsnapchat-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div935-knight-dialer/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 9866e74b0edd048c4c27d0aa96b0cd0ecbb31e64 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 27 Nov 2023 22:56:53 +0530 Subject: [PATCH 0420/1399] Time: 206 ms (28.07%), Space: 29.7 MB (52.78%) - LeetHub --- ...t-svg-div-div-div-div935-knight-dialer.cpp | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence10-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwilio-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsnapchat-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div935-knight-dialer/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence10-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwilio-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsnapchat-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div935-knight-dialer.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence10-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwilio-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsnapchat-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div935-knight-dialer/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence10-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwilio-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsnapchat-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div935-knight-dialer.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence10-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwilio-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsnapchat-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div935-knight-dialer/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence10-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwilio-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsnapchat-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div935-knight-dialer.cpp new file mode 100644 index 00000000..9fdae1a4 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence10-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwilio-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsnapchat-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div935-knight-dialer/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbloomberg-divdiv-classcompanytagscontainer-tagoccurence10-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divtwilio-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divmicrosoft-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divsnapchat-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div935-knight-dialer.cpp @@ -0,0 +1,53 @@ +class Solution { +public: + + const int mod = 1e9+7; + + vector dx = {-2, -2, -1 , -1, +2, +2, +1, +1}; + vector dy = {-1, +1, -2, +2, -1, +1, -2, +2}; + + int helper(int i, int j, int n, vector>>& dp) + { + if(n == 1) + return 1; + + if(dp[i][j][n] != -1) + return dp[i][j][n]; + + int count = 0; + + for(int k = 0; k < 8; ++k) + { + int x = dx[k] + i; + int y = dy[k] + j; + + if(x >= 0 and x < 4 and y >= 0 and y < 3) + { + if(x == 3 and (y == 0 or y == 2)) + continue; + count = (count + helper(x, y, n-1, dp)) % mod; + } + } + + return dp[i][j][n] = count; + } + + int knightDialer(int n) { + + int count = 0; + + vector>> dp(4, vector>(3, vector(n+1,-1))); + + for(int i = 0; i < 4; ++i) + { + for(int j = 0; j < 3; ++j) + { + if(i == 3 and (j == 0 or j == 2)) + continue; + count = (count + helper(i,j,n,dp)) % mod; + } + } + + return count; + } +}; \ No newline at end of file From 349a68c41af062fc02b24f59917707e96507f985 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 28 Nov 2023 20:36:43 +0530 Subject: [PATCH 0421/1399] Create README - LeetHub --- .../README.md | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2147-number-of-ways-to-divide-a-long-corridor/README.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2147-number-of-ways-to-divide-a-long-corridor/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2147-number-of-ways-to-divide-a-long-corridor/README.md new file mode 100644 index 00000000..da38196d --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2147-number-of-ways-to-divide-a-long-corridor/README.md @@ -0,0 +1,42 @@ +

No companies found for this problem
2147. Number of Ways to Divide a Long Corridor

Hard


Along a long library corridor, there is a line of seats and decorative plants. You are given a 0-indexed string corridor of length n consisting of letters 'S' and 'P' where each 'S' represents a seat and each 'P' represents a plant.

+ +

One room divider has already been installed to the left of index 0, and another to the right of index n - 1. Additional room dividers can be installed. For each position between indices i - 1 and i (1 <= i <= n - 1), at most one divider can be installed.

+ +

Divide the corridor into non-overlapping sections, where each section has exactly two seats with any number of plants. There may be multiple ways to perform the division. Two ways are different if there is a position with a room divider installed in the first way but not in the second way.

+ +

Return the number of ways to divide the corridor. Since the answer may be very large, return it modulo 109 + 7. If there is no way, return 0.

+ +

 

+

Example 1:

+ +
Input: corridor = "SSPPSPS"
+Output: 3
+Explanation: There are 3 different ways to divide the corridor.
+The black bars in the above image indicate the two room dividers already installed.
+Note that in each of the ways, each section has exactly two seats.
+
+ +

Example 2:

+ +
Input: corridor = "PPSPSP"
+Output: 1
+Explanation: There is only 1 way to divide the corridor, by not installing any additional dividers.
+Installing any would create some section that does not have exactly two seats.
+
+ +

Example 3:

+ +
Input: corridor = "S"
+Output: 0
+Explanation: There is no way to divide the corridor because there will always be a section that does not have exactly two seats.
+
+ +

 

+

Constraints:

+ +
    +
  • n == corridor.length
  • +
  • 1 <= n <= 105
  • +
  • corridor[i] is either 'S' or 'P'.
  • +
+
\ No newline at end of file From e8af40691804e88d04934c663ae31e3ecb69a277 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 28 Nov 2023 20:36:47 +0530 Subject: [PATCH 0422/1399] Time: 1211 ms (6.86%), Space: 404.8 MB (5.71%) - LeetHub --- ...mber-of-ways-to-divide-a-long-corridor.cpp | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2147-number-of-ways-to-divide-a-long-corridor/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2147-number-of-ways-to-divide-a-long-corridor.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2147-number-of-ways-to-divide-a-long-corridor/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2147-number-of-ways-to-divide-a-long-corridor.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2147-number-of-ways-to-divide-a-long-corridor/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2147-number-of-ways-to-divide-a-long-corridor.cpp new file mode 100644 index 00000000..ca35c3d4 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2147-number-of-ways-to-divide-a-long-corridor/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2147-number-of-ways-to-divide-a-long-corridor.cpp @@ -0,0 +1,45 @@ +class Solution { +public: + + const int mod = 1e9+7; + + int helper(int idx, int cnt, int n, string& corridor, vector>& dp) + { + if(cnt > 2) + return 0; + + if(idx == n) + { + return (cnt == 2); + } + + if(dp[idx][cnt] != -1) + return dp[idx][cnt]; + + int ans = 0; + + if(corridor[idx] == 'S') + { + ans = (ans + helper(idx+1, cnt+1, n, corridor, dp)) % mod; + if(cnt == 2) + ans = (ans + helper(idx+1, 1, n, corridor, dp)) % mod; + } + else + { + ans = (ans + helper(idx+1, cnt, n, corridor, dp)) % mod; + if(cnt == 2) + ans = (ans + helper(idx+1, 0, n, corridor, dp)) % mod; + } + return dp[idx][cnt] = ans; + } + + int numberOfWays(string corridor) { + + int n = corridor.size(); + + vector> dp(n, vector(3, -1)); + + return helper(0, 0, n, corridor, dp); + + } +}; \ No newline at end of file From 3658cc127bcf83d952ec98b04a0d6010290c2d6e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 28 Nov 2023 22:39:16 +0530 Subject: [PATCH 0423/1399] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2147-number-of-ways-to-divide-a-long-corridor/NOTES.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2147-number-of-ways-to-divide-a-long-corridor/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2147-number-of-ways-to-divide-a-long-corridor/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2147-number-of-ways-to-divide-a-long-corridor/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From fe73c267e25b6e1bb8548df352916bb228029e15 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 28 Nov 2023 22:39:18 +0530 Subject: [PATCH 0424/1399] Time: 140 ms (50.86%), Space: 44 MB (43.43%) - LeetHub --- ...mber-of-ways-to-divide-a-long-corridor.cpp | 61 ++++++++++--------- 1 file changed, 32 insertions(+), 29 deletions(-) diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2147-number-of-ways-to-divide-a-long-corridor/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2147-number-of-ways-to-divide-a-long-corridor.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2147-number-of-ways-to-divide-a-long-corridor/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2147-number-of-ways-to-divide-a-long-corridor.cpp index ca35c3d4..89923009 100644 --- a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2147-number-of-ways-to-divide-a-long-corridor/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2147-number-of-ways-to-divide-a-long-corridor.cpp +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2147-number-of-ways-to-divide-a-long-corridor/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2147-number-of-ways-to-divide-a-long-corridor.cpp @@ -1,45 +1,48 @@ -class Solution { +class Solution { public: const int mod = 1e9+7; - int helper(int idx, int cnt, int n, string& corridor, vector>& dp) - { - if(cnt > 2) - return 0; + int numberOfWays(string corridor) { - if(idx == n) - { - return (cnt == 2); - } + int n = corridor.size(); + + int cnt = count(corridor.begin(), corridor.end(), 'S'); + + if(!cnt or cnt & 1) return 0; + + int inc = 0; - if(dp[idx][cnt] != -1) - return dp[idx][cnt]; + vector v; - int ans = 0; + int currCnt = 0; - if(corridor[idx] == 'S') + for(int i = 0; i < n; ++i) { - ans = (ans + helper(idx+1, cnt+1, n, corridor, dp)) % mod; - if(cnt == 2) - ans = (ans + helper(idx+1, 1, n, corridor, dp)) % mod; + if(!cnt) break; + + if(corridor[i] == 'S') + ++currCnt, --cnt; + if(currCnt == 3) + { + v.push_back(inc); + inc = 0; + currCnt = 1; + } + if(currCnt == 2) + (inc = !inc ? 1 : inc+1); } - else + + if(currCnt) { - ans = (ans + helper(idx+1, cnt, n, corridor, dp)) % mod; - if(cnt == 2) - ans = (ans + helper(idx+1, 0, n, corridor, dp)) % mod; + v.push_back(inc); } - return dp[idx][cnt] = ans; - } - - int numberOfWays(string corridor) { - int n = corridor.size(); - - vector> dp(n, vector(3, -1)); + long long ans = v[0]; + + for(int i = 1; i < v.size(); ++i) + ans = (ans *1LL* v[i]) % mod; - return helper(0, 0, n, corridor, dp); - + return ans; } }; \ No newline at end of file From 32001cb4232bc78dcb9b0b5c884a0ebceea8e147 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 29 Nov 2023 07:15:20 +0530 Subject: [PATCH 0425/1399] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbox-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divcisco-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div191-number-of-1-bits/NOTES.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbox-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divcisco-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div191-number-of-1-bits/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbox-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divcisco-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div191-number-of-1-bits/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbox-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divcisco-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div191-number-of-1-bits/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From cf45a03ad798980eee3549088642d9b839ccd0ac Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 29 Nov 2023 07:15:26 +0530 Subject: [PATCH 0426/1399] Time: 4 ms (30.61%), Space: 6.4 MB (57.22%) - LeetHub --- ...t-svg-div-div-div-div191-number-of-1-bits.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbox-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divcisco-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div191-number-of-1-bits/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbox-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divcisco-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div191-number-of-1-bits.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbox-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divcisco-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div191-number-of-1-bits/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbox-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divcisco-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div191-number-of-1-bits.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbox-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divcisco-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div191-number-of-1-bits/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbox-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divcisco-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div191-number-of-1-bits.cpp new file mode 100644 index 00000000..dd0d10a6 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbox-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divcisco-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div191-number-of-1-bits/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divbox-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divcisco-divdiv-classcompanytagscontainer-tagoccurence4-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence3-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divoracle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div191-number-of-1-bits.cpp @@ -0,0 +1,16 @@ +class Solution { +public: + int hammingWeight(uint32_t n) { + + int cnt = 0; + + while(n > 0) + { + if(n & 1) + ++cnt; + n >>= 1; + } + + return cnt; + } +}; \ No newline at end of file From 2ec173673770fbfb2b1bc3a1184c5417a1250462 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 30 Nov 2023 22:00:37 +0530 Subject: [PATCH 0427/1399] Create README - LeetHub --- .../README.md | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1611-minimum-one-bit-operations-to-make-integers-zero/README.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1611-minimum-one-bit-operations-to-make-integers-zero/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1611-minimum-one-bit-operations-to-make-integers-zero/README.md new file mode 100644 index 00000000..683b1133 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1611-minimum-one-bit-operations-to-make-integers-zero/README.md @@ -0,0 +1,37 @@ +

No companies found for this problem
1611. Minimum One Bit Operations to Make Integers Zero

Hard


Given an integer n, you must transform it into 0 using the following operations any number of times:

+ +
    +
  • Change the rightmost (0th) bit in the binary representation of n.
  • +
  • Change the ith bit in the binary representation of n if the (i-1)th bit is set to 1 and the (i-2)th through 0th bits are set to 0.
  • +
+ +

Return the minimum number of operations to transform n into 0.

+ +

 

+

Example 1:

+ +
Input: n = 3
+Output: 2
+Explanation: The binary representation of 3 is "11".
+"11" -> "01" with the 2nd operation since the 0th bit is 1.
+"01" -> "00" with the 1st operation.
+
+ +

Example 2:

+ +
Input: n = 6
+Output: 4
+Explanation: The binary representation of 6 is "110".
+"110" -> "010" with the 2nd operation since the 1st bit is 1 and 0th through 0th bits are 0.
+"010" -> "011" with the 1st operation.
+"011" -> "001" with the 2nd operation since the 0th bit is 1.
+"001" -> "000" with the 1st operation.
+
+ +

 

+

Constraints:

+ +
    +
  • 0 <= n <= 109
  • +
+
\ No newline at end of file From c532108faa75dd5f5765ec3f456948b5d932e50e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 30 Nov 2023 22:00:40 +0530 Subject: [PATCH 0428/1399] Time: 3 ms (45.97%), Space: 6.3 MB (80.09%) - LeetHub --- ...nimum-one-bit-operations-to-make-integers-zero.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1611-minimum-one-bit-operations-to-make-integers-zero/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1611-minimum-one-bit-operations-to-make-integers-zero.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1611-minimum-one-bit-operations-to-make-integers-zero/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1611-minimum-one-bit-operations-to-make-integers-zero.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1611-minimum-one-bit-operations-to-make-integers-zero/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1611-minimum-one-bit-operations-to-make-integers-zero.cpp new file mode 100644 index 00000000..eaad07b4 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1611-minimum-one-bit-operations-to-make-integers-zero/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1611-minimum-one-bit-operations-to-make-integers-zero.cpp @@ -0,0 +1,11 @@ +class Solution { +public: + int minimumOneBitOperations(int n) { + if (n <= 1) + return n; + int bit = 0; + while ((1 << bit) <= n) + bit++; + return ((1 << bit) - 1) - minimumOneBitOperations(n - (1 << (bit-1))); + } +}; \ No newline at end of file From a980aebb2740b56e2e7c72dd4f648978b2ee0428 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 1 Dec 2023 09:45:37 +0530 Subject: [PATCH 0429/1399] Create README - LeetHub --- .../README.md | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1662-check-if-two-string-arrays-are-equivalent/README.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1662-check-if-two-string-arrays-are-equivalent/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1662-check-if-two-string-arrays-are-equivalent/README.md new file mode 100644 index 00000000..3e4a16f2 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1662-check-if-two-string-arrays-are-equivalent/README.md @@ -0,0 +1,36 @@ +

Apple
2
Facebook
1
1662. Check If Two String Arrays are Equivalent

Easy


Given two string arrays word1 and word2, return true if the two arrays represent the same string, and false otherwise.

+ +

A string is represented by an array if the array elements concatenated in order forms the string.

+ +

 

+

Example 1:

+ +
Input: word1 = ["ab", "c"], word2 = ["a", "bc"]
+Output: true
+Explanation:
+word1 represents string "ab" + "c" -> "abc"
+word2 represents string "a" + "bc" -> "abc"
+The strings are the same, so return true.
+ +

Example 2:

+ +
Input: word1 = ["a", "cb"], word2 = ["ab", "c"]
+Output: false
+
+ +

Example 3:

+ +
Input: word1  = ["abc", "d", "defg"], word2 = ["abcddefg"]
+Output: true
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= word1.length, word2.length <= 103
  • +
  • 1 <= word1[i].length, word2[i].length <= 103
  • +
  • 1 <= sum(word1[i].length), sum(word2[i].length) <= 103
  • +
  • word1[i] and word2[i] consist of lowercase letters.
  • +
+
\ No newline at end of file From 6a38c37b87440dc90d592cbc59fef42baf403f17 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 1 Dec 2023 09:45:41 +0530 Subject: [PATCH 0430/1399] Time: 0 ms (100.00%), Space: 11.9 MB (77.65%) - LeetHub --- ...ck-if-two-string-arrays-are-equivalent.cpp | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1662-check-if-two-string-arrays-are-equivalent/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1662-check-if-two-string-arrays-are-equivalent.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1662-check-if-two-string-arrays-are-equivalent/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1662-check-if-two-string-arrays-are-equivalent.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1662-check-if-two-string-arrays-are-equivalent/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1662-check-if-two-string-arrays-are-equivalent.cpp new file mode 100644 index 00000000..08c3a3b1 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1662-check-if-two-string-arrays-are-equivalent/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divfacebook-divdiv-classcompanytagscontainer-tagoccurence1-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1662-check-if-two-string-arrays-are-equivalent.cpp @@ -0,0 +1,21 @@ +class Solution { +public: + bool arrayStringsAreEqual(vector& word1, vector& word2) { + + int i = 0, j = 0; + int n = 0, m = 0; + + while(i < word1.size() and j < word2.size()) + { + if(word1[i][n] != word2[j][m]) + return false; + + ++n, ++m; + + if(n >= word1[i].size()) n = 0, ++i; + if(m >= word2[j].size()) m = 0, ++j; + } + + return (i == word1.size() and j == word2.size()); + } +}; \ No newline at end of file From dfe2c4742af3e562239a0c0674317b325266e57e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Dec 2023 08:22:34 +0530 Subject: [PATCH 0431/1399] Create README - LeetHub --- .../README.md | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divkarat-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divindeed-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1160-find-words-that-can-be-formed-by-characters/README.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divkarat-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divindeed-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1160-find-words-that-can-be-formed-by-characters/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divkarat-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divindeed-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1160-find-words-that-can-be-formed-by-characters/README.md new file mode 100644 index 00000000..de231256 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divkarat-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divindeed-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1160-find-words-that-can-be-formed-by-characters/README.md @@ -0,0 +1,30 @@ +

Karat
12
Indeed
6
Amazon
2
Google
2
1160. Find Words That Can Be Formed by Characters

Easy


You are given an array of strings words and a string chars.

+ +

A string is good if it can be formed by characters from chars (each character can only be used once).

+ +

Return the sum of lengths of all good strings in words.

+ +

 

+

Example 1:

+ +
Input: words = ["cat","bt","hat","tree"], chars = "atach"
+Output: 6
+Explanation: The strings that can be formed are "cat" and "hat" so the answer is 3 + 3 = 6.
+
+ +

Example 2:

+ +
Input: words = ["hello","world","leetcode"], chars = "welldonehoneyr"
+Output: 10
+Explanation: The strings that can be formed are "hello" and "world" so the answer is 5 + 5 = 10.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= words.length <= 1000
  • +
  • 1 <= words[i].length, chars.length <= 100
  • +
  • words[i] and chars consist of lowercase English letters.
  • +
+
\ No newline at end of file From 8cf6dde5cf01d0a8aeed1bac333034f92b98ba0d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Dec 2023 08:22:34 +0530 Subject: [PATCH 0432/1399] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divkarat-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divindeed-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1160-find-words-that-can-be-formed-by-characters/NOTES.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divkarat-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divindeed-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1160-find-words-that-can-be-formed-by-characters/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divkarat-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divindeed-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1160-find-words-that-can-be-formed-by-characters/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divkarat-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divindeed-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1160-find-words-that-can-be-formed-by-characters/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From e4fa46a5e17cf935643778fa2550bfe997060266 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Dec 2023 08:22:38 +0530 Subject: [PATCH 0433/1399] Time: 174 ms (15.47%), Space: 49.6 MB (30.78%) - LeetHub --- ...words-that-can-be-formed-by-characters.cpp | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divkarat-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divindeed-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1160-find-words-that-can-be-formed-by-characters/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divkarat-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divindeed-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1160-find-words-that-can-be-formed-by-characters.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divkarat-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divindeed-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1160-find-words-that-can-be-formed-by-characters/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divkarat-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divindeed-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1160-find-words-that-can-be-formed-by-characters.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divkarat-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divindeed-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1160-find-words-that-can-be-formed-by-characters/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divkarat-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divindeed-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1160-find-words-that-can-be-formed-by-characters.cpp new file mode 100644 index 00000000..1b331ed1 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divkarat-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divindeed-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1160-find-words-that-can-be-formed-by-characters/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divkarat-divdiv-classcompanytagscontainer-tagoccurence12-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divindeed-divdiv-classcompanytagscontainer-tagoccurence6-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divamazon-divdiv-classcompanytagscontainer-tagoccurence2-div-divdiv-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divgoogle-divdiv-classcompanytagscontainer-tagoccurence2-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1160-find-words-that-can-be-formed-by-characters.cpp @@ -0,0 +1,39 @@ +class Solution { +public: + int countCharacters(vector& words, string chars) { + + int counter = 0; + + unordered_map mp; + + for(auto& ch : chars) + ++mp[ch]; + + for(auto& word : words) + { + bool canFormed = true; + + unordered_map freq; + + for(auto& ch : word) + { + ++freq[ch]; + } + + for(auto& ch : word) + { + if(freq[ch] > mp[ch]) + { + canFormed = false; + break; + } + } + + if(canFormed) + counter += word.size(); + } + + return counter; + + } +}; \ No newline at end of file From 057ebf9ae344879aefdef50fcefdc7117cf8a33e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Dec 2023 08:24:06 +0530 Subject: [PATCH 0434/1399] Create README - LeetHub --- .../README.md | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 1160-find-words-that-can-be-formed-by-characters/README.md diff --git a/1160-find-words-that-can-be-formed-by-characters/README.md b/1160-find-words-that-can-be-formed-by-characters/README.md new file mode 100644 index 00000000..62067b24 --- /dev/null +++ b/1160-find-words-that-can-be-formed-by-characters/README.md @@ -0,0 +1,30 @@ +

1160. Find Words That Can Be Formed by Characters

Easy


You are given an array of strings words and a string chars.

+ +

A string is good if it can be formed by characters from chars (each character can only be used once).

+ +

Return the sum of lengths of all good strings in words.

+ +

 

+

Example 1:

+ +
Input: words = ["cat","bt","hat","tree"], chars = "atach"
+Output: 6
+Explanation: The strings that can be formed are "cat" and "hat" so the answer is 3 + 3 = 6.
+
+ +

Example 2:

+ +
Input: words = ["hello","world","leetcode"], chars = "welldonehoneyr"
+Output: 10
+Explanation: The strings that can be formed are "hello" and "world" so the answer is 5 + 5 = 10.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= words.length <= 1000
  • +
  • 1 <= words[i].length, chars.length <= 100
  • +
  • words[i] and chars consist of lowercase English letters.
  • +
+
\ No newline at end of file From cd1fbe856b565bda0e648c97f19bd76e3b58fd51 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Dec 2023 08:24:06 +0530 Subject: [PATCH 0435/1399] Attach NOTES - LeetHub --- 1160-find-words-that-can-be-formed-by-characters/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1160-find-words-that-can-be-formed-by-characters/NOTES.md diff --git a/1160-find-words-that-can-be-formed-by-characters/NOTES.md b/1160-find-words-that-can-be-formed-by-characters/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1160-find-words-that-can-be-formed-by-characters/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 5c530079d8fe4162a8461945f5e2e66ba070ea32 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Dec 2023 08:24:10 +0530 Subject: [PATCH 0436/1399] Time: 174 ms (15.47%), Space: 49.6 MB (30.78%) - LeetHub --- ...words-that-can-be-formed-by-characters.cpp | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 1160-find-words-that-can-be-formed-by-characters/1160-find-words-that-can-be-formed-by-characters.cpp diff --git a/1160-find-words-that-can-be-formed-by-characters/1160-find-words-that-can-be-formed-by-characters.cpp b/1160-find-words-that-can-be-formed-by-characters/1160-find-words-that-can-be-formed-by-characters.cpp new file mode 100644 index 00000000..1b331ed1 --- /dev/null +++ b/1160-find-words-that-can-be-formed-by-characters/1160-find-words-that-can-be-formed-by-characters.cpp @@ -0,0 +1,39 @@ +class Solution { +public: + int countCharacters(vector& words, string chars) { + + int counter = 0; + + unordered_map mp; + + for(auto& ch : chars) + ++mp[ch]; + + for(auto& word : words) + { + bool canFormed = true; + + unordered_map freq; + + for(auto& ch : word) + { + ++freq[ch]; + } + + for(auto& ch : word) + { + if(freq[ch] > mp[ch]) + { + canFormed = false; + break; + } + } + + if(canFormed) + counter += word.size(); + } + + return counter; + + } +}; \ No newline at end of file From 2a0977ba80690d01a10747b6c8357e3438e5adc2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Dec 2023 08:24:21 +0530 Subject: [PATCH 0437/1399] Attach NOTES - LeetHub From 07e84d268cdd4cd464e977d90ba62f0439fb5a39 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Dec 2023 08:24:25 +0530 Subject: [PATCH 0438/1399] Time: 174 ms (15.47%), Space: 49.6 MB (30.78%) - LeetHub From bfd325aaf6548fabdae81ff01751898cca571770 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Dec 2023 10:47:18 +0530 Subject: [PATCH 0439/1399] Create README - LeetHub --- .../README.md | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2949-count-beautiful-substrings-ii/README.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2949-count-beautiful-substrings-ii/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2949-count-beautiful-substrings-ii/README.md new file mode 100644 index 00000000..f8de40bc --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2949-count-beautiful-substrings-ii/README.md @@ -0,0 +1,59 @@ +

No companies found for this problem
2949. Count Beautiful Substrings II

Hard


You are given a string s and a positive integer k.

+ +

Let vowels and consonants be the number of vowels and consonants in a string.

+ +

A string is beautiful if:

+ +
    +
  • vowels == consonants.
  • +
  • (vowels * consonants) % k == 0, in other terms the multiplication of vowels and consonants is divisible by k.
  • +
+ +

Return the number of non-empty beautiful substrings in the given string s.

+ +

A substring is a contiguous sequence of characters in a string.

+ +

Vowel letters in English are 'a', 'e', 'i', 'o', and 'u'.

+ +

Consonant letters in English are every letter except vowels.

+ +

 

+

Example 1:

+ +
Input: s = "baeyh", k = 2
+Output: 2
+Explanation: There are 2 beautiful substrings in the given string.
+- Substring "baeyh", vowels = 2 (["a",e"]), consonants = 2 (["y","h"]).
+You can see that string "aeyh" is beautiful as vowels == consonants and vowels * consonants % k == 0.
+- Substring "baeyh", vowels = 2 (["a",e"]), consonants = 2 (["b","y"]).
+You can see that string "baey" is beautiful as vowels == consonants and vowels * consonants % k == 0.
+It can be shown that there are only 2 beautiful substrings in the given string.
+
+ +

Example 2:

+ +
Input: s = "abba", k = 1
+Output: 3
+Explanation: There are 3 beautiful substrings in the given string.
+- Substring "abba", vowels = 1 (["a"]), consonants = 1 (["b"]).
+- Substring "abba", vowels = 1 (["a"]), consonants = 1 (["b"]).
+- Substring "abba", vowels = 2 (["a","a"]), consonants = 2 (["b","b"]).
+It can be shown that there are only 3 beautiful substrings in the given string.
+
+ +

Example 3:

+ +
Input: s = "bcdf", k = 1
+Output: 0
+Explanation: There are no beautiful substrings in the given string.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 5 * 104
  • +
  • 1 <= k <= 1000
  • +
  • s consists of only English lowercase letters.
  • +
+
\ No newline at end of file From 38f92ea79ddb6071abf4209f44243297f6627273 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Dec 2023 10:47:22 +0530 Subject: [PATCH 0440/1399] Time: 314 ms (49.52%), Space: 49.1 MB (14.44%) - LeetHub --- ...-div2949-count-beautiful-substrings-ii.cpp | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2949-count-beautiful-substrings-ii/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2949-count-beautiful-substrings-ii.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2949-count-beautiful-substrings-ii/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2949-count-beautiful-substrings-ii.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2949-count-beautiful-substrings-ii/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2949-count-beautiful-substrings-ii.cpp new file mode 100644 index 00000000..53c005cb --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2949-count-beautiful-substrings-ii/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2949-count-beautiful-substrings-ii.cpp @@ -0,0 +1,42 @@ +#define ll long long + +class Solution { +public: + long long beautifulSubstrings(string s, int k) { + + int n = s.size(); + + function isVowel = [&](char ch) + { + return (ch == 'a' or ch == 'e' or ch == 'i' or ch == 'o' or ch == 'u'); + }; + + unordered_map > mp; + + ll ans = 0; + + ++mp[0][0]; + + ll pref = 0, vow = 0; + + for(int i = 0; i < n; ++i) + { + if(isVowel(s[i])) + ++pref, ++vow; + else + --pref; + + for(auto& [f, cnt] : mp[pref]) + { + ll curr = (vow%k) - f; + if((curr * curr) % k == 0) + ans += cnt; + } + + ++mp[pref][vow%k]; + } + + return ans; + + } +}; \ No newline at end of file From 9309fa44eb38a764e848e49282879369dcee2c28 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Dec 2023 10:49:32 +0530 Subject: [PATCH 0441/1399] Attach NOTES - LeetHub --- 2949-count-beautiful-substrings-ii/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2949-count-beautiful-substrings-ii/NOTES.md diff --git a/2949-count-beautiful-substrings-ii/NOTES.md b/2949-count-beautiful-substrings-ii/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2949-count-beautiful-substrings-ii/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 28eeed5fcea40537a82080c069ee2cfd137b039d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Dec 2023 10:49:35 +0530 Subject: [PATCH 0442/1399] Time: 314 ms (49.52%), Space: 49.1 MB (14.44%) - LeetHub --- .../2949-count-beautiful-substrings-ii.cpp | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 2949-count-beautiful-substrings-ii/2949-count-beautiful-substrings-ii.cpp diff --git a/2949-count-beautiful-substrings-ii/2949-count-beautiful-substrings-ii.cpp b/2949-count-beautiful-substrings-ii/2949-count-beautiful-substrings-ii.cpp new file mode 100644 index 00000000..53c005cb --- /dev/null +++ b/2949-count-beautiful-substrings-ii/2949-count-beautiful-substrings-ii.cpp @@ -0,0 +1,42 @@ +#define ll long long + +class Solution { +public: + long long beautifulSubstrings(string s, int k) { + + int n = s.size(); + + function isVowel = [&](char ch) + { + return (ch == 'a' or ch == 'e' or ch == 'i' or ch == 'o' or ch == 'u'); + }; + + unordered_map > mp; + + ll ans = 0; + + ++mp[0][0]; + + ll pref = 0, vow = 0; + + for(int i = 0; i < n; ++i) + { + if(isVowel(s[i])) + ++pref, ++vow; + else + --pref; + + for(auto& [f, cnt] : mp[pref]) + { + ll curr = (vow%k) - f; + if((curr * curr) % k == 0) + ans += cnt; + } + + ++mp[pref][vow%k]; + } + + return ans; + + } +}; \ No newline at end of file From 49b7095ed7131167ff5276438da82ed2fe26306d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Dec 2023 07:42:47 +0530 Subject: [PATCH 0443/1399] Create README - LeetHub --- .../README.md | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1266-minimum-time-visiting-all-points/README.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1266-minimum-time-visiting-all-points/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1266-minimum-time-visiting-all-points/README.md new file mode 100644 index 00000000..2fd5cedb --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1266-minimum-time-visiting-all-points/README.md @@ -0,0 +1,43 @@ +

Apple
3
1266. Minimum Time Visiting All Points

Easy


On a 2D plane, there are n points with integer coordinates points[i] = [xi, yi]. Return the minimum time in seconds to visit all the points in the order given by points.

+ +

You can move according to these rules:

+ +
    +
  • In 1 second, you can either: + +
      +
    • move vertically by one unit,
    • +
    • move horizontally by one unit, or
    • +
    • move diagonally sqrt(2) units (in other words, move one unit vertically then one unit horizontally in 1 second).
    • +
    +
  • +
  • You have to visit the points in the same order as they appear in the array.
  • +
  • You are allowed to pass through points that appear later in the order, but these do not count as visits.
  • +
+ +

 

+

Example 1:

+ +
Input: points = [[1,1],[3,4],[-1,0]]
+Output: 7
+Explanation: One optimal path is [1,1] -> [2,2] -> [3,3] -> [3,4] -> [2,3] -> [1,2] -> [0,1] -> [-1,0]   
+Time from [1,1] to [3,4] = 3 seconds 
+Time from [3,4] to [-1,0] = 4 seconds
+Total time = 7 seconds
+ +

Example 2:

+ +
Input: points = [[3,2],[-2,2]]
+Output: 5
+
+ +

 

+

Constraints:

+ +
    +
  • points.length == n
  • +
  • 1 <= n <= 100
  • +
  • points[i].length == 2
  • +
  • -1000 <= points[i][0], points[i][1] <= 1000
  • +
+
\ No newline at end of file From 5929eb154b77d9ee1f07a168c1467c9f4bbf3cb7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Dec 2023 07:42:51 +0530 Subject: [PATCH 0444/1399] Time: 11 ms (13.23%), Space: 10.4 MB (95.08%) - LeetHub --- ...div1266-minimum-time-visiting-all-points.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1266-minimum-time-visiting-all-points/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1266-minimum-time-visiting-all-points.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1266-minimum-time-visiting-all-points/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1266-minimum-time-visiting-all-points.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1266-minimum-time-visiting-all-points/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1266-minimum-time-visiting-all-points.cpp new file mode 100644 index 00000000..a4e92859 --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1266-minimum-time-visiting-all-points/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tag-stylebackground-color-rgba0-10-32-005-divapple-divdiv-classcompanytagscontainer-tagoccurence3-div-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div1266-minimum-time-visiting-all-points.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + int minTimeToVisitAllPoints(vector>& points) { + + int n = points.size(); + + int t = 0; + + for(int i = 1; i < n; ++i) + { + t += max(abs(points[i][1] - points[i-1][1]), abs(points[i][0] - points[i-1][0])); + } + + return t; + + } +}; \ No newline at end of file From 7288e0f7d05151ac0115d73dd0cc97fffbfafae9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Dec 2023 08:02:28 +0530 Subject: [PATCH 0445/1399] Create README - LeetHub --- .../README.md | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2264-largest-3-same-digit-number-in-string/README.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2264-largest-3-same-digit-number-in-string/README.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2264-largest-3-same-digit-number-in-string/README.md new file mode 100644 index 00000000..aeb415ac --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2264-largest-3-same-digit-number-in-string/README.md @@ -0,0 +1,47 @@ +

No companies found for this problem
2264. Largest 3-Same-Digit Number in String

Easy


You are given a string num representing a large integer. An integer is good if it meets the following conditions:

+ +
    +
  • It is a substring of num with length 3.
  • +
  • It consists of only one unique digit.
  • +
+ +

Return the maximum good integer as a string or an empty string "" if no such integer exists.

+ +

Note:

+ +
    +
  • A substring is a contiguous sequence of characters within a string.
  • +
  • There may be leading zeroes in num or a good integer.
  • +
+ +

 

+

Example 1:

+ +
Input: num = "6777133339"
+Output: "777"
+Explanation: There are two distinct good integers: "777" and "333".
+"777" is the largest, so we return "777".
+
+ +

Example 2:

+ +
Input: num = "2300019"
+Output: "000"
+Explanation: "000" is the only good integer.
+
+ +

Example 3:

+ +
Input: num = "42352338"
+Output: ""
+Explanation: No substring of length 3 consists of only one unique digit. Therefore, there are no good integers.
+
+ +

 

+

Constraints:

+ +
    +
  • 3 <= num.length <= 1000
  • +
  • num only consists of digits.
  • +
+
\ No newline at end of file From 7d9b117fc38e55ea0c49463a34c301aede6f7199 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Dec 2023 08:02:28 +0530 Subject: [PATCH 0446/1399] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2264-largest-3-same-digit-number-in-string/NOTES.md diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2264-largest-3-same-digit-number-in-string/NOTES.md b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2264-largest-3-same-digit-number-in-string/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2264-largest-3-same-digit-number-in-string/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 9e7d2e03f5b323185fe5ec0dc28ccd1578625506 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Dec 2023 08:02:32 +0530 Subject: [PATCH 0447/1399] Time: 9 ms (5.22%), Space: 8.1 MB (5.80%) - LeetHub --- ...-largest-3-same-digit-number-in-string.cpp | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2264-largest-3-same-digit-number-in-string/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2264-largest-3-same-digit-number-in-string.cpp diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2264-largest-3-same-digit-number-in-string/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2264-largest-3-same-digit-number-in-string.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2264-largest-3-same-digit-number-in-string/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2264-largest-3-same-digit-number-in-string.cpp new file mode 100644 index 00000000..70a8ccbe --- /dev/null +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2264-largest-3-same-digit-number-in-string/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2264-largest-3-same-digit-number-in-string.cpp @@ -0,0 +1,44 @@ +class Solution { +public: + string largestGoodInteger(string num) { + + int n = num.size(); + + unordered_map mp; + + int maxi = 0; + string ans; + + int i = 0, j = 0, k = 3; + + string curr; + + while(j < n) + { + ++mp[num[j]]; + + curr += num[j]; + + if(curr.size() == 3) + { + int number = stoi(curr); + if(number >= maxi and mp.size() == 1) + { + maxi = number; + ans = curr; + } + + --mp[num[i]]; + if(mp[num[i]] == 0) + mp.erase(num[i]); + ++i; + + curr.erase(curr.begin()); + } + + ++j; + } + + return ans; + } +}; \ No newline at end of file From baaf2223e10f1a1d56203806e11260ffd2ec5b9b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Dec 2023 08:03:10 +0530 Subject: [PATCH 0448/1399] Create README - LeetHub --- .../README.md | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 2264-largest-3-same-digit-number-in-string/README.md diff --git a/2264-largest-3-same-digit-number-in-string/README.md b/2264-largest-3-same-digit-number-in-string/README.md new file mode 100644 index 00000000..6dfcc6a6 --- /dev/null +++ b/2264-largest-3-same-digit-number-in-string/README.md @@ -0,0 +1,47 @@ +

2264. Largest 3-Same-Digit Number in String

Easy


You are given a string num representing a large integer. An integer is good if it meets the following conditions:

+ +
    +
  • It is a substring of num with length 3.
  • +
  • It consists of only one unique digit.
  • +
+ +

Return the maximum good integer as a string or an empty string "" if no such integer exists.

+ +

Note:

+ +
    +
  • A substring is a contiguous sequence of characters within a string.
  • +
  • There may be leading zeroes in num or a good integer.
  • +
+ +

 

+

Example 1:

+ +
Input: num = "6777133339"
+Output: "777"
+Explanation: There are two distinct good integers: "777" and "333".
+"777" is the largest, so we return "777".
+
+ +

Example 2:

+ +
Input: num = "2300019"
+Output: "000"
+Explanation: "000" is the only good integer.
+
+ +

Example 3:

+ +
Input: num = "42352338"
+Output: ""
+Explanation: No substring of length 3 consists of only one unique digit. Therefore, there are no good integers.
+
+ +

 

+

Constraints:

+ +
    +
  • 3 <= num.length <= 1000
  • +
  • num only consists of digits.
  • +
+
\ No newline at end of file From bb41e77364a81261bbd4768c5aea3e08a1e263a6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Dec 2023 08:03:10 +0530 Subject: [PATCH 0449/1399] Attach NOTES - LeetHub --- 2264-largest-3-same-digit-number-in-string/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2264-largest-3-same-digit-number-in-string/NOTES.md diff --git a/2264-largest-3-same-digit-number-in-string/NOTES.md b/2264-largest-3-same-digit-number-in-string/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2264-largest-3-same-digit-number-in-string/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From d7676343395e30b9bb2d9a916cf299695816f63b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Dec 2023 08:03:14 +0530 Subject: [PATCH 0450/1399] Time: 9 ms (5.22%), Space: 8.1 MB (5.80%) - LeetHub --- ...-largest-3-same-digit-number-in-string.cpp | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 2264-largest-3-same-digit-number-in-string/2264-largest-3-same-digit-number-in-string.cpp diff --git a/2264-largest-3-same-digit-number-in-string/2264-largest-3-same-digit-number-in-string.cpp b/2264-largest-3-same-digit-number-in-string/2264-largest-3-same-digit-number-in-string.cpp new file mode 100644 index 00000000..70a8ccbe --- /dev/null +++ b/2264-largest-3-same-digit-number-in-string/2264-largest-3-same-digit-number-in-string.cpp @@ -0,0 +1,44 @@ +class Solution { +public: + string largestGoodInteger(string num) { + + int n = num.size(); + + unordered_map mp; + + int maxi = 0; + string ans; + + int i = 0, j = 0, k = 3; + + string curr; + + while(j < n) + { + ++mp[num[j]]; + + curr += num[j]; + + if(curr.size() == 3) + { + int number = stoi(curr); + if(number >= maxi and mp.size() == 1) + { + maxi = number; + ans = curr; + } + + --mp[num[i]]; + if(mp[num[i]] == 0) + mp.erase(num[i]); + ++i; + + curr.erase(curr.begin()); + } + + ++j; + } + + return ans; + } +}; \ No newline at end of file From 4945bbbcccf987efdd13f3dc04536c306c8d965d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Dec 2023 08:05:07 +0530 Subject: [PATCH 0451/1399] Attach NOTES - LeetHub From 0b0801bc8153029191f36fad9d3b9648d5930449 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Dec 2023 08:05:10 +0530 Subject: [PATCH 0452/1399] Time: 2 ms (53.04%), Space: 6.8 MB (91.88%) - LeetHub --- ...64-largest-3-same-digit-number-in-string.cpp | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2264-largest-3-same-digit-number-in-string/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2264-largest-3-same-digit-number-in-string.cpp b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2264-largest-3-same-digit-number-in-string/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2264-largest-3-same-digit-number-in-string.cpp index 70a8ccbe..2dc69092 100644 --- a/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2264-largest-3-same-digit-number-in-string/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2264-largest-3-same-digit-number-in-string.cpp +++ b/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2264-largest-3-same-digit-number-in-string/0div-idbig-omega-company-tagsdiv-idbig-omega-topbardiv-classcompanytagscontainer-styleoverflow-x-scroll-flex-wrap-nowrap-div-classcompanytagscontainer-tagno-companies-found-for-this-problem-div-divdiv-classcompanytagscontainer-chevrondivsvg-version11-idicon-xmlnshttp-wwww3org-2000-svg-xmlns-xlinkhttp-wwww3org-1999-xlink-x0px-y0px-viewbox0-0-32-32-fill4087f1-xml-spacepreserve-stylewidth-20px-polygon-points16-22-6-12-74-106-16-192-246-106-26-12-polygonrect-id-x3c-transparent-rectangle-x3e-classst0-fillnone-width32-height32-rect-svg-div-div-div-div2264-largest-3-same-digit-number-in-string.cpp @@ -4,9 +4,6 @@ class Solution { int n = num.size(); - unordered_map mp; - - int maxi = 0; string ans; int i = 0, j = 0, k = 3; @@ -15,24 +12,14 @@ class Solution { while(j < n) { - ++mp[num[j]]; - curr += num[j]; if(curr.size() == 3) { - int number = stoi(curr); - if(number >= maxi and mp.size() == 1) + if(j>=2 and num[j] == num[j-1] and num[j-1] == num[j-2]) { - maxi = number; - ans = curr; + ans = max(ans, curr); } - - --mp[num[i]]; - if(mp[num[i]] == 0) - mp.erase(num[i]); - ++i; - curr.erase(curr.begin()); } From 382a1c8d0182591d54b25423975c7919ce13d5f0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Dec 2023 08:05:43 +0530 Subject: [PATCH 0453/1399] Attach NOTES - LeetHub From f66fdf06ad9790d4b5eb8cd9688ceb48648442db Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Dec 2023 08:05:47 +0530 Subject: [PATCH 0454/1399] Time: 2 ms (53.04%), Space: 6.8 MB (91.88%) - LeetHub --- ...64-largest-3-same-digit-number-in-string.cpp | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/2264-largest-3-same-digit-number-in-string/2264-largest-3-same-digit-number-in-string.cpp b/2264-largest-3-same-digit-number-in-string/2264-largest-3-same-digit-number-in-string.cpp index 70a8ccbe..2dc69092 100644 --- a/2264-largest-3-same-digit-number-in-string/2264-largest-3-same-digit-number-in-string.cpp +++ b/2264-largest-3-same-digit-number-in-string/2264-largest-3-same-digit-number-in-string.cpp @@ -4,9 +4,6 @@ class Solution { int n = num.size(); - unordered_map mp; - - int maxi = 0; string ans; int i = 0, j = 0, k = 3; @@ -15,24 +12,14 @@ class Solution { while(j < n) { - ++mp[num[j]]; - curr += num[j]; if(curr.size() == 3) { - int number = stoi(curr); - if(number >= maxi and mp.size() == 1) + if(j>=2 and num[j] == num[j-1] and num[j-1] == num[j-2]) { - maxi = number; - ans = curr; + ans = max(ans, curr); } - - --mp[num[i]]; - if(mp[num[i]] == 0) - mp.erase(num[i]); - ++i; - curr.erase(curr.begin()); } From 926fffbe13b21e6a2734ad3f729e18041db7a646 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 6 Dec 2023 09:25:31 +0530 Subject: [PATCH 0455/1399] Create README - LeetHub --- .../README.md | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 1716-calculate-money-in-leetcode-bank/README.md diff --git a/1716-calculate-money-in-leetcode-bank/README.md b/1716-calculate-money-in-leetcode-bank/README.md new file mode 100644 index 00000000..3ebe921b --- /dev/null +++ b/1716-calculate-money-in-leetcode-bank/README.md @@ -0,0 +1,35 @@ +

1716. Calculate Money in Leetcode Bank

Easy


Hercy wants to save money for his first car. He puts money in the Leetcode bank every day.

+ +

He starts by putting in $1 on Monday, the first day. Every day from Tuesday to Sunday, he will put in $1 more than the day before. On every subsequent Monday, he will put in $1 more than the previous Monday.

+ +

Given n, return the total amount of money he will have in the Leetcode bank at the end of the nth day.

+ +

 

+

Example 1:

+ +
Input: n = 4
+Output: 10
+Explanation: After the 4th day, the total is 1 + 2 + 3 + 4 = 10.
+
+ +

Example 2:

+ +
Input: n = 10
+Output: 37
+Explanation: After the 10th day, the total is (1 + 2 + 3 + 4 + 5 + 6 + 7) + (2 + 3 + 4) = 37. Notice that on the 2nd Monday, Hercy only puts in $2.
+
+ +

Example 3:

+ +
Input: n = 20
+Output: 96
+Explanation: After the 20th day, the total is (1 + 2 + 3 + 4 + 5 + 6 + 7) + (2 + 3 + 4 + 5 + 6 + 7 + 8) + (3 + 4 + 5 + 6 + 7 + 8) = 96.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= 1000
  • +
+
\ No newline at end of file From 4febfedb260e44ecee7df0cb7b6dbe57a417687e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 6 Dec 2023 09:25:34 +0530 Subject: [PATCH 0456/1399] Time: 3 ms (13.68%), Space: 6.4 MB (35.53%) - LeetHub --- .../1716-calculate-money-in-leetcode-bank.cpp | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 1716-calculate-money-in-leetcode-bank/1716-calculate-money-in-leetcode-bank.cpp diff --git a/1716-calculate-money-in-leetcode-bank/1716-calculate-money-in-leetcode-bank.cpp b/1716-calculate-money-in-leetcode-bank/1716-calculate-money-in-leetcode-bank.cpp new file mode 100644 index 00000000..1bba9dac --- /dev/null +++ b/1716-calculate-money-in-leetcode-bank/1716-calculate-money-in-leetcode-bank.cpp @@ -0,0 +1,25 @@ +class Solution { +public: + int totalMoney(int n) { + + int totMoney = 0; + int track = 1, day = 0, curr = 1; + + + for(int i = 1; i<=n; ++i) + { + totMoney += curr; + ++curr; + ++day; + + if(day == 7) + { + curr = track+1; + ++track; + day = 0; + } + } + + return totMoney; + } +}; \ No newline at end of file From 5ee4129aa1a395a9fdb43e999d69f5bf8a8d2441 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 6 Dec 2023 09:29:44 +0530 Subject: [PATCH 0457/1399] Attach NOTES - LeetHub --- 1716-calculate-money-in-leetcode-bank/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1716-calculate-money-in-leetcode-bank/NOTES.md diff --git a/1716-calculate-money-in-leetcode-bank/NOTES.md b/1716-calculate-money-in-leetcode-bank/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1716-calculate-money-in-leetcode-bank/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 1efaab6fe80d563b6651104b66723de24f93226b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 6 Dec 2023 09:29:47 +0530 Subject: [PATCH 0458/1399] Time: 3 ms (13.68%), Space: 6.4 MB (35.53%) - LeetHub From 30d6c792f0c6ed88bd2a0bfd0a648c782bf02e0c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 7 Dec 2023 09:01:08 +0530 Subject: [PATCH 0459/1399] Create README - LeetHub --- 1903-largest-odd-number-in-string/README.md | 34 +++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 1903-largest-odd-number-in-string/README.md diff --git a/1903-largest-odd-number-in-string/README.md b/1903-largest-odd-number-in-string/README.md new file mode 100644 index 00000000..0def66fa --- /dev/null +++ b/1903-largest-odd-number-in-string/README.md @@ -0,0 +1,34 @@ +

1903. Largest Odd Number in String

Easy


You are given a string num, representing a large integer. Return the largest-valued odd integer (as a string) that is a non-empty substring of num, or an empty string "" if no odd integer exists.

+ +

A substring is a contiguous sequence of characters within a string.

+ +

 

+

Example 1:

+ +
Input: num = "52"
+Output: "5"
+Explanation: The only non-empty substrings are "5", "2", and "52". "5" is the only odd number.
+
+ +

Example 2:

+ +
Input: num = "4206"
+Output: ""
+Explanation: There are no odd numbers in "4206".
+
+ +

Example 3:

+ +
Input: num = "35427"
+Output: "35427"
+Explanation: "35427" is already an odd number.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= num.length <= 105
  • +
  • num only consists of digits and does not contain any leading zeros.
  • +
+
\ No newline at end of file From 4723d73cfafab4b131ce467658a384afa81665d5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 7 Dec 2023 09:01:08 +0530 Subject: [PATCH 0460/1399] Attach NOTES - LeetHub --- 1903-largest-odd-number-in-string/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1903-largest-odd-number-in-string/NOTES.md diff --git a/1903-largest-odd-number-in-string/NOTES.md b/1903-largest-odd-number-in-string/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1903-largest-odd-number-in-string/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From fc94e317a8267bb217a94721a6c8a70161e2b8b3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 7 Dec 2023 09:01:11 +0530 Subject: [PATCH 0461/1399] Time: 23 ms (91.89%), Space: 15.4 MB (37.80%) - LeetHub --- .../1903-largest-odd-number-in-string.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 1903-largest-odd-number-in-string/1903-largest-odd-number-in-string.cpp diff --git a/1903-largest-odd-number-in-string/1903-largest-odd-number-in-string.cpp b/1903-largest-odd-number-in-string/1903-largest-odd-number-in-string.cpp new file mode 100644 index 00000000..3422d3f7 --- /dev/null +++ b/1903-largest-odd-number-in-string/1903-largest-odd-number-in-string.cpp @@ -0,0 +1,16 @@ +class Solution { +public: + string largestOddNumber(string num) { + + int n = num.size(); + + for(int i = n-1; i >= 0; --i) + { + if((num[i] - '0') & 1) + return num.substr(0, i+1); + } + + return ""; + + } +}; \ No newline at end of file From 8d8d8904571f25e49c0d42d8bd24a9341e049ae7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 7 Dec 2023 09:02:36 +0530 Subject: [PATCH 0462/1399] Attach NOTES - LeetHub From 594462ae8a000664b278c94f6f229feca4fa969c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 7 Dec 2023 09:02:39 +0530 Subject: [PATCH 0463/1399] Time: 23 ms (91.89%), Space: 15.4 MB (37.80%) - LeetHub From 9ae84fd53e831f8440ce219c77b72c2432f99fa1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 8 Dec 2023 18:38:43 +0530 Subject: [PATCH 0464/1399] Create README - LeetHub --- .../README.md | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 0606-construct-string-from-binary-tree/README.md diff --git a/0606-construct-string-from-binary-tree/README.md b/0606-construct-string-from-binary-tree/README.md new file mode 100644 index 00000000..d304033f --- /dev/null +++ b/0606-construct-string-from-binary-tree/README.md @@ -0,0 +1,27 @@ +

606. Construct String from Binary Tree

Easy


Given the root of a binary tree, construct a string consisting of parenthesis and integers from a binary tree with the preorder traversal way, and return it.

+ +

Omit all the empty parenthesis pairs that do not affect the one-to-one mapping relationship between the string and the original binary tree.

+ +

 

+

Example 1:

+ +
Input: root = [1,2,3,4]
+Output: "1(2(4))(3)"
+Explanation: Originally, it needs to be "1(2(4)())(3()())", but you need to omit all the unnecessary empty parenthesis pairs. And it will be "1(2(4))(3)"
+
+ +

Example 2:

+ +
Input: root = [1,2,3,null,4]
+Output: "1(2()(4))(3)"
+Explanation: Almost the same as the first example, except we cannot omit the first parenthesis pair to break the one-to-one mapping relationship between the input and the output.
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is in the range [1, 104].
  • +
  • -1000 <= Node.val <= 1000
  • +
+
\ No newline at end of file From 52af689cd305c799f35fe29b77e3281cd4d928c8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 8 Dec 2023 18:38:43 +0530 Subject: [PATCH 0465/1399] Attach NOTES - LeetHub --- 0606-construct-string-from-binary-tree/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0606-construct-string-from-binary-tree/NOTES.md diff --git a/0606-construct-string-from-binary-tree/NOTES.md b/0606-construct-string-from-binary-tree/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0606-construct-string-from-binary-tree/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 456b0b14fd60cf24b83f865d6d401dae81ab787c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 8 Dec 2023 18:38:46 +0530 Subject: [PATCH 0466/1399] Time: 0 ms (100.00%), Space: 23.8 MB (80.74%) - LeetHub --- ...0606-construct-string-from-binary-tree.cpp | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 0606-construct-string-from-binary-tree/0606-construct-string-from-binary-tree.cpp diff --git a/0606-construct-string-from-binary-tree/0606-construct-string-from-binary-tree.cpp b/0606-construct-string-from-binary-tree/0606-construct-string-from-binary-tree.cpp new file mode 100644 index 00000000..0323b754 --- /dev/null +++ b/0606-construct-string-from-binary-tree/0606-construct-string-from-binary-tree.cpp @@ -0,0 +1,45 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + + void helper(TreeNode* root, string& ans) + { + if(root) + { + ans += to_string(root->val); + + if(root->left) + { + ans += '('; + helper(root->left, ans); + ans += ')'; + } + + if(root->right) + { + if(!root->left) + ans += "()"; + ans += '('; + helper(root->right, ans); + ans += ')'; + } + } + } + + string tree2str(TreeNode* root) { + + string ans; + helper(root, ans); + return ans; + } +}; \ No newline at end of file From 0b8b82cfdcf87fa1d929b13554321c7006fcc280 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 10 Dec 2023 09:21:40 +0530 Subject: [PATCH 0467/1399] Create README - LeetHub --- 0867-transpose-matrix/README.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 0867-transpose-matrix/README.md diff --git a/0867-transpose-matrix/README.md b/0867-transpose-matrix/README.md new file mode 100644 index 00000000..8eaf9eef --- /dev/null +++ b/0867-transpose-matrix/README.md @@ -0,0 +1,30 @@ +

867. Transpose Matrix

Easy


Given a 2D integer array matrix, return the transpose of matrix.

+ +

The transpose of a matrix is the matrix flipped over its main diagonal, switching the matrix's row and column indices.

+ +

+ +

 

+

Example 1:

+ +
Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
+Output: [[1,4,7],[2,5,8],[3,6,9]]
+
+ +

Example 2:

+ +
Input: matrix = [[1,2,3],[4,5,6]]
+Output: [[1,4],[2,5],[3,6]]
+
+ +

 

+

Constraints:

+ +
    +
  • m == matrix.length
  • +
  • n == matrix[i].length
  • +
  • 1 <= m, n <= 1000
  • +
  • 1 <= m * n <= 105
  • +
  • -109 <= matrix[i][j] <= 109
  • +
+
\ No newline at end of file From b473006d25dadba025d1a0facbc588e9d5758773 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 10 Dec 2023 09:21:40 +0530 Subject: [PATCH 0468/1399] Attach NOTES - LeetHub --- 0867-transpose-matrix/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0867-transpose-matrix/NOTES.md diff --git a/0867-transpose-matrix/NOTES.md b/0867-transpose-matrix/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0867-transpose-matrix/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 7bfe6344c0eb83a531ecd548f518dc4b3643ad0b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 10 Dec 2023 09:21:44 +0530 Subject: [PATCH 0469/1399] Time: 7 ms (87.64%), Space: 11.2 MB (10.71%) - LeetHub --- .../0867-transpose-matrix.cpp | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 0867-transpose-matrix/0867-transpose-matrix.cpp diff --git a/0867-transpose-matrix/0867-transpose-matrix.cpp b/0867-transpose-matrix/0867-transpose-matrix.cpp new file mode 100644 index 00000000..b24a5239 --- /dev/null +++ b/0867-transpose-matrix/0867-transpose-matrix.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + vector> transpose(vector>& matrix) { + + vector> ans; + + int n = matrix[0].size(), m = matrix.size(); + + for(int i = 0; i < n; ++i) + { + vector curr; + for(int j = 0; j < m; ++j) + { + curr.push_back(matrix[j][i]); + } + ans.push_back(curr); + } + return ans; + } +}; \ No newline at end of file From 59ce32b4286d5e52ce8634b5787e72c2b37b9c1c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 11 Dec 2023 09:37:21 +0530 Subject: [PATCH 0470/1399] Create README - LeetHub --- .../README.md | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 1287-element-appearing-more-than-25-in-sorted-array/README.md diff --git a/1287-element-appearing-more-than-25-in-sorted-array/README.md b/1287-element-appearing-more-than-25-in-sorted-array/README.md new file mode 100644 index 00000000..d7dfbbff --- /dev/null +++ b/1287-element-appearing-more-than-25-in-sorted-array/README.md @@ -0,0 +1,23 @@ +

1287. Element Appearing More Than 25% In Sorted Array

Easy


Given an integer array sorted in non-decreasing order, there is exactly one integer in the array that occurs more than 25% of the time, return that integer.

+ +

 

+

Example 1:

+ +
Input: arr = [1,2,2,6,6,6,6,7,10]
+Output: 6
+
+ +

Example 2:

+ +
Input: arr = [1,1]
+Output: 1
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= arr.length <= 104
  • +
  • 0 <= arr[i] <= 105
  • +
+
\ No newline at end of file From 1ae90984be1c8b6779d0cff0f17f0baeb0c65177 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 11 Dec 2023 09:37:25 +0530 Subject: [PATCH 0471/1399] Time: 6 ms (92.41%), Space: 12.7 MB (72.53%) - LeetHub --- ...nt-appearing-more-than-25-in-sorted-array.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 1287-element-appearing-more-than-25-in-sorted-array/1287-element-appearing-more-than-25-in-sorted-array.cpp diff --git a/1287-element-appearing-more-than-25-in-sorted-array/1287-element-appearing-more-than-25-in-sorted-array.cpp b/1287-element-appearing-more-than-25-in-sorted-array/1287-element-appearing-more-than-25-in-sorted-array.cpp new file mode 100644 index 00000000..f5a2fa74 --- /dev/null +++ b/1287-element-appearing-more-than-25-in-sorted-array/1287-element-appearing-more-than-25-in-sorted-array.cpp @@ -0,0 +1,16 @@ +class Solution { +public: + int findSpecialInteger(vector& arr) { + + int n = arr.size(), oneFourth = n/4; + + for(int i = 0; i < n - oneFourth; ++i) + { + if(arr[i] == arr[i+oneFourth]) + return arr[i]; + } + + return -1; + + } +}; \ No newline at end of file From a06742d13ed3d7d9e4d6c87799030579bbeaacaf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 11 Dec 2023 20:24:01 +0530 Subject: [PATCH 0472/1399] Create README - LeetHub --- .../README.md | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 2963-count-the-number-of-good-partitions/README.md diff --git a/2963-count-the-number-of-good-partitions/README.md b/2963-count-the-number-of-good-partitions/README.md new file mode 100644 index 00000000..057c9ef1 --- /dev/null +++ b/2963-count-the-number-of-good-partitions/README.md @@ -0,0 +1,38 @@ +

2963. Count the Number of Good Partitions

Hard


You are given a 0-indexed array nums consisting of positive integers.

+ +

A partition of an array into one or more contiguous subarrays is called good if no two subarrays contain the same number.

+ +

Return the total number of good partitions of nums.

+ +

Since the answer may be large, return it modulo 109 + 7.

+ +

 

+

Example 1:

+ +
Input: nums = [1,2,3,4]
+Output: 8
+Explanation: The 8 possible good partitions are: ([1], [2], [3], [4]), ([1], [2], [3,4]), ([1], [2,3], [4]), ([1], [2,3,4]), ([1,2], [3], [4]), ([1,2], [3,4]), ([1,2,3], [4]), and ([1,2,3,4]).
+
+ +

Example 2:

+ +
Input: nums = [1,1,1,1]
+Output: 1
+Explanation: The only possible good partition is: ([1,1,1,1]).
+
+ +

Example 3:

+ +
Input: nums = [1,2,1,3]
+Output: 2
+Explanation: The 2 possible good partitions are: ([1,2,1], [3]) and ([1,2,1,3]).
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 105
  • +
  • 1 <= nums[i] <= 109
  • +
+
\ No newline at end of file From cc15f28d062b28ca71ccaeb4de970afb23b7bf83 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 11 Dec 2023 20:24:05 +0530 Subject: [PATCH 0473/1399] Time: 226 ms (80.00%), Space: 134.3 MB (80.00%) - LeetHub --- ...63-count-the-number-of-good-partitions.cpp | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 2963-count-the-number-of-good-partitions/2963-count-the-number-of-good-partitions.cpp diff --git a/2963-count-the-number-of-good-partitions/2963-count-the-number-of-good-partitions.cpp b/2963-count-the-number-of-good-partitions/2963-count-the-number-of-good-partitions.cpp new file mode 100644 index 00000000..70e9c842 --- /dev/null +++ b/2963-count-the-number-of-good-partitions/2963-count-the-number-of-good-partitions.cpp @@ -0,0 +1,52 @@ +#define ll long long + +class Solution { +public: + + const int mod = 1e9 + 7; + + ll expo(ll x, ll y, ll mod) + { + ll res = 1; + + while(y > 0) + { + if(y & 1) + res = (res * x) % mod; + x = (x * x) % mod; + y >>= 1; + } + + return res; + } + + int numberOfGoodPartitions(vector& nums) { + + int n = nums.size(); + + int maxGotSoFar = 0; + + unordered_map mp; + + for(int i = 0; i < n; ++i) + { + mp[nums[i]] = i; + } + + int nonOverLappingPartitions = 0; + + for(int i = 0; i < n; ++i) + { + maxGotSoFar = max(maxGotSoFar, mp[nums[i]]); + + if(mp[nums[i]] == i and maxGotSoFar == i) + { + ++nonOverLappingPartitions; + } + + } + + return expo(2, nonOverLappingPartitions - 1, mod); + + } +}; \ No newline at end of file From 029aad36c79d1cc802234eec62d68c26fd8f7927 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 11 Dec 2023 20:29:52 +0530 Subject: [PATCH 0474/1399] Attach NOTES - LeetHub --- 2963-count-the-number-of-good-partitions/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2963-count-the-number-of-good-partitions/NOTES.md diff --git a/2963-count-the-number-of-good-partitions/NOTES.md b/2963-count-the-number-of-good-partitions/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2963-count-the-number-of-good-partitions/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From df301911c3a36eaa0edaded1d86914c9f8ee384a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 11 Dec 2023 20:29:55 +0530 Subject: [PATCH 0475/1399] Time: 226 ms (80.00%), Space: 134.3 MB (80.00%) - LeetHub From 33d31ed741393d35873ead031818397fc7b1e62c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 11 Dec 2023 21:03:33 +0530 Subject: [PATCH 0476/1399] Attach NOTES - LeetHub From c1178f7d93dc084ed0b3c801396ff2e8a9d1874b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 11 Dec 2023 21:03:36 +0530 Subject: [PATCH 0477/1399] Time: 226 ms (80.00%), Space: 134.3 MB (80.00%) - LeetHub From 1708fa4adcccf16151459e5ce135a3f1848e329e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 12 Dec 2023 08:31:32 +0530 Subject: [PATCH 0478/1399] Create README - LeetHub --- .../README.md | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 1464-maximum-product-of-two-elements-in-an-array/README.md diff --git a/1464-maximum-product-of-two-elements-in-an-array/README.md b/1464-maximum-product-of-two-elements-in-an-array/README.md new file mode 100644 index 00000000..c3561d41 --- /dev/null +++ b/1464-maximum-product-of-two-elements-in-an-array/README.md @@ -0,0 +1,30 @@ +

1464. Maximum Product of Two Elements in an Array

Easy


Given the array of integers nums, you will choose two different indices i and j of that array. Return the maximum value of (nums[i]-1)*(nums[j]-1). +

 

+

Example 1:

+ +
Input: nums = [3,4,5,2]
+Output: 12 
+Explanation: If you choose the indices i=1 and j=2 (indexed from 0), you will get the maximum value, that is, (nums[1]-1)*(nums[2]-1) = (4-1)*(5-1) = 3*4 = 12. 
+
+ +

Example 2:

+ +
Input: nums = [1,5,4,5]
+Output: 16
+Explanation: Choosing the indices i=1 and j=3 (indexed from 0), you will get the maximum value of (5-1)*(5-1) = 16.
+
+ +

Example 3:

+ +
Input: nums = [3,7]
+Output: 12
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= nums.length <= 500
  • +
  • 1 <= nums[i] <= 10^3
  • +
+
\ No newline at end of file From e40df366ccbb6bb32b0a92b22597d8f5b0c925c1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 12 Dec 2023 08:31:35 +0530 Subject: [PATCH 0479/1399] Time: 0 ms (100.00%), Space: 10.4 MB (38.51%) - LeetHub --- ...464-maximum-product-of-two-elements-in-an-array.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 1464-maximum-product-of-two-elements-in-an-array/1464-maximum-product-of-two-elements-in-an-array.cpp diff --git a/1464-maximum-product-of-two-elements-in-an-array/1464-maximum-product-of-two-elements-in-an-array.cpp b/1464-maximum-product-of-two-elements-in-an-array/1464-maximum-product-of-two-elements-in-an-array.cpp new file mode 100644 index 00000000..c3d7c4e1 --- /dev/null +++ b/1464-maximum-product-of-two-elements-in-an-array/1464-maximum-product-of-two-elements-in-an-array.cpp @@ -0,0 +1,10 @@ +class Solution { +public: + int maxProduct(vector& nums) { + + sort(nums.begin(), nums.end()); + + return (--nums[nums.size()-1] * --nums[nums.size()-2]); + + } +}; \ No newline at end of file From 1fd28e2bc8bd5cf6ae5e1030d09a79aeb1cbf233 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 12 Dec 2023 08:32:33 +0530 Subject: [PATCH 0480/1399] Attach NOTES - LeetHub --- 1464-maximum-product-of-two-elements-in-an-array/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1464-maximum-product-of-two-elements-in-an-array/NOTES.md diff --git a/1464-maximum-product-of-two-elements-in-an-array/NOTES.md b/1464-maximum-product-of-two-elements-in-an-array/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1464-maximum-product-of-two-elements-in-an-array/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From f7e4a4d34b31c227db1b6d777a41286977c87e7c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 12 Dec 2023 08:32:36 +0530 Subject: [PATCH 0481/1399] Time: 0 ms (100.00%), Space: 10.4 MB (38.51%) - LeetHub From 15599bd56e6e4b52f38152d0944245352c5558c6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 13 Dec 2023 08:13:32 +0530 Subject: [PATCH 0482/1399] Create README - LeetHub --- .../README.md | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 1582-special-positions-in-a-binary-matrix/README.md diff --git a/1582-special-positions-in-a-binary-matrix/README.md b/1582-special-positions-in-a-binary-matrix/README.md new file mode 100644 index 00000000..c973d914 --- /dev/null +++ b/1582-special-positions-in-a-binary-matrix/README.md @@ -0,0 +1,29 @@ +

1582. Special Positions in a Binary Matrix

Easy


Given an m x n binary matrix mat, return the number of special positions in mat.

+ +

A position (i, j) is called special if mat[i][j] == 1 and all other elements in row i and column j are 0 (rows and columns are 0-indexed).

+ +

 

+

Example 1:

+ +
Input: mat = [[1,0,0],[0,0,1],[1,0,0]]
+Output: 1
+Explanation: (1, 2) is a special position because mat[1][2] == 1 and all other elements in row 1 and column 2 are 0.
+
+ +

Example 2:

+ +
Input: mat = [[1,0,0],[0,1,0],[0,0,1]]
+Output: 3
+Explanation: (0, 0), (1, 1) and (2, 2) are special positions.
+
+ +

 

+

Constraints:

+ +
    +
  • m == mat.length
  • +
  • n == mat[i].length
  • +
  • 1 <= m, n <= 100
  • +
  • mat[i][j] is either 0 or 1.
  • +
+
\ No newline at end of file From b257280df09e957e3cb0488820dddf416a35525f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 13 Dec 2023 08:13:33 +0530 Subject: [PATCH 0483/1399] Attach NOTES - LeetHub --- 1582-special-positions-in-a-binary-matrix/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1582-special-positions-in-a-binary-matrix/NOTES.md diff --git a/1582-special-positions-in-a-binary-matrix/NOTES.md b/1582-special-positions-in-a-binary-matrix/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1582-special-positions-in-a-binary-matrix/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 2013862733e21823f4916383c7cb3bd1114d8574 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 13 Dec 2023 08:13:37 +0530 Subject: [PATCH 0484/1399] Time: 16 ms (68.95%), Space: 13.3 MB (46.58%) - LeetHub --- ...2-special-positions-in-a-binary-matrix.cpp | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 1582-special-positions-in-a-binary-matrix/1582-special-positions-in-a-binary-matrix.cpp diff --git a/1582-special-positions-in-a-binary-matrix/1582-special-positions-in-a-binary-matrix.cpp b/1582-special-positions-in-a-binary-matrix/1582-special-positions-in-a-binary-matrix.cpp new file mode 100644 index 00000000..b1c99cf8 --- /dev/null +++ b/1582-special-positions-in-a-binary-matrix/1582-special-positions-in-a-binary-matrix.cpp @@ -0,0 +1,31 @@ +class Solution { +public: + int numSpecial(vector>& mat) { + + int n = mat.size(), m = mat[0].size(); + + vector row(n), col(m); + + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < m; ++j) + { + if(mat[i][j] == 1) + ++row[i], ++col[j]; + } + } + + int specialPosition = 0; + + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < m; ++j) + { + if(mat[i][j] and row[i] == 1 and col[j] == 1) + ++specialPosition; + } + } + + return specialPosition; + } +}; \ No newline at end of file From d14a561533a7b6f2d107a22fc06770aa414e6c52 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 13 Dec 2023 08:14:39 +0530 Subject: [PATCH 0485/1399] Attach NOTES - LeetHub From 3c95bd9e65b2659fe51b5075ca393ccf3b8bf42a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 13 Dec 2023 08:14:42 +0530 Subject: [PATCH 0486/1399] Time: 16 ms (68.95%), Space: 13.3 MB (46.58%) - LeetHub From 959e59b84bfe7d06f8d96608d81c7ae6189abdf7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 14 Dec 2023 08:24:06 +0530 Subject: [PATCH 0487/1399] Create README - LeetHub --- .../README.md | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 2482-difference-between-ones-and-zeros-in-row-and-column/README.md diff --git a/2482-difference-between-ones-and-zeros-in-row-and-column/README.md b/2482-difference-between-ones-and-zeros-in-row-and-column/README.md new file mode 100644 index 00000000..3353ff56 --- /dev/null +++ b/2482-difference-between-ones-and-zeros-in-row-and-column/README.md @@ -0,0 +1,55 @@ +

2482. Difference Between Ones and Zeros in Row and Column

Medium


You are given a 0-indexed m x n binary matrix grid.

+ +

A 0-indexed m x n difference matrix diff is created with the following procedure:

+ +
    +
  • Let the number of ones in the ith row be onesRowi.
  • +
  • Let the number of ones in the jth column be onesColj.
  • +
  • Let the number of zeros in the ith row be zerosRowi.
  • +
  • Let the number of zeros in the jth column be zerosColj.
  • +
  • diff[i][j] = onesRowi + onesColj - zerosRowi - zerosColj
  • +
+ +

Return the difference matrix diff.

+ +

 

+

Example 1:

+ +
Input: grid = [[0,1,1],[1,0,1],[0,0,1]]
+Output: [[0,0,4],[0,0,4],[-2,-2,2]]
+Explanation:
+- diff[0][0] = onesRow0 + onesCol0 - zerosRow0 - zerosCol0 = 2 + 1 - 1 - 2 = 0 
+- diff[0][1] = onesRow0 + onesCol1 - zerosRow0 - zerosCol1 = 2 + 1 - 1 - 2 = 0 
+- diff[0][2] = onesRow0 + onesCol2 - zerosRow0 - zerosCol2 = 2 + 3 - 1 - 0 = 4 
+- diff[1][0] = onesRow1 + onesCol0 - zerosRow1 - zerosCol0 = 2 + 1 - 1 - 2 = 0 
+- diff[1][1] = onesRow1 + onesCol1 - zerosRow1 - zerosCol1 = 2 + 1 - 1 - 2 = 0 
+- diff[1][2] = onesRow1 + onesCol2 - zerosRow1 - zerosCol2 = 2 + 3 - 1 - 0 = 4 
+- diff[2][0] = onesRow2 + onesCol0 - zerosRow2 - zerosCol0 = 1 + 1 - 2 - 2 = -2
+- diff[2][1] = onesRow2 + onesCol1 - zerosRow2 - zerosCol1 = 1 + 1 - 2 - 2 = -2
+- diff[2][2] = onesRow2 + onesCol2 - zerosRow2 - zerosCol2 = 1 + 3 - 2 - 0 = 2
+
+ +

Example 2:

+ +
Input: grid = [[1,1,1],[1,1,1]]
+Output: [[5,5,5],[5,5,5]]
+Explanation:
+- diff[0][0] = onesRow0 + onesCol0 - zerosRow0 - zerosCol0 = 3 + 2 - 0 - 0 = 5
+- diff[0][1] = onesRow0 + onesCol1 - zerosRow0 - zerosCol1 = 3 + 2 - 0 - 0 = 5
+- diff[0][2] = onesRow0 + onesCol2 - zerosRow0 - zerosCol2 = 3 + 2 - 0 - 0 = 5
+- diff[1][0] = onesRow1 + onesCol0 - zerosRow1 - zerosCol0 = 3 + 2 - 0 - 0 = 5
+- diff[1][1] = onesRow1 + onesCol1 - zerosRow1 - zerosCol1 = 3 + 2 - 0 - 0 = 5
+- diff[1][2] = onesRow1 + onesCol2 - zerosRow1 - zerosCol2 = 3 + 2 - 0 - 0 = 5
+
+ +

 

+

Constraints:

+ +
    +
  • m == grid.length
  • +
  • n == grid[i].length
  • +
  • 1 <= m, n <= 105
  • +
  • 1 <= m * n <= 105
  • +
  • grid[i][j] is either 0 or 1.
  • +
+
\ No newline at end of file From b19456ab154158b120c52af78f2daa43b0e2fd99 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 14 Dec 2023 08:24:06 +0530 Subject: [PATCH 0488/1399] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2482-difference-between-ones-and-zeros-in-row-and-column/NOTES.md diff --git a/2482-difference-between-ones-and-zeros-in-row-and-column/NOTES.md b/2482-difference-between-ones-and-zeros-in-row-and-column/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2482-difference-between-ones-and-zeros-in-row-and-column/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 986565b93ec3f0a7c039cf68b34de0b1999db4b5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 14 Dec 2023 08:24:11 +0530 Subject: [PATCH 0489/1399] Time: 201 ms (47.29%), Space: 117.5 MB (65.73%) - LeetHub --- ...tween-ones-and-zeros-in-row-and-column.cpp | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 2482-difference-between-ones-and-zeros-in-row-and-column/2482-difference-between-ones-and-zeros-in-row-and-column.cpp diff --git a/2482-difference-between-ones-and-zeros-in-row-and-column/2482-difference-between-ones-and-zeros-in-row-and-column.cpp b/2482-difference-between-ones-and-zeros-in-row-and-column/2482-difference-between-ones-and-zeros-in-row-and-column.cpp new file mode 100644 index 00000000..97f982b3 --- /dev/null +++ b/2482-difference-between-ones-and-zeros-in-row-and-column/2482-difference-between-ones-and-zeros-in-row-and-column.cpp @@ -0,0 +1,28 @@ +class Solution { +public: + vector> onesMinusZeros(vector>& grid) { + + int n = grid.size(), m = grid[0].size(); + + vector row(n, 0), col(m, 0); + + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < m; ++j) + { + if(grid[i][j] == 1) + ++row[i], ++col[j]; + } + } + + vector> diff(n, vector(m)); + + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < m; ++j) + diff[i][j] = (row[i] + col[j] - (m - row[i]) - (n - col[j])); + } + + return diff; + } +}; \ No newline at end of file From ece63523731d20f51c7e8817eec183bb0f7829cb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 14 Dec 2023 08:25:48 +0530 Subject: [PATCH 0490/1399] Attach NOTES - LeetHub From 483641701d0bbba576a281c777b3f6dfe41810ca Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 14 Dec 2023 08:25:51 +0530 Subject: [PATCH 0491/1399] Time: 201 ms (47.29%), Space: 117.5 MB (65.73%) - LeetHub From 34e82c2e68ecb8aca8c84e0c5d94be9be0bb05fc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 16 Dec 2023 08:39:18 +0530 Subject: [PATCH 0492/1399] Create README - LeetHub --- 0242-valid-anagram/README.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 0242-valid-anagram/README.md diff --git a/0242-valid-anagram/README.md b/0242-valid-anagram/README.md new file mode 100644 index 00000000..c06723c0 --- /dev/null +++ b/0242-valid-anagram/README.md @@ -0,0 +1,23 @@ +

242. Valid Anagram

Easy


Given two strings s and t, return true if t is an anagram of s, and false otherwise.

+ +

An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

+ +

 

+

Example 1:

+
Input: s = "anagram", t = "nagaram"
+Output: true
+

Example 2:

+
Input: s = "rat", t = "car"
+Output: false
+
+

 

+

Constraints:

+ +
    +
  • 1 <= s.length, t.length <= 5 * 104
  • +
  • s and t consist of lowercase English letters.
  • +
+ +

 

+

Follow up: What if the inputs contain Unicode characters? How would you adapt your solution to such a case?

+
\ No newline at end of file From 6ed56ddbb8daf0b2baa3d8e16c4273007765ea47 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 16 Dec 2023 08:39:22 +0530 Subject: [PATCH 0493/1399] Time: 2 ms (98.77%), Space: 7.6 MB (79.21%) - LeetHub --- 0242-valid-anagram/0242-valid-anagram.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 0242-valid-anagram/0242-valid-anagram.cpp diff --git a/0242-valid-anagram/0242-valid-anagram.cpp b/0242-valid-anagram/0242-valid-anagram.cpp new file mode 100644 index 00000000..e0ced785 --- /dev/null +++ b/0242-valid-anagram/0242-valid-anagram.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + bool isAnagram(string s, string t) { + + vector freq(26, 0); + + for(auto& ch : s) + ++freq[ch - 'a']; + + for(auto& ch : t) + --freq[ch - 'a']; + + for(int i = 0; i< 26; ++i) + { + if(freq[i] != 0) return false; + } + + return true; + } +}; \ No newline at end of file From d3c4083bf0551c1a0211ecb383fc07715d379f84 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 16 Dec 2023 08:39:36 +0530 Subject: [PATCH 0494/1399] Attach NOTES - LeetHub --- 0242-valid-anagram/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0242-valid-anagram/NOTES.md diff --git a/0242-valid-anagram/NOTES.md b/0242-valid-anagram/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0242-valid-anagram/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 4638a93f8c7e795811213f5e132f5598bc171279 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 16 Dec 2023 08:39:40 +0530 Subject: [PATCH 0495/1399] Time: 2 ms (98.77%), Space: 7.6 MB (79.21%) - LeetHub From bbd273d4dc5eed863b223258711d2ad310a9df71 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 17 Dec 2023 08:39:21 +0530 Subject: [PATCH 0496/1399] Create README - LeetHub --- 0938-range-sum-of-bst/README.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 0938-range-sum-of-bst/README.md diff --git a/0938-range-sum-of-bst/README.md b/0938-range-sum-of-bst/README.md new file mode 100644 index 00000000..25f2c7e6 --- /dev/null +++ b/0938-range-sum-of-bst/README.md @@ -0,0 +1,27 @@ +

938. Range Sum of BST

Easy


Given the root node of a binary search tree and two integers low and high, return the sum of values of all nodes with a value in the inclusive range [low, high].

+ +

 

+

Example 1:

+ +
Input: root = [10,5,15,3,7,null,18], low = 7, high = 15
+Output: 32
+Explanation: Nodes 7, 10, and 15 are in the range [7, 15]. 7 + 10 + 15 = 32.
+
+ +

Example 2:

+ +
Input: root = [10,5,15,3,7,13,18,1,null,6], low = 6, high = 10
+Output: 23
+Explanation: Nodes 6, 7, and 10 are in the range [6, 10]. 6 + 7 + 10 = 23.
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is in the range [1, 2 * 104].
  • +
  • 1 <= Node.val <= 105
  • +
  • 1 <= low <= high <= 105
  • +
  • All Node.val are unique.
  • +
+
\ No newline at end of file From 5c0f7a38b5af69dc4f98fef736100864afbe4053 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 17 Dec 2023 08:39:22 +0530 Subject: [PATCH 0497/1399] Attach NOTES - LeetHub --- 0938-range-sum-of-bst/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0938-range-sum-of-bst/NOTES.md diff --git a/0938-range-sum-of-bst/NOTES.md b/0938-range-sum-of-bst/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0938-range-sum-of-bst/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From d7e010f8ecc25a51cb7b94dcb970e02f4c2307d2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 17 Dec 2023 08:39:59 +0530 Subject: [PATCH 0498/1399] Create README - LeetHub From 536815bac1eb8fcb1281092a29248f3494b6ec9a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 17 Dec 2023 08:40:00 +0530 Subject: [PATCH 0499/1399] Attach NOTES - LeetHub From ac7d164fb8566101498672f69b21729dd4c7a654 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 17 Dec 2023 08:40:03 +0530 Subject: [PATCH 0500/1399] Time: 92 ms (72.75%), Space: 65 MB (50.95%) - LeetHub --- .../0938-range-sum-of-bst.cpp | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 0938-range-sum-of-bst/0938-range-sum-of-bst.cpp diff --git a/0938-range-sum-of-bst/0938-range-sum-of-bst.cpp b/0938-range-sum-of-bst/0938-range-sum-of-bst.cpp new file mode 100644 index 00000000..235bfc29 --- /dev/null +++ b/0938-range-sum-of-bst/0938-range-sum-of-bst.cpp @@ -0,0 +1,27 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + int rangeSumBST(TreeNode* root, int low, int high) { + + if(!root) + return 0; + + if(root->val < low) + return rangeSumBST(root->right, low, high); + + if(root->val > high) + return rangeSumBST(root->left, low, high); + + return root->val + rangeSumBST(root->left, low, high) + rangeSumBST(root->right, low, high); + } +}; \ No newline at end of file From 0666c6991ce6c6aed965c5825400413aa6999deb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 17 Dec 2023 08:40:20 +0530 Subject: [PATCH 0501/1399] Attach NOTES - LeetHub From 69a730579724221fbdebeb7d837c9b7bf0bf6213 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 17 Dec 2023 08:40:24 +0530 Subject: [PATCH 0502/1399] Time: 92 ms (72.75%), Space: 65 MB (50.95%) - LeetHub From 957aee2ddd31f164fdba68008fdc9437c3227d4b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 17 Dec 2023 22:41:17 +0530 Subject: [PATCH 0503/1399] Create README - LeetHub --- 2353-design-a-food-rating-system/README.md | 63 ++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 2353-design-a-food-rating-system/README.md diff --git a/2353-design-a-food-rating-system/README.md b/2353-design-a-food-rating-system/README.md new file mode 100644 index 00000000..085588a8 --- /dev/null +++ b/2353-design-a-food-rating-system/README.md @@ -0,0 +1,63 @@ +

2353. Design a Food Rating System

Medium


Design a food rating system that can do the following:

+ +
    +
  • Modify the rating of a food item listed in the system.
  • +
  • Return the highest-rated food item for a type of cuisine in the system.
  • +
+ +

Implement the FoodRatings class:

+ +
    +
  • FoodRatings(String[] foods, String[] cuisines, int[] ratings) Initializes the system. The food items are described by foods, cuisines and ratings, all of which have a length of n. + +
      +
    • foods[i] is the name of the ith food,
    • +
    • cuisines[i] is the type of cuisine of the ith food, and
    • +
    • ratings[i] is the initial rating of the ith food.
    • +
    +
  • +
  • void changeRating(String food, int newRating) Changes the rating of the food item with the name food.
  • +
  • String highestRated(String cuisine) Returns the name of the food item that has the highest rating for the given type of cuisine. If there is a tie, return the item with the lexicographically smaller name.
  • +
+ +

Note that a string x is lexicographically smaller than string y if x comes before y in dictionary order, that is, either x is a prefix of y, or if i is the first position such that x[i] != y[i], then x[i] comes before y[i] in alphabetic order.

+ +

 

+

Example 1:

+ +
Input
+["FoodRatings", "highestRated", "highestRated", "changeRating", "highestRated", "changeRating", "highestRated"]
+[[["kimchi", "miso", "sushi", "moussaka", "ramen", "bulgogi"], ["korean", "japanese", "japanese", "greek", "japanese", "korean"], [9, 12, 8, 15, 14, 7]], ["korean"], ["japanese"], ["sushi", 16], ["japanese"], ["ramen", 16], ["japanese"]]
+Output
+[null, "kimchi", "ramen", null, "sushi", null, "ramen"]
+
+Explanation
+FoodRatings foodRatings = new FoodRatings(["kimchi", "miso", "sushi", "moussaka", "ramen", "bulgogi"], ["korean", "japanese", "japanese", "greek", "japanese", "korean"], [9, 12, 8, 15, 14, 7]);
+foodRatings.highestRated("korean"); // return "kimchi"
+                                    // "kimchi" is the highest rated korean food with a rating of 9.
+foodRatings.highestRated("japanese"); // return "ramen"
+                                      // "ramen" is the highest rated japanese food with a rating of 14.
+foodRatings.changeRating("sushi", 16); // "sushi" now has a rating of 16.
+foodRatings.highestRated("japanese"); // return "sushi"
+                                      // "sushi" is the highest rated japanese food with a rating of 16.
+foodRatings.changeRating("ramen", 16); // "ramen" now has a rating of 16.
+foodRatings.highestRated("japanese"); // return "ramen"
+                                      // Both "sushi" and "ramen" have a rating of 16.
+                                      // However, "ramen" is lexicographically smaller than "sushi".
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= 2 * 104
  • +
  • n == foods.length == cuisines.length == ratings.length
  • +
  • 1 <= foods[i].length, cuisines[i].length <= 10
  • +
  • foods[i], cuisines[i] consist of lowercase English letters.
  • +
  • 1 <= ratings[i] <= 108
  • +
  • All the strings in foods are distinct.
  • +
  • food will be the name of a food item in the system across all calls to changeRating.
  • +
  • cuisine will be a type of cuisine of at least one food item in the system across all calls to highestRated.
  • +
  • At most 2 * 104 calls in total will be made to changeRating and highestRated.
  • +
+
\ No newline at end of file From b6d1d05cfdf64b1ada84d710555f5a256b88aec4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 17 Dec 2023 22:41:17 +0530 Subject: [PATCH 0504/1399] Attach NOTES - LeetHub --- 2353-design-a-food-rating-system/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2353-design-a-food-rating-system/NOTES.md diff --git a/2353-design-a-food-rating-system/NOTES.md b/2353-design-a-food-rating-system/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2353-design-a-food-rating-system/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 12629e817cc8432a3aefbeb30379fbe1be9177e9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 17 Dec 2023 22:41:21 +0530 Subject: [PATCH 0505/1399] Time: 414 ms (65.55%), Space: 167.1 MB (30.14%) - LeetHub --- .../2353-design-a-food-rating-system.cpp | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 2353-design-a-food-rating-system/2353-design-a-food-rating-system.cpp diff --git a/2353-design-a-food-rating-system/2353-design-a-food-rating-system.cpp b/2353-design-a-food-rating-system/2353-design-a-food-rating-system.cpp new file mode 100644 index 00000000..f865a9a6 --- /dev/null +++ b/2353-design-a-food-rating-system/2353-design-a-food-rating-system.cpp @@ -0,0 +1,34 @@ +class FoodRatings { +public: + map>> s; + unordered_map cus; + unordered_map rat; + FoodRatings(vector& foods, vector& cuisines, vector& ratings) { + for(int i=0;i p= *(s[cuisine].begin()); + return p.second; + } +}; + +/** + * Your FoodRatings object will be instantiated and called as such: + * FoodRatings* obj = new FoodRatings(foods, cuisines, ratings); + * obj->changeRating(food,newRating); + * string param_2 = obj->highestRated(cuisine); + */ \ No newline at end of file From 573d54269cd1486f3ee38c8aa97798896d33582e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 17 Dec 2023 23:34:58 +0530 Subject: [PATCH 0506/1399] Create README - LeetHub --- .../README.md | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 0160-intersection-of-two-linked-lists/README.md diff --git a/0160-intersection-of-two-linked-lists/README.md b/0160-intersection-of-two-linked-lists/README.md new file mode 100644 index 00000000..bf03bc16 --- /dev/null +++ b/0160-intersection-of-two-linked-lists/README.md @@ -0,0 +1,64 @@ +

160. Intersection of Two Linked Lists

Easy


Given the heads of two singly linked-lists headA and headB, return the node at which the two lists intersect. If the two linked lists have no intersection at all, return null.

+ +

For example, the following two linked lists begin to intersect at node c1:

+ +

The test cases are generated such that there are no cycles anywhere in the entire linked structure.

+ +

Note that the linked lists must retain their original structure after the function returns.

+ +

Custom Judge:

+ +

The inputs to the judge are given as follows (your program is not given these inputs):

+ +
    +
  • intersectVal - The value of the node where the intersection occurs. This is 0 if there is no intersected node.
  • +
  • listA - The first linked list.
  • +
  • listB - The second linked list.
  • +
  • skipA - The number of nodes to skip ahead in listA (starting from the head) to get to the intersected node.
  • +
  • skipB - The number of nodes to skip ahead in listB (starting from the head) to get to the intersected node.
  • +
+ +

The judge will then create the linked structure based on these inputs and pass the two heads, headA and headB to your program. If you correctly return the intersected node, then your solution will be accepted.

+ +

 

+

Example 1:

+ +
Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3
+Output: Intersected at '8'
+Explanation: The intersected node's value is 8 (note that this must not be 0 if the two lists intersect).
+From the head of A, it reads as [4,1,8,4,5]. From the head of B, it reads as [5,6,1,8,4,5]. There are 2 nodes before the intersected node in A; There are 3 nodes before the intersected node in B.
+- Note that the intersected node's value is not 1 because the nodes with value 1 in A and B (2nd node in A and 3rd node in B) are different node references. In other words, they point to two different locations in memory, while the nodes with value 8 in A and B (3rd node in A and 4th node in B) point to the same location in memory.
+
+ +

Example 2:

+ +
Input: intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
+Output: Intersected at '2'
+Explanation: The intersected node's value is 2 (note that this must not be 0 if the two lists intersect).
+From the head of A, it reads as [1,9,1,2,4]. From the head of B, it reads as [3,2,4]. There are 3 nodes before the intersected node in A; There are 1 node before the intersected node in B.
+
+ +

Example 3:

+ +
Input: intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
+Output: No intersection
+Explanation: From the head of A, it reads as [2,6,4]. From the head of B, it reads as [1,5]. Since the two lists do not intersect, intersectVal must be 0, while skipA and skipB can be arbitrary values.
+Explanation: The two lists do not intersect, so return null.
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes of listA is in the m.
  • +
  • The number of nodes of listB is in the n.
  • +
  • 1 <= m, n <= 3 * 104
  • +
  • 1 <= Node.val <= 105
  • +
  • 0 <= skipA < m
  • +
  • 0 <= skipB < n
  • +
  • intersectVal is 0 if listA and listB do not intersect.
  • +
  • intersectVal == listA[skipA] == listB[skipB] if listA and listB intersect.
  • +
+ +

 

+Follow up: Could you write a solution that runs in O(m + n) time and use only O(1) memory?
\ No newline at end of file From fd6a338335b574300548447b53ce37e2a9f3d844 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 17 Dec 2023 23:50:11 +0530 Subject: [PATCH 0507/1399] Create README - LeetHub --- 0200-number-of-islands/README.md | 37 ++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 0200-number-of-islands/README.md diff --git a/0200-number-of-islands/README.md b/0200-number-of-islands/README.md new file mode 100644 index 00000000..f0398d0f --- /dev/null +++ b/0200-number-of-islands/README.md @@ -0,0 +1,37 @@ +

200. Number of Islands

Medium


Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands.

+ +

An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.

+ +

 

+

Example 1:

+ +
Input: grid = [
+  ["1","1","1","1","0"],
+  ["1","1","0","1","0"],
+  ["1","1","0","0","0"],
+  ["0","0","0","0","0"]
+]
+Output: 1
+
+ +

Example 2:

+ +
Input: grid = [
+  ["1","1","0","0","0"],
+  ["1","1","0","0","0"],
+  ["0","0","1","0","0"],
+  ["0","0","0","1","1"]
+]
+Output: 3
+
+ +

 

+

Constraints:

+ +
    +
  • m == grid.length
  • +
  • n == grid[i].length
  • +
  • 1 <= m, n <= 300
  • +
  • grid[i][j] is '0' or '1'.
  • +
+
\ No newline at end of file From ad880607ac9e06d25a75507b8a218afd71d4dc09 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 17 Dec 2023 23:50:12 +0530 Subject: [PATCH 0508/1399] Attach NOTES - LeetHub --- 0200-number-of-islands/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0200-number-of-islands/NOTES.md diff --git a/0200-number-of-islands/NOTES.md b/0200-number-of-islands/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0200-number-of-islands/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From cd6e380f041333557689dcf0e7e13d59a45be3df Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 17 Dec 2023 23:50:17 +0530 Subject: [PATCH 0509/1399] Time: 20 ms (97.87%), Space: 13 MB (53.30%) - LeetHub --- .../0200-number-of-islands.cpp | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 0200-number-of-islands/0200-number-of-islands.cpp diff --git a/0200-number-of-islands/0200-number-of-islands.cpp b/0200-number-of-islands/0200-number-of-islands.cpp new file mode 100644 index 00000000..7a793f47 --- /dev/null +++ b/0200-number-of-islands/0200-number-of-islands.cpp @@ -0,0 +1,52 @@ +class Solution { +public: + + vector dy = {-1, +1, 0, 0}; + vector dx = {0, 0, -1, +1}; + + bool isValid(int i, int j, int n, int m) + { + return (i >= 0 and j >= 0 and i < n and j < m); + } + + void dfs(int i, int j, int n, int m, vector>& grid, vector>& visited) + { + visited[i][j] = true; + + for(int k = 0; k < 4; ++k) + { + int newx = dx[k] + i; + int newy = dy[k] + j; + + if(isValid(newx, newy, n, m) and !visited[newx][newy] and grid[newx][newy] == '1') + { + dfs(newx, newy, n, m, grid, visited); + } + } + } + + int numIslands(vector>& grid) { + + int n = grid.size(); + int m = grid[0].size(); + + int numberOfIslands = 0; + + vector> visited(n, vector(m, false)); + + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < m; ++j) + { + if(grid[i][j] == '1' and !visited[i][j]) + { + ++numberOfIslands; + dfs(i, j, n, m, grid, visited); + } + } + } + + return numberOfIslands; + + } +}; \ No newline at end of file From 95eeac1d68d0e9fbc030683360940b4d8671fba1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 18 Dec 2023 00:39:16 +0530 Subject: [PATCH 0510/1399] Create README - LeetHub --- .../README.md | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 1358-number-of-substrings-containing-all-three-characters/README.md diff --git a/1358-number-of-substrings-containing-all-three-characters/README.md b/1358-number-of-substrings-containing-all-three-characters/README.md new file mode 100644 index 00000000..fbdc7088 --- /dev/null +++ b/1358-number-of-substrings-containing-all-three-characters/README.md @@ -0,0 +1,33 @@ +

1358. Number of Substrings Containing All Three Characters

Medium


Given a string s consisting only of characters a, b and c.

+ +

Return the number of substrings containing at least one occurrence of all these characters a, b and c.

+ +

 

+

Example 1:

+ +
Input: s = "abcabc"
+Output: 10
+Explanation: The substrings containing at least one occurrence of the characters ab and c are "abc", "abca", "abcab", "abcabc", "bca", "bcab", "bcabc", "cab", "cabc" and "abc" (again). 
+
+ +

Example 2:

+ +
Input: s = "aaacb"
+Output: 3
+Explanation: The substrings containing at least one occurrence of the characters ab and c are "aaacb", "aacb" and "acb". 
+
+ +

Example 3:

+ +
Input: s = "abc"
+Output: 1
+
+ +

 

+

Constraints:

+ +
    +
  • 3 <= s.length <= 5 x 10^4
  • +
  • s only consists of a, b or characters.
  • +
+
\ No newline at end of file From 2c4a5c77f9f72b1eaee9f2542bde2160a81fe38f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 18 Dec 2023 00:39:16 +0530 Subject: [PATCH 0511/1399] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1358-number-of-substrings-containing-all-three-characters/NOTES.md diff --git a/1358-number-of-substrings-containing-all-three-characters/NOTES.md b/1358-number-of-substrings-containing-all-three-characters/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1358-number-of-substrings-containing-all-three-characters/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 53360f9e5c791cf187799aac610daae64f6208f7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 18 Dec 2023 03:26:55 +0530 Subject: [PATCH 0512/1399] Create README - LeetHub --- 0514-freedom-trail/README.md | 40 ++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 0514-freedom-trail/README.md diff --git a/0514-freedom-trail/README.md b/0514-freedom-trail/README.md new file mode 100644 index 00000000..7f40ba56 --- /dev/null +++ b/0514-freedom-trail/README.md @@ -0,0 +1,40 @@ +

514. Freedom Trail

Hard


In the video game Fallout 4, the quest "Road to Freedom" requires players to reach a metal dial called the "Freedom Trail Ring" and use the dial to spell a specific keyword to open the door.

+ +

Given a string ring that represents the code engraved on the outer ring and another string key that represents the keyword that needs to be spelled, return the minimum number of steps to spell all the characters in the keyword.

+ +

Initially, the first character of the ring is aligned at the "12:00" direction. You should spell all the characters in key one by one by rotating ring clockwise or anticlockwise to make each character of the string key aligned at the "12:00" direction and then by pressing the center button.

+ +

At the stage of rotating the ring to spell the key character key[i]:

+ +
    +
  1. You can rotate the ring clockwise or anticlockwise by one place, which counts as one step. The final purpose of the rotation is to align one of ring's characters at the "12:00" direction, where this character must equal key[i].
  2. +
  3. If the character key[i] has been aligned at the "12:00" direction, press the center button to spell, which also counts as one step. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.
  4. +
+ +

 

+

Example 1:

+ +
Input: ring = "godding", key = "gd"
+Output: 4
+Explanation:
+For the first key character 'g', since it is already in place, we just need 1 step to spell this character. 
+For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
+Also, we need 1 more step for spelling.
+So the final output is 4.
+
+ +

Example 2:

+ +
Input: ring = "godding", key = "godding"
+Output: 13
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= ring.length, key.length <= 100
  • +
  • ring and key consist of only lower case English letters.
  • +
  • It is guaranteed that key could always be spelled by rotating ring.
  • +
+
\ No newline at end of file From 610818a87badc876431320e9b5e57d0d5c5dc967 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 18 Dec 2023 03:26:58 +0530 Subject: [PATCH 0513/1399] Time: 20 ms (62.83%), Space: 13.7 MB (48.67%) - LeetHub --- 0514-freedom-trail/0514-freedom-trail.cpp | 33 +++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 0514-freedom-trail/0514-freedom-trail.cpp diff --git a/0514-freedom-trail/0514-freedom-trail.cpp b/0514-freedom-trail/0514-freedom-trail.cpp new file mode 100644 index 00000000..4eaf3a81 --- /dev/null +++ b/0514-freedom-trail/0514-freedom-trail.cpp @@ -0,0 +1,33 @@ +class Solution { +public: + + int helper(int idx, int ptr, int n, int m, string& ring, string& key, vector>& dp) + { + if(idx >= m) + return 0; + + if(dp[idx][ptr] != -1) + return dp[idx][ptr]; + + int steps = 1e9; + + for(int i = 0; i < n; ++i) + { + if(ring[i] == key[idx]) + steps = min(steps, 1 + min(abs(i - ptr), n - abs(i-ptr)) + helper(idx+1, i, n, m, ring, key, dp)); + } + + return dp[idx][ptr] = steps; + } + + int findRotateSteps(string ring, string key) { + + int n = ring.size(); + int m = key.size(); + + vector> dp(m+1, vector(n+1, -1)); + + return helper(0, 0, n, m, ring, key, dp); + + } +}; \ No newline at end of file From 036889f4e18001ed963e384a6dba42732ccbd61b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 18 Dec 2023 22:00:53 +0530 Subject: [PATCH 0514/1399] Create README - LeetHub --- .../README.md | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 1913-maximum-product-difference-between-two-pairs/README.md diff --git a/1913-maximum-product-difference-between-two-pairs/README.md b/1913-maximum-product-difference-between-two-pairs/README.md new file mode 100644 index 00000000..3b05b9ed --- /dev/null +++ b/1913-maximum-product-difference-between-two-pairs/README.md @@ -0,0 +1,34 @@ +

1913. Maximum Product Difference Between Two Pairs

Easy


The product difference between two pairs (a, b) and (c, d) is defined as (a * b) - (c * d).

+ +
    +
  • For example, the product difference between (5, 6) and (2, 7) is (5 * 6) - (2 * 7) = 16.
  • +
+ +

Given an integer array nums, choose four distinct indices w, x, y, and z such that the product difference between pairs (nums[w], nums[x]) and (nums[y], nums[z]) is maximized.

+ +

Return the maximum such product difference.

+ +

 

+

Example 1:

+ +
Input: nums = [5,6,2,7,4]
+Output: 34
+Explanation: We can choose indices 1 and 3 for the first pair (6, 7) and indices 2 and 4 for the second pair (2, 4).
+The product difference is (6 * 7) - (2 * 4) = 34.
+
+ +

Example 2:

+ +
Input: nums = [4,2,5,9,7,4,8]
+Output: 64
+Explanation: We can choose indices 3 and 6 for the first pair (9, 8) and indices 1 and 5 for the second pair (2, 4).
+The product difference is (9 * 8) - (2 * 4) = 64.
+
+ +

 

+

Constraints:

+ +
    +
  • 4 <= nums.length <= 104
  • +
  • 1 <= nums[i] <= 104
  • +
\ No newline at end of file From 348f2201d1ad17c5f3412ef2ae25009bcdf8f6d8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 18 Dec 2023 22:00:57 +0530 Subject: [PATCH 0515/1399] Time: 33 ms (8.20%), Space: 20.6 MB (64.73%) - LeetHub --- ...3-maximum-product-difference-between-two-pairs.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 1913-maximum-product-difference-between-two-pairs/1913-maximum-product-difference-between-two-pairs.cpp diff --git a/1913-maximum-product-difference-between-two-pairs/1913-maximum-product-difference-between-two-pairs.cpp b/1913-maximum-product-difference-between-two-pairs/1913-maximum-product-difference-between-two-pairs.cpp new file mode 100644 index 00000000..7488c8e8 --- /dev/null +++ b/1913-maximum-product-difference-between-two-pairs/1913-maximum-product-difference-between-two-pairs.cpp @@ -0,0 +1,11 @@ +class Solution { +public: + int maxProductDifference(vector& nums) { + + int n = nums.size(); + + sort(nums.begin(),nums.end()); + + return ((nums[n-1] * nums[n-2]) - (nums[0] * nums[1])); + } +}; \ No newline at end of file From 6b242367996ff49277770fb84ab311afbf12485c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 18 Dec 2023 22:02:10 +0530 Subject: [PATCH 0516/1399] Attach NOTES - LeetHub --- 1913-maximum-product-difference-between-two-pairs/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1913-maximum-product-difference-between-two-pairs/NOTES.md diff --git a/1913-maximum-product-difference-between-two-pairs/NOTES.md b/1913-maximum-product-difference-between-two-pairs/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1913-maximum-product-difference-between-two-pairs/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 9c8e8b6036ab4d6272e62d594465310009d4bd9e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 18 Dec 2023 22:02:13 +0530 Subject: [PATCH 0517/1399] Time: 33 ms (8.20%), Space: 20.6 MB (64.73%) - LeetHub From 2ca1736c58970289b705142dd3577b242c8ccc63 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 19 Dec 2023 03:05:05 +0530 Subject: [PATCH 0518/1399] Create README - LeetHub --- 0402-remove-k-digits/README.md | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 0402-remove-k-digits/README.md diff --git a/0402-remove-k-digits/README.md b/0402-remove-k-digits/README.md new file mode 100644 index 00000000..4319e3f3 --- /dev/null +++ b/0402-remove-k-digits/README.md @@ -0,0 +1,33 @@ +

402. Remove K Digits

Medium


Given string num representing a non-negative integer num, and an integer k, return the smallest possible integer after removing k digits from num.

+ +

 

+

Example 1:

+ +
Input: num = "1432219", k = 3
+Output: "1219"
+Explanation: Remove the three digits 4, 3, and 2 to form the new number 1219 which is the smallest.
+
+ +

Example 2:

+ +
Input: num = "10200", k = 1
+Output: "200"
+Explanation: Remove the leading 1 and the number is 200. Note that the output must not contain leading zeroes.
+
+ +

Example 3:

+ +
Input: num = "10", k = 2
+Output: "0"
+Explanation: Remove all the digits from the number and it is left with nothing which is 0.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= k <= num.length <= 105
  • +
  • num consists of only digits.
  • +
  • num does not have any leading zeros except for the zero itself.
  • +
+
\ No newline at end of file From be47d1b117ca0d1e61465eba04b81b2c1117ddb1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 19 Dec 2023 03:05:06 +0530 Subject: [PATCH 0519/1399] Attach NOTES - LeetHub --- 0402-remove-k-digits/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0402-remove-k-digits/NOTES.md diff --git a/0402-remove-k-digits/NOTES.md b/0402-remove-k-digits/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0402-remove-k-digits/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 788a4a5552292c6d8b530aac22c5da86646d0c4c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 19 Dec 2023 03:05:09 +0530 Subject: [PATCH 0520/1399] Time: 14 ms (39.19%), Space: 9.1 MB (65.72%) - LeetHub --- 0402-remove-k-digits/0402-remove-k-digits.cpp | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 0402-remove-k-digits/0402-remove-k-digits.cpp diff --git a/0402-remove-k-digits/0402-remove-k-digits.cpp b/0402-remove-k-digits/0402-remove-k-digits.cpp new file mode 100644 index 00000000..a7e3602b --- /dev/null +++ b/0402-remove-k-digits/0402-remove-k-digits.cpp @@ -0,0 +1,48 @@ +class Solution { +public: + string removeKdigits(string num, int k) { + + if(k >= num.size()) + return "0"; + + stack st; + + int n = num.size(); + + st.push(num[0]); + + for(int i = 1; i < n; ++i) + { + while(k > 0 and !st.empty() and st.top() > num[i]) + { + --k; + st.pop(); + } + st.push(num[i]); + + if(st.size() == 1 and num[i] == '0') + st.pop(); + } + + while(k and !st.empty()) + { + --k; + st.pop(); + } + + string res; + + while(!st.empty()) + { + res += st.top(); + st.pop(); + } + + if(res.empty()) + return "0"; + + reverse(res.begin(), res.end()); + + return res; + } +}; \ No newline at end of file From 16de5add79ca3ad28aacafd733f05d3d5b90068e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 19 Dec 2023 03:56:11 +0530 Subject: [PATCH 0521/1399] Create README - LeetHub --- 0556-next-greater-element-iii/README.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 0556-next-greater-element-iii/README.md diff --git a/0556-next-greater-element-iii/README.md b/0556-next-greater-element-iii/README.md new file mode 100644 index 00000000..3827d0f3 --- /dev/null +++ b/0556-next-greater-element-iii/README.md @@ -0,0 +1,19 @@ +

556. Next Greater Element III

Medium


Given a positive integer n, find the smallest integer which has exactly the same digits existing in the integer n and is greater in value than n. If no such positive integer exists, return -1.

+ +

Note that the returned integer should fit in 32-bit integer, if there is a valid answer but it does not fit in 32-bit integer, return -1.

+ +

 

+

Example 1:

+
Input: n = 12
+Output: 21
+

Example 2:

+
Input: n = 21
+Output: -1
+
+

 

+

Constraints:

+ +
    +
  • 1 <= n <= 231 - 1
  • +
+
\ No newline at end of file From e081583344ff0728f017d0969ade0506f413b533 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 19 Dec 2023 03:56:15 +0530 Subject: [PATCH 0522/1399] Time: 0 ms (100.00%), Space: 6.5 MB (17.41%) - LeetHub --- .../0556-next-greater-element-iii.cpp | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 0556-next-greater-element-iii/0556-next-greater-element-iii.cpp diff --git a/0556-next-greater-element-iii/0556-next-greater-element-iii.cpp b/0556-next-greater-element-iii/0556-next-greater-element-iii.cpp new file mode 100644 index 00000000..6d89562c --- /dev/null +++ b/0556-next-greater-element-iii/0556-next-greater-element-iii.cpp @@ -0,0 +1,49 @@ +class Solution { +public: + + long long next_Permutation(int n) + { + string str = to_string(n); + + int breakPoint = -1; + + int m = str.size(); + + for(int i = m-2; i >= 0; --i) + { + if(str[i] < str[i+1]) + { + breakPoint = i; + break; + } + } + + if(breakPoint == -1) + { + reverse(str.begin(), str.end()); + } + else + { + for(int i = m-1; i > breakPoint; --i) + { + if(str[i] > str[breakPoint]) + { + swap(str[i], str[breakPoint]); + break; + } + } + + reverse(str.begin() + breakPoint + 1 , str.end()); + } + + return stoll(str); + } + + int nextGreaterElement(int n) { + + long long nextGreater = next_Permutation(n); + + return (nextGreater > INT_MAX or nextGreater <= n ? -1 : nextGreater); + + } +}; \ No newline at end of file From 39acd0d160a1c274f9d6891dddd88dcd9daeac1e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 19 Dec 2023 23:55:10 +0530 Subject: [PATCH 0523/1399] Create README - LeetHub --- 0661-image-smoother/README.md | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 0661-image-smoother/README.md diff --git a/0661-image-smoother/README.md b/0661-image-smoother/README.md new file mode 100644 index 00000000..6bd8ee84 --- /dev/null +++ b/0661-image-smoother/README.md @@ -0,0 +1,35 @@ +

661. Image Smoother

Easy


An image smoother is a filter of the size 3 x 3 that can be applied to each cell of an image by rounding down the average of the cell and the eight surrounding cells (i.e., the average of the nine cells in the blue smoother). If one or more of the surrounding cells of a cell is not present, we do not consider it in the average (i.e., the average of the four cells in the red smoother).

+ +

Given an m x n integer matrix img representing the grayscale of an image, return the image after applying the smoother on each cell of it.

+ +

 

+

Example 1:

+ +
Input: img = [[1,1,1],[1,0,1],[1,1,1]]
+Output: [[0,0,0],[0,0,0],[0,0,0]]
+Explanation:
+For the points (0,0), (0,2), (2,0), (2,2): floor(3/4) = floor(0.75) = 0
+For the points (0,1), (1,0), (1,2), (2,1): floor(5/6) = floor(0.83333333) = 0
+For the point (1,1): floor(8/9) = floor(0.88888889) = 0
+
+ +

Example 2:

+ +
Input: img = [[100,200,100],[200,50,200],[100,200,100]]
+Output: [[137,141,137],[141,138,141],[137,141,137]]
+Explanation:
+For the points (0,0), (0,2), (2,0), (2,2): floor((100+200+200+50)/4) = floor(137.5) = 137
+For the points (0,1), (1,0), (1,2), (2,1): floor((200+200+50+200+100+100)/6) = floor(141.666667) = 141
+For the point (1,1): floor((50+200+200+200+200+100+100+100+100)/9) = floor(138.888889) = 138
+
+ +

 

+

Constraints:

+ +
    +
  • m == img.length
  • +
  • n == img[i].length
  • +
  • 1 <= m, n <= 200
  • +
  • 0 <= img[i][j] <= 255
  • +
+
\ No newline at end of file From 3fa9614a570cf778aba53510aa3c1e4f511c03a6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 19 Dec 2023 23:55:14 +0530 Subject: [PATCH 0524/1399] Time: 32 ms (96.92%), Space: 22 MB (59.38%) - LeetHub --- 0661-image-smoother/0661-image-smoother.cpp | 30 +++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 0661-image-smoother/0661-image-smoother.cpp diff --git a/0661-image-smoother/0661-image-smoother.cpp b/0661-image-smoother/0661-image-smoother.cpp new file mode 100644 index 00000000..81933ccf --- /dev/null +++ b/0661-image-smoother/0661-image-smoother.cpp @@ -0,0 +1,30 @@ +class Solution { +public: + vector> imageSmoother(vector>& img) { + int rows = img.size(); + int cols = img[0].size(); + vector> result(rows, vector(cols, 0)); + + for (int i = 0; i < rows; ++i) + { + for (int j = 0; j < cols; ++j) + { + int total_sum = 0; + int count = 0; + + for (int l = max(0, i-1); l < min(rows, i+2); ++l) + { + for (int k = max(0, j-1); k < min(cols, j+2); ++k) + { + total_sum += img[l][k]; + count += 1; + } + } + + result[i][j] = total_sum / count; + } + } + + return result; + } +}; \ No newline at end of file From 40a773064c176242097d45ad2bce8faaa63a9381 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 20 Dec 2023 09:05:40 +0530 Subject: [PATCH 0525/1399] Create README - LeetHub --- 2706-buy-two-chocolates/README.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 2706-buy-two-chocolates/README.md diff --git a/2706-buy-two-chocolates/README.md b/2706-buy-two-chocolates/README.md new file mode 100644 index 00000000..c6497e3f --- /dev/null +++ b/2706-buy-two-chocolates/README.md @@ -0,0 +1,30 @@ +

2706. Buy Two Chocolates

Easy


You are given an integer array prices representing the prices of various chocolates in a store. You are also given a single integer money, which represents your initial amount of money.

+ +

You must buy exactly two chocolates in such a way that you still have some non-negative leftover money. You would like to minimize the sum of the prices of the two chocolates you buy.

+ +

Return the amount of money you will have leftover after buying the two chocolates. If there is no way for you to buy two chocolates without ending up in debt, return money. Note that the leftover must be non-negative.

+ +

 

+

Example 1:

+ +
Input: prices = [1,2,2], money = 3
+Output: 0
+Explanation: Purchase the chocolates priced at 1 and 2 units respectively. You will have 3 - 3 = 0 units of money afterwards. Thus, we return 0.
+
+ +

Example 2:

+ +
Input: prices = [3,2,3], money = 3
+Output: 3
+Explanation: You cannot buy 2 chocolates without going in debt, so we return 3.
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= prices.length <= 50
  • +
  • 1 <= prices[i] <= 100
  • +
  • 1 <= money <= 100
  • +
+
\ No newline at end of file From f626b122b7aee3104be860959e3c74368b4ec1e3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 20 Dec 2023 09:05:41 +0530 Subject: [PATCH 0526/1399] Attach NOTES - LeetHub --- 2706-buy-two-chocolates/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2706-buy-two-chocolates/NOTES.md diff --git a/2706-buy-two-chocolates/NOTES.md b/2706-buy-two-chocolates/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2706-buy-two-chocolates/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From f392a7d4044add3bbf8c28fa9cb599d8f9a77592 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 20 Dec 2023 09:05:44 +0530 Subject: [PATCH 0527/1399] Time: 11 ms (73.75%), Space: 47 MB (58.30%) - LeetHub --- 2706-buy-two-chocolates/2706-buy-two-chocolates.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 2706-buy-two-chocolates/2706-buy-two-chocolates.cpp diff --git a/2706-buy-two-chocolates/2706-buy-two-chocolates.cpp b/2706-buy-two-chocolates/2706-buy-two-chocolates.cpp new file mode 100644 index 00000000..f26c009f --- /dev/null +++ b/2706-buy-two-chocolates/2706-buy-two-chocolates.cpp @@ -0,0 +1,8 @@ +class Solution { +public: + int buyChoco(vector& prices, int money) { + sort(prices.begin(), prices.end()); + int sum = prices[0] + prices[1]; + return (sum > money)?money: (money - sum); + } +}; \ No newline at end of file From e750add6f84cc1bf1921ad97866bcb5abe036b8f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 21 Dec 2023 22:14:02 +0530 Subject: [PATCH 0528/1399] Create README - LeetHub --- .../README.md | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 1637-widest-vertical-area-between-two-points-containing-no-points/README.md diff --git a/1637-widest-vertical-area-between-two-points-containing-no-points/README.md b/1637-widest-vertical-area-between-two-points-containing-no-points/README.md new file mode 100644 index 00000000..1dd90505 --- /dev/null +++ b/1637-widest-vertical-area-between-two-points-containing-no-points/README.md @@ -0,0 +1,30 @@ +

1637. Widest Vertical Area Between Two Points Containing No Points

Medium


Given n points on a 2D plane where points[i] = [xi, yi], Return the widest vertical area between two points such that no points are inside the area.

+ +

A vertical area is an area of fixed-width extending infinitely along the y-axis (i.e., infinite height). The widest vertical area is the one with the maximum width.

+ +

Note that points on the edge of a vertical area are not considered included in the area.

+ +

 

+

Example 1:

+​ +
Input: points = [[8,7],[9,9],[7,4],[9,7]]
+Output: 1
+Explanation: Both the red and the blue area are optimal.
+
+ +

Example 2:

+ +
Input: points = [[3,1],[9,0],[1,0],[1,4],[5,3],[8,8]]
+Output: 3
+
+ +

 

+

Constraints:

+ +
    +
  • n == points.length
  • +
  • 2 <= n <= 105
  • +
  • points[i].length == 2
  • +
  • 0 <= xi, yi <= 109
  • +
+
\ No newline at end of file From c0968d3a7e1e605a71db51c5ca46460447b500ad Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 21 Dec 2023 22:14:02 +0530 Subject: [PATCH 0529/1399] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1637-widest-vertical-area-between-two-points-containing-no-points/NOTES.md diff --git a/1637-widest-vertical-area-between-two-points-containing-no-points/NOTES.md b/1637-widest-vertical-area-between-two-points-containing-no-points/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1637-widest-vertical-area-between-two-points-containing-no-points/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From afa2b7d95ce55be7b6de2c9be279bfd859ac0e2a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 21 Dec 2023 22:14:05 +0530 Subject: [PATCH 0530/1399] Time: 281 ms (6.93%), Space: 68.7 MB (51.13%) - LeetHub --- ...etween-two-points-containing-no-points.cpp | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 1637-widest-vertical-area-between-two-points-containing-no-points/1637-widest-vertical-area-between-two-points-containing-no-points.cpp diff --git a/1637-widest-vertical-area-between-two-points-containing-no-points/1637-widest-vertical-area-between-two-points-containing-no-points.cpp b/1637-widest-vertical-area-between-two-points-containing-no-points/1637-widest-vertical-area-between-two-points-containing-no-points.cpp new file mode 100644 index 00000000..ff3ea789 --- /dev/null +++ b/1637-widest-vertical-area-between-two-points-containing-no-points/1637-widest-vertical-area-between-two-points-containing-no-points.cpp @@ -0,0 +1,36 @@ +class Solution { +public: + int maxWidthOfVerticalArea(vector>& points) { + + int n = points.size(); + + int ans = 0; + + sort(points.begin(), points.end()); + + vector v; + + for(int i = 0; i < n; ++i) + { + v.push_back(points[i][0]); + } + + + for(int i = 0; i < n; ++i) + { + int x1 = v[i]; + int idx = upper_bound(v.begin(), v.end(), v[i]) - v.begin(); + + if(idx == n) + continue; + + int x2 = v[idx]; + + // cout< Date: Thu, 21 Dec 2023 22:16:02 +0530 Subject: [PATCH 0531/1399] Attach NOTES - LeetHub From bb757d55aba8f41ca1eff9cfcedd48865771d51e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 21 Dec 2023 22:16:07 +0530 Subject: [PATCH 0532/1399] Time: 248 ms (40.64%), Space: 66.5 MB (87.58%) - LeetHub --- ...etween-two-points-containing-no-points.cpp | 29 ++++--------------- 1 file changed, 5 insertions(+), 24 deletions(-) diff --git a/1637-widest-vertical-area-between-two-points-containing-no-points/1637-widest-vertical-area-between-two-points-containing-no-points.cpp b/1637-widest-vertical-area-between-two-points-containing-no-points/1637-widest-vertical-area-between-two-points-containing-no-points.cpp index ff3ea789..8da3ae7b 100644 --- a/1637-widest-vertical-area-between-two-points-containing-no-points/1637-widest-vertical-area-between-two-points-containing-no-points.cpp +++ b/1637-widest-vertical-area-between-two-points-containing-no-points/1637-widest-vertical-area-between-two-points-containing-no-points.cpp @@ -1,36 +1,17 @@ class Solution { public: int maxWidthOfVerticalArea(vector>& points) { - + int n = points.size(); - int ans = 0; - sort(points.begin(), points.end()); - vector v; - - for(int i = 0; i < n; ++i) - { - v.push_back(points[i][0]); - } - + int ans = 0; - for(int i = 0; i < n; ++i) - { - int x1 = v[i]; - int idx = upper_bound(v.begin(), v.end(), v[i]) - v.begin(); - - if(idx == n) - continue; - - int x2 = v[idx]; - - // cout< Date: Fri, 22 Dec 2023 00:06:26 +0530 Subject: [PATCH 0533/1399] Update README.md --- README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/README.md b/README.md index c84a2351..eec66b90 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,24 @@ # Leetcode This Repository Contains All My Solved Leetcode Problems. + 2023 + +leetcode.com/rewind/?ref=KnockCat + +![LeetCode_Rewind_2023](https://github.com/knockcat/Temp/assets/85362504/ceb0a224-1036-478d-9d21-805c14ed6389) + +![image](https://github.com/knockcat/Temp/assets/85362504/7e954346-c8a9-4786-89f4-949f2406e3b9) + +![image](https://github.com/knockcat/Temp/assets/85362504/2c7a62b7-221f-4493-9111-a5d7bb5ba85b) + +![Screenshot (965)](https://github.com/knockcat/Temp/assets/85362504/d06eb3c4-8ba9-4d4e-8b79-fea5eca506ca) + +![image](https://github.com/knockcat/Temp/assets/85362504/d835a19b-f770-4946-9b3e-cc909bf50e10) + +![image](https://github.com/knockcat/Temp/assets/85362504/249fc2eb-1410-4ee8-aaf2-8f8bda6db33b) + + 2022 + ![image](https://user-images.githubusercontent.com/85362504/210123821-c3c67c36-5321-4b00-88de-faa8d3b0d30f.png) From 6411c931f5023fd1af44389e3c34b09cbd6979dc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 22 Dec 2023 08:53:35 +0530 Subject: [PATCH 0534/1399] Create README - LeetHub --- .../README.md | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 1422-maximum-score-after-splitting-a-string/README.md diff --git a/1422-maximum-score-after-splitting-a-string/README.md b/1422-maximum-score-after-splitting-a-string/README.md new file mode 100644 index 00000000..a8524c03 --- /dev/null +++ b/1422-maximum-score-after-splitting-a-string/README.md @@ -0,0 +1,39 @@ +

1422. Maximum Score After Splitting a String

Easy


Given a string s of zeros and ones, return the maximum score after splitting the string into two non-empty substrings (i.e. left substring and right substring).

+ +

The score after splitting a string is the number of zeros in the left substring plus the number of ones in the right substring.

+ +

 

+

Example 1:

+ +
Input: s = "011101"
+Output: 5 
+Explanation: 
+All possible ways of splitting s into two non-empty substrings are:
+left = "0" and right = "11101", score = 1 + 4 = 5 
+left = "01" and right = "1101", score = 1 + 3 = 4 
+left = "011" and right = "101", score = 1 + 2 = 3 
+left = "0111" and right = "01", score = 1 + 1 = 2 
+left = "01110" and right = "1", score = 2 + 1 = 3
+
+ +

Example 2:

+ +
Input: s = "00111"
+Output: 5
+Explanation: When left = "00" and right = "111", we get the maximum score = 2 + 3 = 5
+
+ +

Example 3:

+ +
Input: s = "1111"
+Output: 3
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= s.length <= 500
  • +
  • The string s consists of characters '0' and '1' only.
  • +
+
\ No newline at end of file From 56d5147298f77db90966b476b32240b8033bf88b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 22 Dec 2023 08:53:39 +0530 Subject: [PATCH 0535/1399] Time: 4 ms (23.21%), Space: 6.9 MB (30.36%) - LeetHub --- ...maximum-score-after-splitting-a-string.cpp | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 1422-maximum-score-after-splitting-a-string/1422-maximum-score-after-splitting-a-string.cpp diff --git a/1422-maximum-score-after-splitting-a-string/1422-maximum-score-after-splitting-a-string.cpp b/1422-maximum-score-after-splitting-a-string/1422-maximum-score-after-splitting-a-string.cpp new file mode 100644 index 00000000..2617543b --- /dev/null +++ b/1422-maximum-score-after-splitting-a-string/1422-maximum-score-after-splitting-a-string.cpp @@ -0,0 +1,31 @@ +class Solution { +public: + int maxScore(string s) { + + int n = s.size(); + int ans = 0; + + vector suffOne(n, 0); + + suffOne[n-1] = (s[n-1] == '1' ? 1 : 0); + + for(int i = n-2; i >= 1; --i) + { + suffOne[i] = (s[i] == '1' ? suffOne[i+1]+1 : suffOne[i+1]); + + } + + int zeroCnt = 0; + + for(int i = 0; i < n; ++i) + { + if(s[i] == '0' and i != n-1) + ++zeroCnt; + + ans = max(ans, zeroCnt + suffOne[i]); + } + + return ans; + + } +}; \ No newline at end of file From 6483bc6723a858c97d6e444d3dd2ae0b5ed4f9a0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 22 Dec 2023 08:55:04 +0530 Subject: [PATCH 0536/1399] Attach NOTES - LeetHub --- 1422-maximum-score-after-splitting-a-string/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1422-maximum-score-after-splitting-a-string/NOTES.md diff --git a/1422-maximum-score-after-splitting-a-string/NOTES.md b/1422-maximum-score-after-splitting-a-string/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1422-maximum-score-after-splitting-a-string/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From f6c7b85043d73851d066843568e4b7915b2b82cf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 22 Dec 2023 08:55:07 +0530 Subject: [PATCH 0537/1399] Time: 4 ms (23.21%), Space: 6.9 MB (30.36%) - LeetHub From 9df0ffa098fa49f58a1baa009a22a26ee3d4330f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 22 Dec 2023 08:56:54 +0530 Subject: [PATCH 0538/1399] Attach NOTES - LeetHub From 6d35ed6f1824e984ddc9312d61d2bfc53d5b09ce Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 22 Dec 2023 08:56:58 +0530 Subject: [PATCH 0539/1399] Time: 3 ms (50.89%), Space: 6.6 MB (62.95%) - LeetHub --- ...maximum-score-after-splitting-a-string.cpp | 24 ++++++------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/1422-maximum-score-after-splitting-a-string/1422-maximum-score-after-splitting-a-string.cpp b/1422-maximum-score-after-splitting-a-string/1422-maximum-score-after-splitting-a-string.cpp index 2617543b..4aa0af90 100644 --- a/1422-maximum-score-after-splitting-a-string/1422-maximum-score-after-splitting-a-string.cpp +++ b/1422-maximum-score-after-splitting-a-string/1422-maximum-score-after-splitting-a-string.cpp @@ -3,29 +3,19 @@ class Solution { int maxScore(string s) { int n = s.size(); - int ans = 0; - vector suffOne(n, 0); + int ones = count(s.begin(), s.end(), '1'); + + int zero = 0, ans = 0; - suffOne[n-1] = (s[n-1] == '1' ? 1 : 0); - - for(int i = n-2; i >= 1; --i) + for(int i = 0; i < n-1; ++i) { - suffOne[i] = (s[i] == '1' ? suffOne[i+1]+1 : suffOne[i+1]); + if(s[i] == '0') ++zero; + else --ones; - } - - int zeroCnt = 0; - - for(int i = 0; i < n; ++i) - { - if(s[i] == '0' and i != n-1) - ++zeroCnt; - - ans = max(ans, zeroCnt + suffOne[i]); + ans = max(ans, zero + ones); } return ans; - } }; \ No newline at end of file From c01ccecc914d3e12f8026da43d6e24020a56500f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 22 Dec 2023 23:47:05 +0530 Subject: [PATCH 0540/1399] Create README - LeetHub --- 0091-decode-ways/README.md | 51 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 0091-decode-ways/README.md diff --git a/0091-decode-ways/README.md b/0091-decode-ways/README.md new file mode 100644 index 00000000..84e177d9 --- /dev/null +++ b/0091-decode-ways/README.md @@ -0,0 +1,51 @@ +

91. Decode Ways

Medium


A message containing letters from A-Z can be encoded into numbers using the following mapping:

+ +
'A' -> "1"
+'B' -> "2"
+...
+'Z' -> "26"
+
+ +

To decode an encoded message, all the digits must be grouped then mapped back into letters using the reverse of the mapping above (there may be multiple ways). For example, "11106" can be mapped into:

+ +
    +
  • "AAJF" with the grouping (1 1 10 6)
  • +
  • "KJF" with the grouping (11 10 6)
  • +
+ +

Note that the grouping (1 11 06) is invalid because "06" cannot be mapped into 'F' since "6" is different from "06".

+ +

Given a string s containing only digits, return the number of ways to decode it.

+ +

The test cases are generated so that the answer fits in a 32-bit integer.

+ +

 

+

Example 1:

+ +
Input: s = "12"
+Output: 2
+Explanation: "12" could be decoded as "AB" (1 2) or "L" (12).
+
+ +

Example 2:

+ +
Input: s = "226"
+Output: 3
+Explanation: "226" could be decoded as "BZ" (2 26), "VF" (22 6), or "BBF" (2 2 6).
+
+ +

Example 3:

+ +
Input: s = "06"
+Output: 0
+Explanation: "06" cannot be mapped to "F" because of the leading zero ("6" is different from "06").
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 100
  • +
  • s contains only digits and may contain leading zero(s).
  • +
+
\ No newline at end of file From 1efded292389aeab2c249ef2243863d65fadee89 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 22 Dec 2023 23:47:08 +0530 Subject: [PATCH 0541/1399] Time: 0 ms (100.00%), Space: 6.8 MB (29.13%) - LeetHub --- 0091-decode-ways/0091-decode-ways.cpp | 50 +++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 0091-decode-ways/0091-decode-ways.cpp diff --git a/0091-decode-ways/0091-decode-ways.cpp b/0091-decode-ways/0091-decode-ways.cpp new file mode 100644 index 00000000..149b5847 --- /dev/null +++ b/0091-decode-ways/0091-decode-ways.cpp @@ -0,0 +1,50 @@ +class Solution { +public: + + int helper(int idx, int n, string& str, vector& dp) + { + if(idx == n) + { + return 1; + } + + if(str[idx] == '0') + return 0; + + if(dp[idx] != -1) + return dp[idx]; + + int first = 0, second = 0; + + if(idx + 2 <= n) + { + string curr = str.substr(idx, 2); + + int num = stoi(curr); + + if(num >= 1 and num <= 26) + { + first = helper(idx+2, n, str, dp); + } + } + + int num = str[idx] - '0'; + + if(num >= 1 and num <= 26) + { + second = helper(idx+1, n, str, dp); + } + + return dp[idx] = first + second; + } + + int numDecodings(string s) { + + int n = s.size(); + + vector dp(n+1, -1); + + return helper(0, n, s, dp); + + } +}; \ No newline at end of file From 5ace66e435a3664b65a937f1f7fa6d2dbe2e7f83 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 23 Dec 2023 08:58:41 +0530 Subject: [PATCH 0542/1399] Create README - LeetHub --- 1496-path-crossing/README.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 1496-path-crossing/README.md diff --git a/1496-path-crossing/README.md b/1496-path-crossing/README.md new file mode 100644 index 00000000..71c6c3b5 --- /dev/null +++ b/1496-path-crossing/README.md @@ -0,0 +1,26 @@ +

1496. Path Crossing

Easy


Given a string path, where path[i] = 'N', 'S', 'E' or 'W', each representing moving one unit north, south, east, or west, respectively. You start at the origin (0, 0) on a 2D plane and walk on the path specified by path.

+ +

Return true if the path crosses itself at any point, that is, if at any time you are on a location you have previously visited. Return false otherwise.

+ +

 

+

Example 1:

+ +
Input: path = "NES"
+Output: false 
+Explanation: Notice that the path doesn't cross any point more than once.
+
+ +

Example 2:

+ +
Input: path = "NESWW"
+Output: true
+Explanation: Notice that the path visits the origin twice.
+ +

 

+

Constraints:

+ +
    +
  • 1 <= path.length <= 104
  • +
  • path[i] is either 'N', 'S', 'E', or 'W'.
  • +
+
\ No newline at end of file From 0d9be1411704c493203e260fe3d07f80a482bda6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 23 Dec 2023 08:58:44 +0530 Subject: [PATCH 0543/1399] Time: 0 ms (100.00%), Space: 7.1 MB (93.92%) - LeetHub --- 1496-path-crossing/1496-path-crossing.cpp | 30 +++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 1496-path-crossing/1496-path-crossing.cpp diff --git a/1496-path-crossing/1496-path-crossing.cpp b/1496-path-crossing/1496-path-crossing.cpp new file mode 100644 index 00000000..867b4685 --- /dev/null +++ b/1496-path-crossing/1496-path-crossing.cpp @@ -0,0 +1,30 @@ +class Solution { +public: + bool isPathCrossing(string path) { + + int n = path.size(); + + set> used; + + int x = 0, y = 0; + + used.insert({x, y}); + + for(int i = 0; i < n; ++i) + { + if(path[i] == 'N') + ++y; + else if(path[i] == 'E') + ++x; + else if(path[i] == 'S') + --y; + else + --x; + if(used.count({x, y})) + return true; + used.insert({x,y}); + } + + return false; + } +}; \ No newline at end of file From 3af949c9a2e5f64bb3df64245401ce13a16b56d2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 23 Dec 2023 08:59:15 +0530 Subject: [PATCH 0544/1399] Attach NOTES - LeetHub --- 1496-path-crossing/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1496-path-crossing/NOTES.md diff --git a/1496-path-crossing/NOTES.md b/1496-path-crossing/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1496-path-crossing/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 779a94a093e1373c52b53a9701f615b53c5c52b0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 23 Dec 2023 08:59:18 +0530 Subject: [PATCH 0545/1399] Time: 0 ms (100.00%), Space: 7.1 MB (93.92%) - LeetHub From c91318612b24b391704268e923c2d0c46dd43852 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 24 Dec 2023 14:52:21 +0530 Subject: [PATCH 0546/1399] Create README - LeetHub --- 0827-making-a-large-island/README.md | 36 ++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 0827-making-a-large-island/README.md diff --git a/0827-making-a-large-island/README.md b/0827-making-a-large-island/README.md new file mode 100644 index 00000000..930714d9 --- /dev/null +++ b/0827-making-a-large-island/README.md @@ -0,0 +1,36 @@ +

827. Making A Large Island

Hard


You are given an n x n binary matrix grid. You are allowed to change at most one 0 to be 1.

+ +

Return the size of the largest island in grid after applying this operation.

+ +

An island is a 4-directionally connected group of 1s.

+ +

 

+

Example 1:

+ +
Input: grid = [[1,0],[0,1]]
+Output: 3
+Explanation: Change one 0 to 1 and connect two 1s, then we get an island with area = 3.
+
+ +

Example 2:

+ +
Input: grid = [[1,1],[1,0]]
+Output: 4
+Explanation: Change the 0 to 1 and make the island bigger, only one island with area = 4.
+ +

Example 3:

+ +
Input: grid = [[1,1],[1,1]]
+Output: 4
+Explanation: Can't change any 0 to 1, only one island with area = 4.
+
+ +

 

+

Constraints:

+ +
    +
  • n == grid.length
  • +
  • n == grid[i].length
  • +
  • 1 <= n <= 500
  • +
  • grid[i][j] is either 0 or 1.
  • +
\ No newline at end of file From 0d4dd20fb9f28422d07c2b89ba1bf9d20606c696 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 24 Dec 2023 14:52:21 +0530 Subject: [PATCH 0547/1399] Attach NOTES - LeetHub --- 0827-making-a-large-island/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0827-making-a-large-island/NOTES.md diff --git a/0827-making-a-large-island/NOTES.md b/0827-making-a-large-island/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0827-making-a-large-island/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From f7c8f0d2867d6bb33718d1a7736f56b6f30b18ad Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 24 Dec 2023 14:52:25 +0530 Subject: [PATCH 0548/1399] Time: 371 ms (64.16%), Space: 93.5 MB (95.28%) - LeetHub --- .../0827-making-a-large-island.cpp | 145 ++++++++++++++++++ 1 file changed, 145 insertions(+) create mode 100644 0827-making-a-large-island/0827-making-a-large-island.cpp diff --git a/0827-making-a-large-island/0827-making-a-large-island.cpp b/0827-making-a-large-island/0827-making-a-large-island.cpp new file mode 100644 index 00000000..79b5e3d4 --- /dev/null +++ b/0827-making-a-large-island/0827-making-a-large-island.cpp @@ -0,0 +1,145 @@ +class Solution { +public: + + class DSU{ + public: + vector parent, size, rank; + public: + DSU(int n) + { + parent.resize(n+1); + size.resize(n+1, 1); + rank.resize(n+1, 0); + + for(int i = 0; i <= n; ++i) + parent[i] = i; + } + + int findParent(int u) + { + if(u == parent[u]) + return u; + return parent[u] = findParent(parent[u]); + } + + void unionByRank(int u, int v) + { + int parU = findParent(u); + int parV = findParent(v); + + if(parU == parV) + return; + + if(rank[parU] < rank[parV]) + parent[parU] = parV; + else if(rank[parV] > rank[parU]) + parent[parV] = parU; + else + { + parent[parV] = parU; + ++rank[parU]; + } + } + + void unionBySize(int u, int v) + { + int parU = findParent(u); + int parV = findParent(v); + + if(parU == parV) + return; + + if(size[parU] < size[parV]) + { + parent[parU] = parV; + size[parV] += size[parU]; + } + else + { + parent[parV] = parU; + size[parU] += size[parV]; + } + } + + bool isSame(int u, int v) + { + return findParent(u) == findParent(v); + } + }; + + int largestIsland(vector>& grid) { + + int n = grid.size(); + int m = grid[0].size(); + + DSU dsu((n*m) + 1); + + vector dx = {-1, 0, +1, 0}; + vector dy = {0, -1, 0, +1}; + + auto isValid = [&](int x, int y) + { + return (x >= 0 and y >= 0 and x < n and y < m); + }; + + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < n; ++j) + { + if(grid[i][j] == 0) + continue; + + for(int k = 0; k < 4; ++k) + { + int newx = dx[k] + i; + int newy = dy[k] + j; + + if(isValid(newx, newy) and grid[newx][newy]) + { + int node = (i * m) + j; + int adjNode = (newx * m) + newy; + + if(!dsu.isSame(node, adjNode)) + dsu.unionBySize(node, adjNode); + } + } + } + } + + int maxi = -1; + + unordered_set visited; + + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < n; ++j) + { + if(grid[i][j] == 0) + { + for(int k = 0; k < 4; ++k) + { + int newx = dx[k] + i; + int newy = dy[k] + j; + + if(isValid(newx, newy) and grid[newx][newy]) + { + visited.insert(dsu.findParent((newx*m) + newy)); + } + } + + int curr = 0; + + for(auto& ele : visited) + curr += dsu.size[ele]; + + visited.clear(); + + maxi = max(maxi, curr + 1); + } + } + } + + + return (maxi == -1 ? (n * m) : maxi); + } +}; \ No newline at end of file From 7fa4b6fdc06c39960092ae611159575e7730dcd6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 24 Dec 2023 22:29:26 +0530 Subject: [PATCH 0549/1399] Create README - LeetHub --- .../README.md | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 1758-minimum-changes-to-make-alternating-binary-string/README.md diff --git a/1758-minimum-changes-to-make-alternating-binary-string/README.md b/1758-minimum-changes-to-make-alternating-binary-string/README.md new file mode 100644 index 00000000..960678e9 --- /dev/null +++ b/1758-minimum-changes-to-make-alternating-binary-string/README.md @@ -0,0 +1,36 @@ +

1758. Minimum Changes To Make Alternating Binary String

Easy


You are given a string s consisting only of the characters '0' and '1'. In one operation, you can change any '0' to '1' or vice versa.

+ +

The string is called alternating if no two adjacent characters are equal. For example, the string "010" is alternating, while the string "0100" is not.

+ +

Return the minimum number of operations needed to make s alternating.

+ +

 

+

Example 1:

+ +
Input: s = "0100"
+Output: 1
+Explanation: If you change the last character to '1', s will be "0101", which is alternating.
+
+ +

Example 2:

+ +
Input: s = "10"
+Output: 0
+Explanation: s is already alternating.
+
+ +

Example 3:

+ +
Input: s = "1111"
+Output: 2
+Explanation: You need two operations to reach "0101" or "1010".
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 104
  • +
  • s[i] is either '0' or '1'.
  • +
+
\ No newline at end of file From 723ce98654e40ab9767d6e7699f88549806bd07a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 24 Dec 2023 22:29:30 +0530 Subject: [PATCH 0550/1399] Time: 6 ms (35.61%), Space: 7.2 MB (86.65%) - LeetHub --- ...nges-to-make-alternating-binary-string.cpp | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 1758-minimum-changes-to-make-alternating-binary-string/1758-minimum-changes-to-make-alternating-binary-string.cpp diff --git a/1758-minimum-changes-to-make-alternating-binary-string/1758-minimum-changes-to-make-alternating-binary-string.cpp b/1758-minimum-changes-to-make-alternating-binary-string/1758-minimum-changes-to-make-alternating-binary-string.cpp new file mode 100644 index 00000000..98bd5d13 --- /dev/null +++ b/1758-minimum-changes-to-make-alternating-binary-string/1758-minimum-changes-to-make-alternating-binary-string.cpp @@ -0,0 +1,25 @@ +class Solution { +public: + int minOperations(string s) { + + int zcnt = 0, ocnt = 0; + for(int i = 0; i Date: Mon, 25 Dec 2023 08:31:24 +0530 Subject: [PATCH 0551/1399] Attach NOTES - LeetHub --- 1758-minimum-changes-to-make-alternating-binary-string/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1758-minimum-changes-to-make-alternating-binary-string/NOTES.md diff --git a/1758-minimum-changes-to-make-alternating-binary-string/NOTES.md b/1758-minimum-changes-to-make-alternating-binary-string/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1758-minimum-changes-to-make-alternating-binary-string/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From e841248cb4e483ae7c9ed82c7b1fc450112baa07 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 25 Dec 2023 08:31:26 +0530 Subject: [PATCH 0552/1399] Time: 9 ms (8.38%), Space: 8.2 MB (5.99%) - LeetHub --- ...nges-to-make-alternating-binary-string.cpp | 39 +++++++++++-------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/1758-minimum-changes-to-make-alternating-binary-string/1758-minimum-changes-to-make-alternating-binary-string.cpp b/1758-minimum-changes-to-make-alternating-binary-string/1758-minimum-changes-to-make-alternating-binary-string.cpp index 98bd5d13..636a953e 100644 --- a/1758-minimum-changes-to-make-alternating-binary-string/1758-minimum-changes-to-make-alternating-binary-string.cpp +++ b/1758-minimum-changes-to-make-alternating-binary-string/1758-minimum-changes-to-make-alternating-binary-string.cpp @@ -1,25 +1,30 @@ class Solution { public: int minOperations(string s) { - - int zcnt = 0, ocnt = 0; - for(int i = 0; i Date: Mon, 25 Dec 2023 11:47:34 +0530 Subject: [PATCH 0553/1399] Create README - LeetHub --- 0069-sqrtx/README.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 0069-sqrtx/README.md diff --git a/0069-sqrtx/README.md b/0069-sqrtx/README.md new file mode 100644 index 00000000..3b33c2e0 --- /dev/null +++ b/0069-sqrtx/README.md @@ -0,0 +1,30 @@ +

69. Sqrt(x)

Easy


Given a non-negative integer x, return the square root of x rounded down to the nearest integer. The returned integer should be non-negative as well.

+ +

You must not use any built-in exponent function or operator.

+ +
    +
  • For example, do not use pow(x, 0.5) in c++ or x ** 0.5 in python.
  • +
+ +

 

+

Example 1:

+ +
Input: x = 4
+Output: 2
+Explanation: The square root of 4 is 2, so we return 2.
+
+ +

Example 2:

+ +
Input: x = 8
+Output: 2
+Explanation: The square root of 8 is 2.82842..., and since we round it down to the nearest integer, 2 is returned.
+
+ +

 

+

Constraints:

+ +
    +
  • 0 <= x <= 231 - 1
  • +
+
\ No newline at end of file From 10f7851609b9b0d9f247e4ed04ae0e0d6a479abb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 25 Dec 2023 11:47:37 +0530 Subject: [PATCH 0554/1399] Time: 0 ms (100.00%), Space: 6.3 MB (73.43%) - LeetHub --- 0069-sqrtx/0069-sqrtx.cpp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 0069-sqrtx/0069-sqrtx.cpp diff --git a/0069-sqrtx/0069-sqrtx.cpp b/0069-sqrtx/0069-sqrtx.cpp new file mode 100644 index 00000000..e22f88c4 --- /dev/null +++ b/0069-sqrtx/0069-sqrtx.cpp @@ -0,0 +1,29 @@ +class Solution { +public: + int mySqrt(int x) { + + long long start = 1, end = x; + + int ans = 0; + + while(start <= end) + { + long long mid = (start + end) >> 1; + + long long val = mid * 1LL * mid; + + if(val <= x) + { + ans = mid; + start = mid+1; + } + else + { + end = mid-1; + } + } + + return ans; + + } +}; \ No newline at end of file From a1a8dcc678a9a6c1d63a1aca919ae3db835fd6bd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 25 Dec 2023 12:03:57 +0530 Subject: [PATCH 0555/1399] Attach NOTES - LeetHub --- 0069-sqrtx/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0069-sqrtx/NOTES.md diff --git a/0069-sqrtx/NOTES.md b/0069-sqrtx/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0069-sqrtx/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 3000740e10c72a19a33726b01453830837a4dfdd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 25 Dec 2023 12:04:00 +0530 Subject: [PATCH 0556/1399] Time: 0 ms (100.00%), Space: 6.3 MB (73.43%) - LeetHub From d5bef42a09df9caaccae1acbdfe025f48334d31b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 25 Dec 2023 12:05:28 +0530 Subject: [PATCH 0557/1399] Attach NOTES - LeetHub From e269a31e17e795f01a39dce47eabca69e7b13b04 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 25 Dec 2023 12:05:31 +0530 Subject: [PATCH 0558/1399] Time: 0 ms (100.00%), Space: 6.3 MB (73.43%) - LeetHub From 83955a84c7c8343e725950153cfcd8a01ce19681 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 25 Dec 2023 22:54:34 +0530 Subject: [PATCH 0559/1399] Attach NOTES - LeetHub --- 0091-decode-ways/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0091-decode-ways/NOTES.md diff --git a/0091-decode-ways/NOTES.md b/0091-decode-ways/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0091-decode-ways/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 9b99901da3a5675bd912eabc3da67ca2bf92ca43 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 25 Dec 2023 22:54:36 +0530 Subject: [PATCH 0560/1399] Time: 0 ms (100.00%), Space: 6.5 MB (65.44%) - LeetHub --- 0091-decode-ways/0091-decode-ways.cpp | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/0091-decode-ways/0091-decode-ways.cpp b/0091-decode-ways/0091-decode-ways.cpp index 149b5847..ce5333e9 100644 --- a/0091-decode-ways/0091-decode-ways.cpp +++ b/0091-decode-ways/0091-decode-ways.cpp @@ -1,14 +1,12 @@ class Solution { public: - int helper(int idx, int n, string& str, vector& dp) - { + int helper(int idx, int n, string& s, vector& dp) + { if(idx == n) - { return 1; - } - if(str[idx] == '0') + if(s[idx] == '0') return 0; if(dp[idx] != -1) @@ -18,22 +16,18 @@ class Solution { if(idx + 2 <= n) { - string curr = str.substr(idx, 2); - - int num = stoi(curr); + string curr = s.substr(idx, 2); - if(num >= 1 and num <= 26) + if(stoi(curr) >= 1 and stoi(curr) <= 26) { - first = helper(idx+2, n, str, dp); + first = helper(idx + 2, n, s, dp); } } - int num = str[idx] - '0'; + int ch = s[idx] - '0'; - if(num >= 1 and num <= 26) - { - second = helper(idx+1, n, str, dp); - } + if(ch >= 1 and ch <= 26) + second = helper(idx + 1,n, s, dp); return dp[idx] = first + second; } From 19ab4d74e5eb045f27bddfe9bd27f70375315d41 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 26 Dec 2023 07:54:26 +0530 Subject: [PATCH 0561/1399] Create README - LeetHub --- .../README.md | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 1155-number-of-dice-rolls-with-target-sum/README.md diff --git a/1155-number-of-dice-rolls-with-target-sum/README.md b/1155-number-of-dice-rolls-with-target-sum/README.md new file mode 100644 index 00000000..19dd039a --- /dev/null +++ b/1155-number-of-dice-rolls-with-target-sum/README.md @@ -0,0 +1,36 @@ +

1155. Number of Dice Rolls With Target Sum

Medium


You have n dice, and each die has k faces numbered from 1 to k.

+ +

Given three integers n, k, and target, return the number of possible ways (out of the kn total ways) to roll the dice, so the sum of the face-up numbers equals target. Since the answer may be too large, return it modulo 109 + 7.

+ +

 

+

Example 1:

+ +
Input: n = 1, k = 6, target = 3
+Output: 1
+Explanation: You throw one die with 6 faces.
+There is only one way to get a sum of 3.
+
+ +

Example 2:

+ +
Input: n = 2, k = 6, target = 7
+Output: 6
+Explanation: You throw two dice, each with 6 faces.
+There are 6 ways to get a sum of 7: 1+6, 2+5, 3+4, 4+3, 5+2, 6+1.
+
+ +

Example 3:

+ +
Input: n = 30, k = 30, target = 500
+Output: 222616187
+Explanation: The answer must be returned modulo 109 + 7.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n, k <= 30
  • +
  • 1 <= target <= 1000
  • +
+
\ No newline at end of file From e7ea87ec50b884896a9f48763ad85ee86df825ac Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 26 Dec 2023 07:54:26 +0530 Subject: [PATCH 0562/1399] Attach NOTES - LeetHub --- 1155-number-of-dice-rolls-with-target-sum/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1155-number-of-dice-rolls-with-target-sum/NOTES.md diff --git a/1155-number-of-dice-rolls-with-target-sum/NOTES.md b/1155-number-of-dice-rolls-with-target-sum/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1155-number-of-dice-rolls-with-target-sum/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 0ba46f5884141433cd66c9c8bfdc86c14752e46d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 26 Dec 2023 07:54:30 +0530 Subject: [PATCH 0563/1399] Time: 26 ms (53.55%), Space: 9.1 MB (29.50%) - LeetHub --- ...5-number-of-dice-rolls-with-target-sum.cpp | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 1155-number-of-dice-rolls-with-target-sum/1155-number-of-dice-rolls-with-target-sum.cpp diff --git a/1155-number-of-dice-rolls-with-target-sum/1155-number-of-dice-rolls-with-target-sum.cpp b/1155-number-of-dice-rolls-with-target-sum/1155-number-of-dice-rolls-with-target-sum.cpp new file mode 100644 index 00000000..57738d20 --- /dev/null +++ b/1155-number-of-dice-rolls-with-target-sum/1155-number-of-dice-rolls-with-target-sum.cpp @@ -0,0 +1,32 @@ +class Solution { +public: + + const int mod = 1e9+7; + + int helper(int idx, int n, int k, int target, vector>& dp) + { + if(idx == n) + return (target == 0); + + if(dp[idx][target] != -1) + return dp[idx][target]; + + int ways = 0; + + for(int i = 1; i <= k; ++i) + { + if(i <= target) + ways = (ways + helper(idx+1, n, k, target - i, dp))%mod; + + } + + return dp[idx][target] = ways; + } + + int numRollsToTarget(int n, int k, int target) { + + vector> dp(n+1, vector(target+1, -1)); + + return helper(0, n, k, target, dp); + } +}; \ No newline at end of file From 56cc286d4699db36ee337647f7fab491704173ed Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 26 Dec 2023 07:55:11 +0530 Subject: [PATCH 0564/1399] Attach NOTES - LeetHub From cd7b8eb5349651435a48c6576666898885e0bf59 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 26 Dec 2023 07:55:14 +0530 Subject: [PATCH 0565/1399] Time: 26 ms (53.55%), Space: 9.1 MB (29.50%) - LeetHub From ed14d60d2eb483b41be9ae8fabd3d86f2f797a8e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 26 Dec 2023 07:58:38 +0530 Subject: [PATCH 0566/1399] Attach NOTES - LeetHub From c50057d67dba69ef5141c2ceba163632d457159b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 26 Dec 2023 07:58:42 +0530 Subject: [PATCH 0567/1399] Time: 26 ms (53.55%), Space: 9.1 MB (29.50%) - LeetHub From bbc480af98f167a8f6756770a3e31e53172b138a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 26 Dec 2023 18:35:28 +0530 Subject: [PATCH 0568/1399] Attach NOTES - LeetHub From ea20c4336f44b82034a662012e50851705077f11 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 26 Dec 2023 18:35:31 +0530 Subject: [PATCH 0569/1399] Time: 73 ms (13.06%), Space: 8.8 MB (65.29%) - LeetHub --- ...5-number-of-dice-rolls-with-target-sum.cpp | 36 ++++++++----------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/1155-number-of-dice-rolls-with-target-sum/1155-number-of-dice-rolls-with-target-sum.cpp b/1155-number-of-dice-rolls-with-target-sum/1155-number-of-dice-rolls-with-target-sum.cpp index 57738d20..af9c24b2 100644 --- a/1155-number-of-dice-rolls-with-target-sum/1155-number-of-dice-rolls-with-target-sum.cpp +++ b/1155-number-of-dice-rolls-with-target-sum/1155-number-of-dice-rolls-with-target-sum.cpp @@ -1,32 +1,26 @@ class Solution { public: - - const int mod = 1e9+7; - - int helper(int idx, int n, int k, int target, vector>& dp) - { - if(idx == n) - return (target == 0); + int numRollsToTarget(int n, int k, int target) { + + const int mod = 1e9+7; - if(dp[idx][target] != -1) - return dp[idx][target]; + vector> dp(n+1, vector(target+1, 0)); - int ways = 0; + dp[0][0] = 1; - for(int i = 1; i <= k; ++i) + for(int i = 1; i <= n; ++i) { - if(i <= target) - ways = (ways + helper(idx+1, n, k, target - i, dp))%mod; - + for(int j = 1; j <= target; ++j) + { + for(int f = 1; f <= k; ++f) + { + if(j - f >= 0) + dp[i][j] = (dp[i][j] + dp[i-1][j-f]) % mod; + } + } } - return dp[idx][target] = ways; - } - - int numRollsToTarget(int n, int k, int target) { + return dp[n][target]; - vector> dp(n+1, vector(target+1, -1)); - - return helper(0, n, k, target, dp); } }; \ No newline at end of file From c2d1f26fa623b0598fc3eebdc607e28749b90b00 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 27 Dec 2023 19:03:23 +0530 Subject: [PATCH 0570/1399] Create README - LeetHub --- .../README.md | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 1578-minimum-time-to-make-rope-colorful/README.md diff --git a/1578-minimum-time-to-make-rope-colorful/README.md b/1578-minimum-time-to-make-rope-colorful/README.md new file mode 100644 index 00000000..6e5e854a --- /dev/null +++ b/1578-minimum-time-to-make-rope-colorful/README.md @@ -0,0 +1,40 @@ +

1578. Minimum Time to Make Rope Colorful

Medium


Alice has n balloons arranged on a rope. You are given a 0-indexed string colors where colors[i] is the color of the ith balloon.

+ +

Alice wants the rope to be colorful. She does not want two consecutive balloons to be of the same color, so she asks Bob for help. Bob can remove some balloons from the rope to make it colorful. You are given a 0-indexed integer array neededTime where neededTime[i] is the time (in seconds) that Bob needs to remove the ith balloon from the rope.

+ +

Return the minimum time Bob needs to make the rope colorful.

+ +

 

+

Example 1:

+ +
Input: colors = "abaac", neededTime = [1,2,3,4,5]
+Output: 3
+Explanation: In the above image, 'a' is blue, 'b' is red, and 'c' is green.
+Bob can remove the blue balloon at index 2. This takes 3 seconds.
+There are no longer two consecutive balloons of the same color. Total time = 3.
+ +

Example 2:

+ +
Input: colors = "abc", neededTime = [1,2,3]
+Output: 0
+Explanation: The rope is already colorful. Bob does not need to remove any balloons from the rope.
+
+ +

Example 3:

+ +
Input: colors = "aabaa", neededTime = [1,2,3,4,1]
+Output: 2
+Explanation: Bob will remove the ballons at indices 0 and 4. Each ballon takes 1 second to remove.
+There are no longer two consecutive balloons of the same color. Total time = 1 + 1 = 2.
+
+ +

 

+

Constraints:

+ +
    +
  • n == colors.length == neededTime.length
  • +
  • 1 <= n <= 105
  • +
  • 1 <= neededTime[i] <= 104
  • +
  • colors contains only lowercase English letters.
  • +
+
\ No newline at end of file From 581c53e79ccd01a1bfc731edff8084e26c14341a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 27 Dec 2023 19:03:24 +0530 Subject: [PATCH 0571/1399] Attach NOTES - LeetHub --- 1578-minimum-time-to-make-rope-colorful/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1578-minimum-time-to-make-rope-colorful/NOTES.md diff --git a/1578-minimum-time-to-make-rope-colorful/NOTES.md b/1578-minimum-time-to-make-rope-colorful/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1578-minimum-time-to-make-rope-colorful/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 4660a4b63c523b33f60dc55d942af9adab37653c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 27 Dec 2023 19:03:27 +0530 Subject: [PATCH 0572/1399] Time: 118 ms (61.10%), Space: 95.7 MB (49.48%) - LeetHub --- ...578-minimum-time-to-make-rope-colorful.cpp | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 1578-minimum-time-to-make-rope-colorful/1578-minimum-time-to-make-rope-colorful.cpp diff --git a/1578-minimum-time-to-make-rope-colorful/1578-minimum-time-to-make-rope-colorful.cpp b/1578-minimum-time-to-make-rope-colorful/1578-minimum-time-to-make-rope-colorful.cpp new file mode 100644 index 00000000..7af1455a --- /dev/null +++ b/1578-minimum-time-to-make-rope-colorful/1578-minimum-time-to-make-rope-colorful.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + int minCost(string colors, vector& neededTime) { + + int n = colors.size(); + + int ans = 0; + + for(int i = 0; i < n; ++i) + { + int cost = neededTime[i], maxSegmentCost = neededTime[i]; + + while(i+1 < n and colors[i] == colors[i+1]) + { + cost += neededTime[i+1]; + maxSegmentCost = max(maxSegmentCost, neededTime[i+1]); + ++i; + } + + cost -= maxSegmentCost; + + ans += cost; + } + + return ans; + } +}; \ No newline at end of file From 2a84bc35e58f64003b16b95fbff8858db1031a86 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 27 Dec 2023 19:05:33 +0530 Subject: [PATCH 0573/1399] Attach NOTES - LeetHub From aee7fc5a67ab3c3360079ee446ef8400ed1969f5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 27 Dec 2023 19:05:36 +0530 Subject: [PATCH 0574/1399] Time: 118 ms (61.10%), Space: 95.7 MB (49.48%) - LeetHub From 5ec03a16bd7b7653adbf23402b8299f6602b7a4d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 29 Dec 2023 01:29:49 +0530 Subject: [PATCH 0575/1399] Create README - LeetHub --- 1531-string-compression-ii/README.md | 38 ++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 1531-string-compression-ii/README.md diff --git a/1531-string-compression-ii/README.md b/1531-string-compression-ii/README.md new file mode 100644 index 00000000..74ed32f7 --- /dev/null +++ b/1531-string-compression-ii/README.md @@ -0,0 +1,38 @@ +

1531. String Compression II

Hard


Run-length encoding is a string compression method that works by replacing consecutive identical characters (repeated 2 or more times) with the concatenation of the character and the number marking the count of the characters (length of the run). For example, to compress the string "aabccc" we replace "aa" by "a2" and replace "ccc" by "c3". Thus the compressed string becomes "a2bc3".

+ +

Notice that in this problem, we are not adding '1' after single characters.

+ +

Given a string s and an integer k. You need to delete at most k characters from s such that the run-length encoded version of s has minimum length.

+ +

Find the minimum length of the run-length encoded version of s after deleting at most k characters.

+ +

 

+

Example 1:

+ +
Input: s = "aaabcccd", k = 2
+Output: 4
+Explanation: Compressing s without deleting anything will give us "a3bc3d" of length 6. Deleting any of the characters 'a' or 'c' would at most decrease the length of the compressed string to 5, for instance delete 2 'a' then we will have s = "abcccd" which compressed is abc3d. Therefore, the optimal way is to delete 'b' and 'd', then the compressed version of s will be "a3c3" of length 4.
+ +

Example 2:

+ +
Input: s = "aabbaa", k = 2
+Output: 2
+Explanation: If we delete both 'b' characters, the resulting compressed string would be "a4" of length 2.
+
+ +

Example 3:

+ +
Input: s = "aaaaaaaaaaa", k = 0
+Output: 3
+Explanation: Since k is zero, we cannot delete anything. The compressed string is "a11" of length 3.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 100
  • +
  • 0 <= k <= s.length
  • +
  • s contains only lowercase English letters.
  • +
+
\ No newline at end of file From d97d7a8bef3f4776c547077eee234f04dfa3c256 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 29 Dec 2023 01:29:53 +0530 Subject: [PATCH 0576/1399] Time: 26 ms (87.64%), Space: 6.6 MB (92.13%) - LeetHub --- .../1531-string-compression-ii.cpp | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 1531-string-compression-ii/1531-string-compression-ii.cpp diff --git a/1531-string-compression-ii/1531-string-compression-ii.cpp b/1531-string-compression-ii/1531-string-compression-ii.cpp new file mode 100644 index 00000000..a8c534fa --- /dev/null +++ b/1531-string-compression-ii/1531-string-compression-ii.cpp @@ -0,0 +1,22 @@ +class Solution { +public: + int dp[101][101]; + int dfs(string &s, int left, int K) { + int k = K; + if(s.size() - left <= k) return 0; + if(dp[left][k] >= 0) return dp[left][k]; + int res = k ? dfs(s, left + 1, k - 1) : 10000, c = 1; + for(int i = left + 1; i <= s.size(); ++i) { + res = min(res, dfs(s, i, k) + 1 + (c >= 100 ? 3 : (c >= 10 ? 2 : (c > 1 ? 1 :0)))); + if(i == s.size()) break; + if(s[i] == s[left]) ++c; + else if(--k < 0) break; + } + return dp[left][K] = res; + } + + int getLengthOfOptimalCompression(string s, int k) { + memset(dp, -1, sizeof(dp)); + return dfs(s, 0, k); + } +}; From 700f1d3a33737e449e231b0c9800f96f3180780f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 29 Dec 2023 22:39:09 +0530 Subject: [PATCH 0577/1399] Create README - LeetHub --- .../README.md | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 1335-minimum-difficulty-of-a-job-schedule/README.md diff --git a/1335-minimum-difficulty-of-a-job-schedule/README.md b/1335-minimum-difficulty-of-a-job-schedule/README.md new file mode 100644 index 00000000..2c1f6130 --- /dev/null +++ b/1335-minimum-difficulty-of-a-job-schedule/README.md @@ -0,0 +1,41 @@ +

1335. Minimum Difficulty of a Job Schedule

Hard


You want to schedule a list of jobs in d days. Jobs are dependent (i.e To work on the ith job, you have to finish all the jobs j where 0 <= j < i).

+ +

You have to finish at least one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the d days. The difficulty of a day is the maximum difficulty of a job done on that day.

+ +

You are given an integer array jobDifficulty and an integer d. The difficulty of the ith job is jobDifficulty[i].

+ +

Return the minimum difficulty of a job schedule. If you cannot find a schedule for the jobs return -1.

+ +

 

+

Example 1:

+ +
Input: jobDifficulty = [6,5,4,3,2,1], d = 2
+Output: 7
+Explanation: First day you can finish the first 5 jobs, total difficulty = 6.
+Second day you can finish the last job, total difficulty = 1.
+The difficulty of the schedule = 6 + 1 = 7 
+
+ +

Example 2:

+ +
Input: jobDifficulty = [9,9,9], d = 4
+Output: -1
+Explanation: If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs.
+
+ +

Example 3:

+ +
Input: jobDifficulty = [1,1,1], d = 3
+Output: 3
+Explanation: The schedule is one job per day. total difficulty will be 3.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= jobDifficulty.length <= 300
  • +
  • 0 <= jobDifficulty[i] <= 1000
  • +
  • 1 <= d <= 10
  • +
+
\ No newline at end of file From 750f69e6bc3394b6992a998a1cf713d49e8d910b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 29 Dec 2023 22:39:12 +0530 Subject: [PATCH 0578/1399] Time: 69 ms (32.29%), Space: 7.6 MB (78.07%) - LeetHub --- ...5-minimum-difficulty-of-a-job-schedule.cpp | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 1335-minimum-difficulty-of-a-job-schedule/1335-minimum-difficulty-of-a-job-schedule.cpp diff --git a/1335-minimum-difficulty-of-a-job-schedule/1335-minimum-difficulty-of-a-job-schedule.cpp b/1335-minimum-difficulty-of-a-job-schedule/1335-minimum-difficulty-of-a-job-schedule.cpp new file mode 100644 index 00000000..8f18eb59 --- /dev/null +++ b/1335-minimum-difficulty-of-a-job-schedule/1335-minimum-difficulty-of-a-job-schedule.cpp @@ -0,0 +1,29 @@ +class Solution { +public: + int t[301][11]; + int solve(vector& mat, int n, int idx, int d) { + if(d == 1) + return *max_element(begin(mat)+idx, end(mat)); + + if(t[idx][d] != -1) + return t[idx][d]; + + + int Max = INT_MIN; + int result = INT_MAX; + + for(int i = idx; i<=n-d; i++) { + Max = max(Max, mat[i]); + result = min(result, Max + solve(mat, n, i+1, d-1)); + } + return t[idx][d] = result; + } + + int minDifficulty(vector& jobDifficulty, int d) { + int n = jobDifficulty.size(); + if(n < d) + return -1; + memset(t, -1, sizeof(t)); + return solve(jobDifficulty, n, 0, d); + } +}; \ No newline at end of file From 7ea3fea6257832205d07f455b186102c60a8e700 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 30 Dec 2023 09:28:18 +0530 Subject: [PATCH 0579/1399] Create README - LeetHub --- .../README.md | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 1897-redistribute-characters-to-make-all-strings-equal/README.md diff --git a/1897-redistribute-characters-to-make-all-strings-equal/README.md b/1897-redistribute-characters-to-make-all-strings-equal/README.md new file mode 100644 index 00000000..a493914d --- /dev/null +++ b/1897-redistribute-characters-to-make-all-strings-equal/README.md @@ -0,0 +1,32 @@ +

1897. Redistribute Characters to Make All Strings Equal

Easy


You are given an array of strings words (0-indexed).

+ +

In one operation, pick two distinct indices i and j, where words[i] is a non-empty string, and move any character from words[i] to any position in words[j].

+ +

Return true if you can make every string in words equal using any number of operations, and false otherwise.

+ +

 

+

Example 1:

+ +
Input: words = ["abc","aabc","bc"]
+Output: true
+Explanation: Move the first 'a' in words[1] to the front of words[2],
+to make words[1] = "abc" and words[2] = "abc".
+All the strings are now equal to "abc", so return true.
+
+ +

Example 2:

+ +
Input: words = ["ab","a"]
+Output: false
+Explanation: It is impossible to make all the strings equal using the operation.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= words.length <= 100
  • +
  • 1 <= words[i].length <= 100
  • +
  • words[i] consists of lowercase English letters.
  • +
+
\ No newline at end of file From 7a1fe94b34abd26322a41e98af66ba78ade8d098 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 30 Dec 2023 09:28:19 +0530 Subject: [PATCH 0580/1399] Attach NOTES - LeetHub --- 1897-redistribute-characters-to-make-all-strings-equal/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1897-redistribute-characters-to-make-all-strings-equal/NOTES.md diff --git a/1897-redistribute-characters-to-make-all-strings-equal/NOTES.md b/1897-redistribute-characters-to-make-all-strings-equal/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1897-redistribute-characters-to-make-all-strings-equal/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From b7a3ad77bce828038e9603aaf754a2d48893e10d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 30 Dec 2023 09:28:22 +0530 Subject: [PATCH 0581/1399] Time: 16 ms (47.86%), Space: 12.1 MB (68.09%) - LeetHub --- ...e-characters-to-make-all-strings-equal.cpp | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 1897-redistribute-characters-to-make-all-strings-equal/1897-redistribute-characters-to-make-all-strings-equal.cpp diff --git a/1897-redistribute-characters-to-make-all-strings-equal/1897-redistribute-characters-to-make-all-strings-equal.cpp b/1897-redistribute-characters-to-make-all-strings-equal/1897-redistribute-characters-to-make-all-strings-equal.cpp new file mode 100644 index 00000000..38a9b341 --- /dev/null +++ b/1897-redistribute-characters-to-make-all-strings-equal/1897-redistribute-characters-to-make-all-strings-equal.cpp @@ -0,0 +1,29 @@ +class Solution { +public: + bool makeEqual(vector& words) { + + int n = words.size(); + + bool ok = true; + + vector freq(26, 0); + + for(auto& word : words) + { + for(auto& ch : word) + ++freq[ch-'a']; + + } + + for(auto& f : freq) + { + if(f and f % n != 0) + { + ok = false; + break; + } + } + + return ok; + } +}; \ No newline at end of file From b42f75a1b759e19650288725752c80470a68ae03 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 31 Dec 2023 13:21:01 +0530 Subject: [PATCH 0582/1399] Create README - LeetHub --- .../README.md | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 2982-find-longest-special-substring-that-occurs-thrice-ii/README.md diff --git a/2982-find-longest-special-substring-that-occurs-thrice-ii/README.md b/2982-find-longest-special-substring-that-occurs-thrice-ii/README.md new file mode 100644 index 00000000..2a1c8241 --- /dev/null +++ b/2982-find-longest-special-substring-that-occurs-thrice-ii/README.md @@ -0,0 +1,40 @@ +

2982. Find Longest Special Substring That Occurs Thrice II

Medium


You are given a string s that consists of lowercase English letters.

+ +

A string is called special if it is made up of only a single character. For example, the string "abc" is not special, whereas the strings "ddd", "zz", and "f" are special.

+ +

Return the length of the longest special substring of s which occurs at least thrice, or -1 if no special substring occurs at least thrice.

+ +

A substring is a contiguous non-empty sequence of characters within a string.

+ +

 

+

Example 1:

+ +
Input: s = "aaaa"
+Output: 2
+Explanation: The longest special substring which occurs thrice is "aa": substrings "aaaa", "aaaa", and "aaaa".
+It can be shown that the maximum length achievable is 2.
+
+ +

Example 2:

+ +
Input: s = "abcdef"
+Output: -1
+Explanation: There exists no special substring which occurs at least thrice. Hence return -1.
+
+ +

Example 3:

+ +
Input: s = "abcaba"
+Output: 1
+Explanation: The longest special substring which occurs thrice is "a": substrings "abcaba", "abcaba", and "abcaba".
+It can be shown that the maximum length achievable is 1.
+
+ +

 

+

Constraints:

+ +
    +
  • 3 <= s.length <= 5 * 105
  • +
  • s consists of only lowercase English letters.
  • +
+
\ No newline at end of file From c390d27298aa170981cd7498c23e957b4e55c19f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 31 Dec 2023 13:21:05 +0530 Subject: [PATCH 0583/1399] Time: 2014 ms (20.00%), Space: 269.2 MB (80.00%) - LeetHub --- ...pecial-substring-that-occurs-thrice-ii.cpp | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 2982-find-longest-special-substring-that-occurs-thrice-ii/2982-find-longest-special-substring-that-occurs-thrice-ii.cpp diff --git a/2982-find-longest-special-substring-that-occurs-thrice-ii/2982-find-longest-special-substring-that-occurs-thrice-ii.cpp b/2982-find-longest-special-substring-that-occurs-thrice-ii/2982-find-longest-special-substring-that-occurs-thrice-ii.cpp new file mode 100644 index 00000000..aa1bf6d7 --- /dev/null +++ b/2982-find-longest-special-substring-that-occurs-thrice-ii/2982-find-longest-special-substring-that-occurs-thrice-ii.cpp @@ -0,0 +1,81 @@ +class Solution { +public: + int maximumLength(string str) { + + unordered_map mp, mp2, mp3; + + map, int> mp4; + + vector freq(26, 0); + + int n = str.size(); + + int ans = -1; + + for(int i = 0; i < n; ++i) + { + ++mp[str[i]]; + if(mp[str[i]] >= 3) + ans = 1; + } + + if(ans == -1) + return ans; + + int cnt = 1; + mp[str[0]] = 1; + for(int i = 1; i < n; ++i) + { + if(str[i] == str[i-1]) + { + ++cnt; + // int al = mp[str[i]]; + mp2[str[i]] = max(mp2[str[i]], cnt); + int have = mp2[str[i]]; + if(cnt < have) + { + if(cnt == have-1) + { + mp3[str[i]] = max(mp3[str[i]], cnt); + } + } + + ++mp4[{str[i], cnt}]; + if(mp4[{str[i], cnt}] >= 3) + freq[str[i]-'a'] = max(freq[str[i]-'a'],cnt); + } + else + { + cnt = 1; + mp2[str[i]] = max(mp2[str[i]], cnt); + } + } + for(auto& [f, e] : mp2) + { + // cout<= 3) + { + // cout<= 3) + // { + // ans = max(ans, itr.first.second); + // } + // } + + for(auto& itr : freq) + ans = max(ans, itr); + + return ans; + } +}; \ No newline at end of file From 860d1f2750748e99f65bbe725507101c59562847 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 31 Dec 2023 18:35:15 +0530 Subject: [PATCH 0584/1399] Create README - LeetHub --- .../README.md | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 1624-largest-substring-between-two-equal-characters/README.md diff --git a/1624-largest-substring-between-two-equal-characters/README.md b/1624-largest-substring-between-two-equal-characters/README.md new file mode 100644 index 00000000..cfc9757d --- /dev/null +++ b/1624-largest-substring-between-two-equal-characters/README.md @@ -0,0 +1,33 @@ +

1624. Largest Substring Between Two Equal Characters

Easy


Given a string s, return the length of the longest substring between two equal characters, excluding the two characters. If there is no such substring return -1.

+ +

A substring is a contiguous sequence of characters within a string.

+ +

 

+

Example 1:

+ +
Input: s = "aa"
+Output: 0
+Explanation: The optimal substring here is an empty substring between the two 'a's.
+ +

Example 2:

+ +
Input: s = "abca"
+Output: 2
+Explanation: The optimal substring here is "bc".
+
+ +

Example 3:

+ +
Input: s = "cbzxy"
+Output: -1
+Explanation: There are no characters that appear twice in s.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 300
  • +
  • s contains only lowercase English letters.
  • +
+
\ No newline at end of file From f39049a7cdd2f606fcff0eab9e611ae3760147d6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 31 Dec 2023 18:35:16 +0530 Subject: [PATCH 0585/1399] Attach NOTES - LeetHub --- 1624-largest-substring-between-two-equal-characters/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1624-largest-substring-between-two-equal-characters/NOTES.md diff --git a/1624-largest-substring-between-two-equal-characters/NOTES.md b/1624-largest-substring-between-two-equal-characters/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1624-largest-substring-between-two-equal-characters/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 994b30d530d7e95013154e2b1ce55dd6cd540a39 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 31 Dec 2023 18:35:19 +0530 Subject: [PATCH 0586/1399] Time: 6 ms (5.74%), Space: 6.9 MB (11.97%) - LeetHub --- ...substring-between-two-equal-characters.cpp | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 1624-largest-substring-between-two-equal-characters/1624-largest-substring-between-two-equal-characters.cpp diff --git a/1624-largest-substring-between-two-equal-characters/1624-largest-substring-between-two-equal-characters.cpp b/1624-largest-substring-between-two-equal-characters/1624-largest-substring-between-two-equal-characters.cpp new file mode 100644 index 00000000..64fb29c3 --- /dev/null +++ b/1624-largest-substring-between-two-equal-characters/1624-largest-substring-between-two-equal-characters.cpp @@ -0,0 +1,23 @@ +class Solution { +public: + int maxLengthBetweenEqualCharacters(string s) { + + int n = s.size(); + + int ans = -1; + + vector occurence(26, -1); + + for(int i = 0; i < n; ++i) + { + int firstOccurence = occurence[s[i] - 'a']; + + if(firstOccurence != -1) + ans = max(ans, i - firstOccurence - 1); + else + occurence[s[i] - 'a'] = i; + } + + return ans ; + } +}; \ No newline at end of file From 133514fce84ca0997a90fa0d49af130f3a1cfe98 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 1 Jan 2024 10:39:49 +0530 Subject: [PATCH 0587/1399] Create README - LeetHub --- 0455-assign-cookies/README.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 0455-assign-cookies/README.md diff --git a/0455-assign-cookies/README.md b/0455-assign-cookies/README.md new file mode 100644 index 00000000..9387f6b5 --- /dev/null +++ b/0455-assign-cookies/README.md @@ -0,0 +1,32 @@ +

455. Assign Cookies

Easy


Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie.

+ +

Each child i has a greed factor g[i], which is the minimum size of a cookie that the child will be content with; and each cookie j has a size s[j]. If s[j] >= g[i], we can assign the cookie j to the child i, and the child i will be content. Your goal is to maximize the number of your content children and output the maximum number.

+ +

 

+

Example 1:

+ +
Input: g = [1,2,3], s = [1,1]
+Output: 1
+Explanation: You have 3 children and 2 cookies. The greed factors of 3 children are 1, 2, 3. 
+And even though you have 2 cookies, since their size is both 1, you could only make the child whose greed factor is 1 content.
+You need to output 1.
+
+ +

Example 2:

+ +
Input: g = [1,2], s = [1,2,3]
+Output: 2
+Explanation: You have 2 children and 3 cookies. The greed factors of 2 children are 1, 2. 
+You have 3 cookies and their sizes are big enough to gratify all of the children, 
+You need to output 2.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= g.length <= 3 * 104
  • +
  • 0 <= s.length <= 3 * 104
  • +
  • 1 <= g[i], s[j] <= 231 - 1
  • +
+
\ No newline at end of file From 3ffbc7f5f80ebbaf0216c52c71377c58d5672740 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 1 Jan 2024 10:39:53 +0530 Subject: [PATCH 0588/1399] Time: 23 ms (61.11%), Space: 17.7 MB (98.96%) - LeetHub --- 0455-assign-cookies/0455-assign-cookies.cpp | 24 +++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 0455-assign-cookies/0455-assign-cookies.cpp diff --git a/0455-assign-cookies/0455-assign-cookies.cpp b/0455-assign-cookies/0455-assign-cookies.cpp new file mode 100644 index 00000000..de9ce97d --- /dev/null +++ b/0455-assign-cookies/0455-assign-cookies.cpp @@ -0,0 +1,24 @@ +class Solution { +public: + int findContentChildren(vector& g, vector& s) { + + sort(g.begin(), g.end()); + sort(s.begin(), s.end()); + + int j = 0; + int cnt = 0; + int i = 0; + + while(j < s.size() and i < g.size()) + { + if(g[i] <= s[j]) + { + ++i, ++j, ++cnt; + } + else + ++j; + } + + return cnt; + } +}; \ No newline at end of file From 8733ff398d8793979d56d2234be84bbfc6c290e1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 2 Jan 2024 21:46:17 +0530 Subject: [PATCH 0589/1399] Create README - LeetHub --- .../README.md | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 2610-convert-an-array-into-a-2d-array-with-conditions/README.md diff --git a/2610-convert-an-array-into-a-2d-array-with-conditions/README.md b/2610-convert-an-array-into-a-2d-array-with-conditions/README.md new file mode 100644 index 00000000..58b3233b --- /dev/null +++ b/2610-convert-an-array-into-a-2d-array-with-conditions/README.md @@ -0,0 +1,39 @@ +

2610. Convert an Array Into a 2D Array With Conditions

Medium


You are given an integer array nums. You need to create a 2D array from nums satisfying the following conditions:

+ +
    +
  • The 2D array should contain only the elements of the array nums.
  • +
  • Each row in the 2D array contains distinct integers.
  • +
  • The number of rows in the 2D array should be minimal.
  • +
+ +

Return the resulting array. If there are multiple answers, return any of them.

+ +

Note that the 2D array can have a different number of elements on each row.

+ +

 

+

Example 1:

+ +
Input: nums = [1,3,4,1,2,3,1]
+Output: [[1,3,4,2],[1,3],[1]]
+Explanation: We can create a 2D array that contains the following rows:
+- 1,3,4,2
+- 1,3
+- 1
+All elements of nums were used, and each row of the 2D array contains distinct integers, so it is a valid answer.
+It can be shown that we cannot have less than 3 rows in a valid array.
+ +

Example 2:

+ +
Input: nums = [1,2,3,4]
+Output: [[4,3,2,1]]
+Explanation: All elements of the array are distinct, so we can keep all of them in the first row of the 2D array.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 200
  • +
  • 1 <= nums[i] <= nums.length
  • +
+
\ No newline at end of file From 07cf7bf021df7f13444b4e3db25c91e703aea9be Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 2 Jan 2024 21:46:17 +0530 Subject: [PATCH 0590/1399] Attach NOTES - LeetHub --- 2610-convert-an-array-into-a-2d-array-with-conditions/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2610-convert-an-array-into-a-2d-array-with-conditions/NOTES.md diff --git a/2610-convert-an-array-into-a-2d-array-with-conditions/NOTES.md b/2610-convert-an-array-into-a-2d-array-with-conditions/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2610-convert-an-array-into-a-2d-array-with-conditions/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 7e579e35dccfbb72688c9748a33bdd6a60080a3c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 2 Jan 2024 21:46:23 +0530 Subject: [PATCH 0591/1399] Time: 23 ms (9.84%), Space: 30.5 MB (26.85%) - LeetHub --- ...-array-into-a-2d-array-with-conditions.cpp | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 2610-convert-an-array-into-a-2d-array-with-conditions/2610-convert-an-array-into-a-2d-array-with-conditions.cpp diff --git a/2610-convert-an-array-into-a-2d-array-with-conditions/2610-convert-an-array-into-a-2d-array-with-conditions.cpp b/2610-convert-an-array-into-a-2d-array-with-conditions/2610-convert-an-array-into-a-2d-array-with-conditions.cpp new file mode 100644 index 00000000..5dd5bc13 --- /dev/null +++ b/2610-convert-an-array-into-a-2d-array-with-conditions/2610-convert-an-array-into-a-2d-array-with-conditions.cpp @@ -0,0 +1,31 @@ +class Solution { +public: + vector> findMatrix(vector& nums) { + + map mp; + + for(auto& itr : nums) + ++mp[itr]; + + vector> ans; + + bool ok = true; + + while(ok) + { + vector currRow; + ok = false; + for(auto& [f, e] : mp) + { + if(mp[f] > 0) + currRow.push_back(f); + --e; + if(e > 0) + ok = true; + } + ans.push_back(currRow); + } + + return ans; + } +}; \ No newline at end of file From 460a64ac992ef0536a3f585dfc823f84859c422d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 3 Jan 2024 20:47:14 +0530 Subject: [PATCH 0592/1399] Create README - LeetHub --- .../README.md | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 2125-number-of-laser-beams-in-a-bank/README.md diff --git a/2125-number-of-laser-beams-in-a-bank/README.md b/2125-number-of-laser-beams-in-a-bank/README.md new file mode 100644 index 00000000..6b3c5a3f --- /dev/null +++ b/2125-number-of-laser-beams-in-a-bank/README.md @@ -0,0 +1,48 @@ +

2125. Number of Laser Beams in a Bank

Medium


Anti-theft security devices are activated inside a bank. You are given a 0-indexed binary string array bank representing the floor plan of the bank, which is an m x n 2D matrix. bank[i] represents the ith row, consisting of '0's and '1's. '0' means the cell is empty, while'1' means the cell has a security device.

+ +

There is one laser beam between any two security devices if both conditions are met:

+ +
    +
  • The two devices are located on two different rows: r1 and r2, where r1 < r2.
  • +
  • For each row i where r1 < i < r2, there are no security devices in the ith row.
  • +
+ +

Laser beams are independent, i.e., one beam does not interfere nor join with another.

+ +

Return the total number of laser beams in the bank.

+ +

 

+

Example 1:

+ +
Input: bank = ["011001","000000","010100","001000"]
+Output: 8
+Explanation: Between each of the following device pairs, there is one beam. In total, there are 8 beams:
+ * bank[0][1] -- bank[2][1]
+ * bank[0][1] -- bank[2][3]
+ * bank[0][2] -- bank[2][1]
+ * bank[0][2] -- bank[2][3]
+ * bank[0][5] -- bank[2][1]
+ * bank[0][5] -- bank[2][3]
+ * bank[2][1] -- bank[3][2]
+ * bank[2][3] -- bank[3][2]
+Note that there is no beam between any device on the 0th row with any on the 3rd row.
+This is because the 2nd row contains security devices, which breaks the second condition.
+
+ +

Example 2:

+ +
Input: bank = ["000","111","000"]
+Output: 0
+Explanation: There does not exist two devices located on two different rows.
+
+ +

 

+

Constraints:

+ +
    +
  • m == bank.length
  • +
  • n == bank[i].length
  • +
  • 1 <= m, n <= 500
  • +
  • bank[i][j] is either '0' or '1'.
  • +
+
\ No newline at end of file From baf38edae18c48bbd7cd4fd4bbdf92ffec18e5e3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 3 Jan 2024 20:47:18 +0530 Subject: [PATCH 0593/1399] Time: 91 ms (54.49%), Space: 23.3 MB (96.49%) - LeetHub --- .../2125-number-of-laser-beams-in-a-bank.cpp | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 2125-number-of-laser-beams-in-a-bank/2125-number-of-laser-beams-in-a-bank.cpp diff --git a/2125-number-of-laser-beams-in-a-bank/2125-number-of-laser-beams-in-a-bank.cpp b/2125-number-of-laser-beams-in-a-bank/2125-number-of-laser-beams-in-a-bank.cpp new file mode 100644 index 00000000..371b3993 --- /dev/null +++ b/2125-number-of-laser-beams-in-a-bank/2125-number-of-laser-beams-in-a-bank.cpp @@ -0,0 +1,32 @@ +class Solution { +public: + int numberOfBeams(vector& bank) { + + int n = bank.size(); + + int ans = 0, i = 0; + + while(i < n) + { + int cnt = count(bank[i].begin(), bank[i].end(), '1'); + + int j = i+1; + + while(j < n) + { + int cnt2 = count(bank[j].begin(), bank[j].end(), '1'); + + if(cnt2 >= 1) + { + ans += (cnt * cnt2); + break; + } + ++j; + } + + i = j++; + } + + return ans; + } +}; \ No newline at end of file From d921074ab76f8638cccc844524da4ea291b17d06 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 3 Jan 2024 20:48:15 +0530 Subject: [PATCH 0594/1399] Attach NOTES - LeetHub --- 2125-number-of-laser-beams-in-a-bank/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2125-number-of-laser-beams-in-a-bank/NOTES.md diff --git a/2125-number-of-laser-beams-in-a-bank/NOTES.md b/2125-number-of-laser-beams-in-a-bank/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2125-number-of-laser-beams-in-a-bank/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From f8b88d3530bf92bf925e400075c8adb05bd7e56a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 3 Jan 2024 20:48:19 +0530 Subject: [PATCH 0595/1399] Time: 91 ms (54.49%), Space: 23.3 MB (96.49%) - LeetHub From 03fd7d5e33f856b51984bf4d91136cb1f524b43e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 3 Jan 2024 20:48:43 +0530 Subject: [PATCH 0596/1399] Attach NOTES - LeetHub From dcc311ba9751b496c2b30b53e812d5ddf019be0c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 3 Jan 2024 20:48:47 +0530 Subject: [PATCH 0597/1399] Time: 91 ms (54.49%), Space: 23.3 MB (96.49%) - LeetHub From c1e2dde3df19dd70c88fc7688197bef4cbbf4527 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 4 Jan 2024 22:48:11 +0530 Subject: [PATCH 0598/1399] Create README - LeetHub --- .../README.md | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 2870-minimum-number-of-operations-to-make-array-empty/README.md diff --git a/2870-minimum-number-of-operations-to-make-array-empty/README.md b/2870-minimum-number-of-operations-to-make-array-empty/README.md new file mode 100644 index 00000000..e32a8716 --- /dev/null +++ b/2870-minimum-number-of-operations-to-make-array-empty/README.md @@ -0,0 +1,39 @@ +

2870. Minimum Number of Operations to Make Array Empty

Medium


You are given a 0-indexed array nums consisting of positive integers.

+ +

There are two types of operations that you can apply on the array any number of times:

+ +
    +
  • Choose two elements with equal values and delete them from the array.
  • +
  • Choose three elements with equal values and delete them from the array.
  • +
+ +

Return the minimum number of operations required to make the array empty, or -1 if it is not possible.

+ +

 

+

Example 1:

+ +
Input: nums = [2,3,3,2,2,4,2,3,4]
+Output: 4
+Explanation: We can apply the following operations to make the array empty:
+- Apply the first operation on the elements at indices 0 and 3. The resulting array is nums = [3,3,2,4,2,3,4].
+- Apply the first operation on the elements at indices 2 and 4. The resulting array is nums = [3,3,4,3,4].
+- Apply the second operation on the elements at indices 0, 1, and 3. The resulting array is nums = [4,4].
+- Apply the first operation on the elements at indices 0 and 1. The resulting array is nums = [].
+It can be shown that we cannot make the array empty in less than 4 operations.
+
+ +

Example 2:

+ +
Input: nums = [2,1,2,2,3,3]
+Output: -1
+Explanation: It is impossible to empty the array.
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= nums.length <= 105
  • +
  • 1 <= nums[i] <= 106
  • +
+
\ No newline at end of file From 18173da209fa9dd392bd9147d4e23c9c3f5ff54a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 4 Jan 2024 22:48:14 +0530 Subject: [PATCH 0599/1399] Time: 136 ms (24.86%), Space: 85.5 MB (31.49%) - LeetHub --- ...mber-of-operations-to-make-array-empty.cpp | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 2870-minimum-number-of-operations-to-make-array-empty/2870-minimum-number-of-operations-to-make-array-empty.cpp diff --git a/2870-minimum-number-of-operations-to-make-array-empty/2870-minimum-number-of-operations-to-make-array-empty.cpp b/2870-minimum-number-of-operations-to-make-array-empty/2870-minimum-number-of-operations-to-make-array-empty.cpp new file mode 100644 index 00000000..f86d744f --- /dev/null +++ b/2870-minimum-number-of-operations-to-make-array-empty/2870-minimum-number-of-operations-to-make-array-empty.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + int minOperations(vector& nums) { + + map mp; + + for(auto& itr : nums) + ++mp[itr]; + + int ans = 0; + + for(auto&[_, e] : mp) + { + if(e == 1) + return -1; + + if(e % 3 == 0) + ans += e/3; + else{ + ans += e/3; + ++ans; + } + } + + return ans; + } +}; \ No newline at end of file From 46f0cc853c4be6c037ccb102c1aa3efff021a774 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 6 Jan 2024 09:31:24 +0530 Subject: [PATCH 0600/1399] Create README - LeetHub --- 0324-wiggle-sort-ii/README.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 0324-wiggle-sort-ii/README.md diff --git a/0324-wiggle-sort-ii/README.md b/0324-wiggle-sort-ii/README.md new file mode 100644 index 00000000..ae6f43d0 --- /dev/null +++ b/0324-wiggle-sort-ii/README.md @@ -0,0 +1,29 @@ +

324. Wiggle Sort II

Medium


Given an integer array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]....

+ +

You may assume the input array always has a valid answer.

+ +

 

+

Example 1:

+ +
Input: nums = [1,5,1,1,6,4]
+Output: [1,6,1,5,1,4]
+Explanation: [1,4,1,5,1,6] is also accepted.
+
+ +

Example 2:

+ +
Input: nums = [1,3,2,2,3,1]
+Output: [2,3,1,3,1,2]
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 5 * 104
  • +
  • 0 <= nums[i] <= 5000
  • +
  • It is guaranteed that there will be an answer for the given input nums.
  • +
+ +

 

+Follow Up: Can you do it in O(n) time and/or in-place with O(1) extra space?
\ No newline at end of file From 7161944ff926f1d49edbf2bb7ae24ff3e0bb2c19 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 6 Jan 2024 09:31:24 +0530 Subject: [PATCH 0601/1399] Attach NOTES - LeetHub --- 0324-wiggle-sort-ii/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0324-wiggle-sort-ii/NOTES.md diff --git a/0324-wiggle-sort-ii/NOTES.md b/0324-wiggle-sort-ii/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0324-wiggle-sort-ii/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From a5044e7d0e15fb340e0aaa5ab91db1b32a819de1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 6 Jan 2024 09:31:27 +0530 Subject: [PATCH 0602/1399] Time: 20 ms (38.64%), Space: 18.2 MB (29.97%) - LeetHub --- 0324-wiggle-sort-ii/0324-wiggle-sort-ii.cpp | 25 +++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 0324-wiggle-sort-ii/0324-wiggle-sort-ii.cpp diff --git a/0324-wiggle-sort-ii/0324-wiggle-sort-ii.cpp b/0324-wiggle-sort-ii/0324-wiggle-sort-ii.cpp new file mode 100644 index 00000000..b49df90a --- /dev/null +++ b/0324-wiggle-sort-ii/0324-wiggle-sort-ii.cpp @@ -0,0 +1,25 @@ +class Solution { +public: + void wiggleSort(vector& nums) { + + int n = nums.size(); + + sort(nums.begin(), nums.end()); + + vector ans(n); + + int k = n-1; + + for(int i = 1; i < n; i += 2) + { + ans[i] = nums[k--]; + } + + for(int i = 0; i < n; i += 2) + { + ans[i] = nums[k--]; + } + + nums = ans; + } +}; \ No newline at end of file From cd12829e48a0ede3e8402bad32dce3e7f4364a16 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 6 Jan 2024 09:37:35 +0530 Subject: [PATCH 0603/1399] Attach NOTES - LeetHub From 38ab38572c95c982a87538392cb5f348300ff761 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 6 Jan 2024 09:37:38 +0530 Subject: [PATCH 0604/1399] Time: 20 ms (38.64%), Space: 18.2 MB (29.97%) - LeetHub From 778fbe1f65fd98d9ccfdc82cefe9af7971ba8b1a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 6 Jan 2024 10:58:27 +0530 Subject: [PATCH 0605/1399] Attach NOTES - LeetHub From d601e510a99f576a01fae200608078fbabba4add Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 6 Jan 2024 10:58:31 +0530 Subject: [PATCH 0606/1399] Time: 20 ms (38.64%), Space: 18.2 MB (29.97%) - LeetHub From cab06b751450c27300f00201427243c4a19f5d55 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 6 Jan 2024 23:30:13 +0530 Subject: [PATCH 0607/1399] Create README - LeetHub --- .../README.md | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 1235-maximum-profit-in-job-scheduling/README.md diff --git a/1235-maximum-profit-in-job-scheduling/README.md b/1235-maximum-profit-in-job-scheduling/README.md new file mode 100644 index 00000000..8660019e --- /dev/null +++ b/1235-maximum-profit-in-job-scheduling/README.md @@ -0,0 +1,44 @@ +

1235. Maximum Profit in Job Scheduling

Hard


We have n jobs, where every job is scheduled to be done from startTime[i] to endTime[i], obtaining a profit of profit[i].

+ +

You're given the startTime, endTime and profit arrays, return the maximum profit you can take such that there are no two jobs in the subset with overlapping time range.

+ +

If you choose a job that ends at time X you will be able to start another job that starts at time X.

+ +

 

+

Example 1:

+ +

+ +
Input: startTime = [1,2,3,3], endTime = [3,4,5,6], profit = [50,10,40,70]
+Output: 120
+Explanation: The subset chosen is the first and fourth job. 
+Time range [1-3]+[3-6] , we get profit of 120 = 50 + 70.
+
+ +

Example 2:

+ +

+ +
Input: startTime = [1,2,3,4,6], endTime = [3,5,10,6,9], profit = [20,20,100,70,60]
+Output: 150
+Explanation: The subset chosen is the first, fourth and fifth job. 
+Profit obtained 150 = 20 + 70 + 60.
+
+ +

Example 3:

+ +

+ +
Input: startTime = [1,1,1], endTime = [2,3,4], profit = [5,6,4]
+Output: 6
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= startTime.length == endTime.length == profit.length <= 5 * 104
  • +
  • 1 <= startTime[i] < endTime[i] <= 109
  • +
  • 1 <= profit[i] <= 104
  • +
+
\ No newline at end of file From b9e2d23cd395d7fa57c379819f14dad2070e9d6a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 6 Jan 2024 23:30:17 +0530 Subject: [PATCH 0608/1399] Time: 196 ms (48.75%), Space: 83 MB (31.69%) - LeetHub --- .../1235-maximum-profit-in-job-scheduling.cpp | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 1235-maximum-profit-in-job-scheduling/1235-maximum-profit-in-job-scheduling.cpp diff --git a/1235-maximum-profit-in-job-scheduling/1235-maximum-profit-in-job-scheduling.cpp b/1235-maximum-profit-in-job-scheduling/1235-maximum-profit-in-job-scheduling.cpp new file mode 100644 index 00000000..a2d455e9 --- /dev/null +++ b/1235-maximum-profit-in-job-scheduling/1235-maximum-profit-in-job-scheduling.cpp @@ -0,0 +1,60 @@ +class Solution { +public: + + int getNextIdx(int low, int last, vector>& jobs) + { + int n = jobs.size(); + int high = n-1, result = n; + + while(low <= high) + { + int mid = low + (high - low)/2; + + if(jobs[mid][0] >= last) + { + result = mid; + high = mid-1; + } + else + low = mid+1; + } + + return result; + } + + int helper(int idx, int n, vector>& jobs, vector& dp) + { + if(idx == n) + return 0; + + if(dp[idx] != -1) + return dp[idx]; + + int next = getNextIdx(idx+1, jobs[idx][1], jobs); + + int notTake = helper(idx+1, n, jobs, dp); + + int take = jobs[idx][2] + helper(next, n, jobs, dp); + + return dp[idx] = max(take, notTake); + } + + int jobScheduling(vector& startTime, vector& endTime, vector& profit) { + + int n = startTime.size(); + + vector> jobs; + + for(int i = 0; i < n; ++i) + { + jobs.push_back({startTime[i], endTime[i], profit[i]}); + } + + sort(jobs.begin(), jobs.end()); + + vector dp(n+1, -1); + + return helper(0, n, jobs, dp); + + } +}; \ No newline at end of file From bdcf78f6fae2a117636de924d9721cfa5d8a86bf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 7 Jan 2024 20:28:00 +0530 Subject: [PATCH 0609/1399] Attach NOTES - LeetHub From 6e429967fb1cd9bf96de832ef0e7345b3c210a4c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 7 Jan 2024 21:46:18 +0530 Subject: [PATCH 0610/1399] Attach NOTES - LeetHub --- 0698-partition-to-k-equal-sum-subsets/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0698-partition-to-k-equal-sum-subsets/NOTES.md diff --git a/0698-partition-to-k-equal-sum-subsets/NOTES.md b/0698-partition-to-k-equal-sum-subsets/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0698-partition-to-k-equal-sum-subsets/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 7b528db9c6d7e7ed426bbe8a6b66e990d5220da7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 8 Jan 2024 21:30:30 +0530 Subject: [PATCH 0611/1399] Attach NOTES - LeetHub --- 0004-median-of-two-sorted-arrays/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0004-median-of-two-sorted-arrays/NOTES.md diff --git a/0004-median-of-two-sorted-arrays/NOTES.md b/0004-median-of-two-sorted-arrays/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0004-median-of-two-sorted-arrays/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From c73705d4c79d02fb62a3164ec4181355b98beaa2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 8 Jan 2024 22:34:43 +0530 Subject: [PATCH 0612/1399] Attach NOTES - LeetHub From bea8877ce110c3d0d801b1d4622fc6df0787b12a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 9 Jan 2024 08:31:37 +0530 Subject: [PATCH 0613/1399] Attach NOTES - LeetHub --- 1539-kth-missing-positive-number/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1539-kth-missing-positive-number/NOTES.md diff --git a/1539-kth-missing-positive-number/NOTES.md b/1539-kth-missing-positive-number/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1539-kth-missing-positive-number/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 557fc29278d23d11b6e7e1b300dbe89cd142d6a9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 9 Jan 2024 10:40:46 +0530 Subject: [PATCH 0614/1399] Create README - LeetHub --- .../README.md | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 1482-minimum-number-of-days-to-make-m-bouquets/README.md diff --git a/1482-minimum-number-of-days-to-make-m-bouquets/README.md b/1482-minimum-number-of-days-to-make-m-bouquets/README.md new file mode 100644 index 00000000..5d2527df --- /dev/null +++ b/1482-minimum-number-of-days-to-make-m-bouquets/README.md @@ -0,0 +1,50 @@ +

1482. Minimum Number of Days to Make m Bouquets

Medium


You are given an integer array bloomDay, an integer m and an integer k.

+ +

You want to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.

+ +

The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet.

+ +

Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make m bouquets return -1.

+ +

 

+

Example 1:

+ +
Input: bloomDay = [1,10,3,10,2], m = 3, k = 1
+Output: 3
+Explanation: Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden.
+We need 3 bouquets each should contain 1 flower.
+After day 1: [x, _, _, _, _]   // we can only make one bouquet.
+After day 2: [x, _, _, _, x]   // we can only make two bouquets.
+After day 3: [x, _, x, _, x]   // we can make 3 bouquets. The answer is 3.
+
+ +

Example 2:

+ +
Input: bloomDay = [1,10,3,10,2], m = 3, k = 2
+Output: -1
+Explanation: We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1.
+
+ +

Example 3:

+ +
Input: bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
+Output: 12
+Explanation: We need 2 bouquets each should have 3 flowers.
+Here is the garden after the 7 and 12 days:
+After day 7: [x, x, x, x, _, x, x]
+We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent.
+After day 12: [x, x, x, x, x, x, x]
+It is obvious that we can make two bouquets in different ways.
+
+ +

 

+

Constraints:

+ +
    +
  • bloomDay.length == n
  • +
  • 1 <= n <= 105
  • +
  • 1 <= bloomDay[i] <= 109
  • +
  • 1 <= m <= 106
  • +
  • 1 <= k <= n
  • +
+
\ No newline at end of file From 60a45d4895d72d4d43c4eaed6570c313db2f7868 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 9 Jan 2024 10:40:46 +0530 Subject: [PATCH 0615/1399] Attach NOTES - LeetHub --- 1482-minimum-number-of-days-to-make-m-bouquets/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1482-minimum-number-of-days-to-make-m-bouquets/NOTES.md diff --git a/1482-minimum-number-of-days-to-make-m-bouquets/NOTES.md b/1482-minimum-number-of-days-to-make-m-bouquets/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1482-minimum-number-of-days-to-make-m-bouquets/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 36d8619ddb95109e9973115fe8ab2b68e35eda93 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 9 Jan 2024 10:40:55 +0530 Subject: [PATCH 0616/1399] Time: 159 ms (23.17%), Space: 66.8 MB (76.25%) - LeetHub --- ...imum-number-of-days-to-make-m-bouquets.cpp | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 1482-minimum-number-of-days-to-make-m-bouquets/1482-minimum-number-of-days-to-make-m-bouquets.cpp diff --git a/1482-minimum-number-of-days-to-make-m-bouquets/1482-minimum-number-of-days-to-make-m-bouquets.cpp b/1482-minimum-number-of-days-to-make-m-bouquets/1482-minimum-number-of-days-to-make-m-bouquets.cpp new file mode 100644 index 00000000..b9454582 --- /dev/null +++ b/1482-minimum-number-of-days-to-make-m-bouquets/1482-minimum-number-of-days-to-make-m-bouquets.cpp @@ -0,0 +1,53 @@ +class Solution { +public: + int minDays(vector& bloomDay, int m, int k) { + + int n = bloomDay.size(); + + if((long long)k * m > n) + return -1; + + int low = *min_element(bloomDay.begin(), bloomDay.end()); + int high = *max_element(bloomDay.begin(), bloomDay.end()); + + auto canBloom = [&](int mid) + { + int bloom = 0; + int cnt = 0; + for(int i = 0; i < n; ++i) + { + if(bloomDay[i] <= mid) + { + ++cnt; + } + else + { + bloom += (cnt / k); + cnt = 0; + } + } + + bloom += (cnt / k); + + return bloom; + }; + + int ans = -1; + + while(low <= high) + { + int mid = low + (high - low) / 2; + + if(canBloom(mid) >= m) + { + ans = mid; + high = mid-1; + } + else + low = mid+1; + } + + return ans; + + } +}; \ No newline at end of file From 4bef233a9416b9dcb8d06d94df335e4314ec8426 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 9 Jan 2024 12:01:22 +0530 Subject: [PATCH 0617/1399] Create README - LeetHub --- 0162-find-peak-element/README.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 0162-find-peak-element/README.md diff --git a/0162-find-peak-element/README.md b/0162-find-peak-element/README.md new file mode 100644 index 00000000..96c32bd2 --- /dev/null +++ b/0162-find-peak-element/README.md @@ -0,0 +1,30 @@ +

162. Find Peak Element

Medium


A peak element is an element that is strictly greater than its neighbors.

+ +

Given a 0-indexed integer array nums, find a peak element, and return its index. If the array contains multiple peaks, return the index to any of the peaks.

+ +

You may imagine that nums[-1] = nums[n] = -∞. In other words, an element is always considered to be strictly greater than a neighbor that is outside the array.

+ +

You must write an algorithm that runs in O(log n) time.

+ +

 

+

Example 1:

+ +
Input: nums = [1,2,3,1]
+Output: 2
+Explanation: 3 is a peak element and your function should return the index number 2.
+ +

Example 2:

+ +
Input: nums = [1,2,1,3,5,6,4]
+Output: 5
+Explanation: Your function can return either index number 1 where the peak element is 2, or index number 5 where the peak element is 6.
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 1000
  • +
  • -231 <= nums[i] <= 231 - 1
  • +
  • nums[i] != nums[i + 1] for all valid i.
  • +
+
\ No newline at end of file From 6d3d78de0b98b4614791e334c794f83bcb1e1854 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 9 Jan 2024 12:01:23 +0530 Subject: [PATCH 0618/1399] Attach NOTES - LeetHub --- 0162-find-peak-element/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0162-find-peak-element/NOTES.md diff --git a/0162-find-peak-element/NOTES.md b/0162-find-peak-element/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0162-find-peak-element/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 43af40764102c1fd02b363535e80cbde761273ac Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 9 Jan 2024 12:01:26 +0530 Subject: [PATCH 0619/1399] Time: 3 ms (70.23%), Space: 9.3 MB (35.78%) - LeetHub --- .../0162-find-peak-element.cpp | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 0162-find-peak-element/0162-find-peak-element.cpp diff --git a/0162-find-peak-element/0162-find-peak-element.cpp b/0162-find-peak-element/0162-find-peak-element.cpp new file mode 100644 index 00000000..b9badb4f --- /dev/null +++ b/0162-find-peak-element/0162-find-peak-element.cpp @@ -0,0 +1,30 @@ +class Solution { +public: + int findPeakElement(vector& arr) { + + int n = arr.size(); + + if(n == 1) + return 0; + if(arr[0] > arr[1]) + return 0; + if(arr[n-1] > arr[n-2]) + return n-1; + + int low = 1, high = n-2; + + while(low <= high) + { + int mid = low + (high - low) / 2; + + if(arr[mid] > arr[mid+1] and arr[mid] > arr[mid-1]) + return mid; + else if(arr[mid] > arr[mid-1]) + low = mid+1; + else + high = mid-1; + } + + return -1; + } +}; \ No newline at end of file From 135631649c9a43d4ce28f8952e5f48ce8d306588 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 9 Jan 2024 22:02:32 +0530 Subject: [PATCH 0620/1399] Create README - LeetHub --- 0872-leaf-similar-trees/README.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 0872-leaf-similar-trees/README.md diff --git a/0872-leaf-similar-trees/README.md b/0872-leaf-similar-trees/README.md new file mode 100644 index 00000000..71da5fe7 --- /dev/null +++ b/0872-leaf-similar-trees/README.md @@ -0,0 +1,31 @@ +

872. Leaf-Similar Trees

Easy


Consider all the leaves of a binary tree, from left to right order, the values of those leaves form a leaf value sequence.

+ +

+ +

For example, in the given tree above, the leaf value sequence is (6, 7, 4, 9, 8).

+ +

Two binary trees are considered leaf-similar if their leaf value sequence is the same.

+ +

Return true if and only if the two given trees with head nodes root1 and root2 are leaf-similar.

+ +

 

+

Example 1:

+ +
Input: root1 = [3,5,1,6,2,9,8,null,null,7,4], root2 = [3,5,1,6,7,4,2,null,null,null,null,null,null,9,8]
+Output: true
+
+ +

Example 2:

+ +
Input: root1 = [1,2,3], root2 = [1,3,2]
+Output: false
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in each tree will be in the range [1, 200].
  • +
  • Both of the given trees will have values in the range [0, 200].
  • +
+
\ No newline at end of file From 384fca62a7d2b5b0c419296851fb3cc18775008c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 10 Jan 2024 23:23:19 +0530 Subject: [PATCH 0621/1399] Create README - LeetHub --- .../README.md | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 2385-amount-of-time-for-binary-tree-to-be-infected/README.md diff --git a/2385-amount-of-time-for-binary-tree-to-be-infected/README.md b/2385-amount-of-time-for-binary-tree-to-be-infected/README.md new file mode 100644 index 00000000..4c9f31b3 --- /dev/null +++ b/2385-amount-of-time-for-binary-tree-to-be-infected/README.md @@ -0,0 +1,42 @@ +

2385. Amount of Time for Binary Tree to Be Infected

Medium


You are given the root of a binary tree with unique values, and an integer start. At minute 0, an infection starts from the node with value start.

+ +

Each minute, a node becomes infected if:

+ +
    +
  • The node is currently uninfected.
  • +
  • The node is adjacent to an infected node.
  • +
+ +

Return the number of minutes needed for the entire tree to be infected.

+ +

 

+

Example 1:

+ +
Input: root = [1,5,3,null,4,10,6,9,2], start = 3
+Output: 4
+Explanation: The following nodes are infected during:
+- Minute 0: Node 3
+- Minute 1: Nodes 1, 10 and 6
+- Minute 2: Node 5
+- Minute 3: Node 4
+- Minute 4: Nodes 9 and 2
+It takes 4 minutes for the whole tree to be infected so we return 4.
+
+ +

Example 2:

+ +
Input: root = [1], start = 1
+Output: 0
+Explanation: At minute 0, the only node in the tree is infected so we return 0.
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is in the range [1, 105].
  • +
  • 1 <= Node.val <= 105
  • +
  • Each node has a unique value.
  • +
  • A node with a value of start exists in the tree.
  • +
+
\ No newline at end of file From 2c1712c52582a2459a36ca4e956c16c02c527420 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 10 Jan 2024 23:23:23 +0530 Subject: [PATCH 0622/1399] Time: 763 ms (10.98%), Space: 185.8 MB (25.66%) - LeetHub --- ...of-time-for-binary-tree-to-be-infected.cpp | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 2385-amount-of-time-for-binary-tree-to-be-infected/2385-amount-of-time-for-binary-tree-to-be-infected.cpp diff --git a/2385-amount-of-time-for-binary-tree-to-be-infected/2385-amount-of-time-for-binary-tree-to-be-infected.cpp b/2385-amount-of-time-for-binary-tree-to-be-infected/2385-amount-of-time-for-binary-tree-to-be-infected.cpp new file mode 100644 index 00000000..c9bdd38b --- /dev/null +++ b/2385-amount-of-time-for-binary-tree-to-be-infected/2385-amount-of-time-for-binary-tree-to-be-infected.cpp @@ -0,0 +1,86 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + + map> graph; + + int amountOfTime(TreeNode* root, int start) { + + queue q; + + q.push(root); + + while(!q.empty()) + { + int size = q.size(); + + for(int i = 0; i < size; ++i) + { + TreeNode* parent = q.front(); + q.pop(); + + if(parent->left) + { + q.push(parent->left); + graph[parent->val].push_back(parent->left->val); + graph[parent->left->val].push_back(parent->val); + } + + if(parent->right) + { + q.push(parent->right); + graph[parent->val].push_back(parent->right->val); + graph[parent->right->val].push_back(parent->val); + } + } + } + + int n = graph.size(); + + queue q2; + + set visited; + + visited.insert(start); + + for(auto& itr : graph[start]) + { + q2.push(itr); + visited.insert(itr); + } + + int cnt = 0; + + while(!q2.empty()) + { + int size = q2.size(); + for(int i = 0; i < size; ++i) + { + int curr = q2.front(); + q2.pop(); + + for(auto& itr : graph[curr]) + { + if(!visited.count(itr)) + { + q2.push(itr); + visited.insert(itr); + } + } + } + ++cnt; + } + + return cnt; + } +}; \ No newline at end of file From 205f22fdb8002bbce33a4be738ff7036425ba0fb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 11 Jan 2024 22:09:49 +0530 Subject: [PATCH 0623/1399] Create README - LeetHub --- .../README.md | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 1026-maximum-difference-between-node-and-ancestor/README.md diff --git a/1026-maximum-difference-between-node-and-ancestor/README.md b/1026-maximum-difference-between-node-and-ancestor/README.md new file mode 100644 index 00000000..67c353c5 --- /dev/null +++ b/1026-maximum-difference-between-node-and-ancestor/README.md @@ -0,0 +1,30 @@ +

1026. Maximum Difference Between Node and Ancestor

Medium


Given the root of a binary tree, find the maximum value v for which there exist different nodes a and b where v = |a.val - b.val| and a is an ancestor of b.

+ +

A node a is an ancestor of b if either: any child of a is equal to b or any child of a is an ancestor of b.

+ +

 

+

Example 1:

+ +
Input: root = [8,3,10,1,6,null,14,null,null,4,7,13]
+Output: 7
+Explanation: We have various ancestor-node differences, some of which are given below :
+|8 - 3| = 5
+|3 - 7| = 4
+|8 - 1| = 7
+|10 - 13| = 3
+Among all possible differences, the maximum value of 7 is obtained by |8 - 1| = 7.
+ +

Example 2:

+ +
Input: root = [1,null,2,null,0,3]
+Output: 3
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is in the range [2, 5000].
  • +
  • 0 <= Node.val <= 105
  • +
+
\ No newline at end of file From 7c41b2dd14774b0e06d075a784b9fd9fdb1df078 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 11 Jan 2024 22:09:52 +0530 Subject: [PATCH 0624/1399] Time: 3 ms (85.41%), Space: 10.1 MB (59.95%) - LeetHub --- ...m-difference-between-node-and-ancestor.cpp | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 1026-maximum-difference-between-node-and-ancestor/1026-maximum-difference-between-node-and-ancestor.cpp diff --git a/1026-maximum-difference-between-node-and-ancestor/1026-maximum-difference-between-node-and-ancestor.cpp b/1026-maximum-difference-between-node-and-ancestor/1026-maximum-difference-between-node-and-ancestor.cpp new file mode 100644 index 00000000..170912a6 --- /dev/null +++ b/1026-maximum-difference-between-node-and-ancestor/1026-maximum-difference-between-node-and-ancestor.cpp @@ -0,0 +1,38 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + + int helper(TreeNode* root, int currMax, int currMin) + { + if(!root) + return currMax - currMin; + currMax = max(currMax,root->val); + currMin = min(currMin,root->val); + + int left = helper(root->left,currMax,currMin); + int right = helper(root->right,currMax,currMin); + + return max(left,right); + } + + + int maxAncestorDiff(TreeNode* root) { + + if(!root) + return 0; + + return helper(root,root->val,root->val); + + + } +}; \ No newline at end of file From f23a787447527e62fd45d8cd6e1932a81eb52e8a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 13 Jan 2024 08:19:15 +0530 Subject: [PATCH 0625/1399] Create README - LeetHub --- .../README.md | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 1347-minimum-number-of-steps-to-make-two-strings-anagram/README.md diff --git a/1347-minimum-number-of-steps-to-make-two-strings-anagram/README.md b/1347-minimum-number-of-steps-to-make-two-strings-anagram/README.md new file mode 100644 index 00000000..6838a54f --- /dev/null +++ b/1347-minimum-number-of-steps-to-make-two-strings-anagram/README.md @@ -0,0 +1,37 @@ +

1347. Minimum Number of Steps to Make Two Strings Anagram

Medium


You are given two strings of the same length s and t. In one step you can choose any character of t and replace it with another character.

+ +

Return the minimum number of steps to make t an anagram of s.

+ +

An Anagram of a string is a string that contains the same characters with a different (or the same) ordering.

+ +

 

+

Example 1:

+ +
Input: s = "bab", t = "aba"
+Output: 1
+Explanation: Replace the first 'a' in t with b, t = "bba" which is anagram of s.
+
+ +

Example 2:

+ +
Input: s = "leetcode", t = "practice"
+Output: 5
+Explanation: Replace 'p', 'r', 'a', 'i' and 'c' from t with proper characters to make t anagram of s.
+
+ +

Example 3:

+ +
Input: s = "anagram", t = "mangaar"
+Output: 0
+Explanation: "anagram" and "mangaar" are anagrams. 
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 5 * 104
  • +
  • s.length == t.length
  • +
  • s and t consist of lowercase English letters only.
  • +
+
\ No newline at end of file From 5c131870428e178f580be406fec17c793d501833 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 13 Jan 2024 08:19:18 +0530 Subject: [PATCH 0626/1399] Time: 145 ms (11.64%), Space: 17.1 MB (19.43%) - LeetHub --- ...r-of-steps-to-make-two-strings-anagram.cpp | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 1347-minimum-number-of-steps-to-make-two-strings-anagram/1347-minimum-number-of-steps-to-make-two-strings-anagram.cpp diff --git a/1347-minimum-number-of-steps-to-make-two-strings-anagram/1347-minimum-number-of-steps-to-make-two-strings-anagram.cpp b/1347-minimum-number-of-steps-to-make-two-strings-anagram/1347-minimum-number-of-steps-to-make-two-strings-anagram.cpp new file mode 100644 index 00000000..2a4429de --- /dev/null +++ b/1347-minimum-number-of-steps-to-make-two-strings-anagram/1347-minimum-number-of-steps-to-make-two-strings-anagram.cpp @@ -0,0 +1,26 @@ +class Solution { +public: + int minSteps(string s, string t) { + + map mp; + + int need = 0; + + for(auto& ele : s) + ++mp[ele]; + + for(auto& ele : t) + { + if(mp[ele] > 0) + { + --mp[ele]; + if(mp[ele] == 0) + mp.erase(ele); + } + else + ++need; + } + + return need; + } +}; \ No newline at end of file From 689688c70a1e0ecf1a0fde5f48ceb14b39371a6f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 13 Jan 2024 08:20:02 +0530 Subject: [PATCH 0627/1399] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1347-minimum-number-of-steps-to-make-two-strings-anagram/NOTES.md diff --git a/1347-minimum-number-of-steps-to-make-two-strings-anagram/NOTES.md b/1347-minimum-number-of-steps-to-make-two-strings-anagram/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1347-minimum-number-of-steps-to-make-two-strings-anagram/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 84a6b7cd0ff2fd34423277bc1c1768affbeddd2c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 13 Jan 2024 08:20:05 +0530 Subject: [PATCH 0628/1399] Time: 145 ms (11.64%), Space: 17.1 MB (19.43%) - LeetHub From 15c7259c1ea9a639099c8b5a10c37294e977bba6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 13 Jan 2024 08:21:48 +0530 Subject: [PATCH 0629/1399] Attach NOTES - LeetHub From 2f8dc68d9631ced74db6331cf50e4491b8f25eab Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 13 Jan 2024 08:21:51 +0530 Subject: [PATCH 0630/1399] Time: 145 ms (11.64%), Space: 17.1 MB (19.43%) - LeetHub From 944fa9c262ce494cc042a18cecab1eb945bdfc91 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 14 Jan 2024 21:37:59 +0530 Subject: [PATCH 0631/1399] Attach NOTES - LeetHub --- 1657-determine-if-two-strings-are-close/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1657-determine-if-two-strings-are-close/NOTES.md diff --git a/1657-determine-if-two-strings-are-close/NOTES.md b/1657-determine-if-two-strings-are-close/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1657-determine-if-two-strings-are-close/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 0839393e0ca8ca8a17c76b65183168da25edd030 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 16 Jan 2024 10:42:43 +0530 Subject: [PATCH 0632/1399] Attach NOTES - LeetHub --- 0380-insert-delete-getrandom-o1/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0380-insert-delete-getrandom-o1/NOTES.md diff --git a/0380-insert-delete-getrandom-o1/NOTES.md b/0380-insert-delete-getrandom-o1/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0380-insert-delete-getrandom-o1/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 185c4502ff1bda2da25f023e4820eab9647af220 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 16 Jan 2024 10:43:43 +0530 Subject: [PATCH 0633/1399] Attach NOTES - LeetHub From 3cbce3ed511e57bd39f9396070a75d13c111bcb8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 17 Jan 2024 08:28:01 +0530 Subject: [PATCH 0634/1399] Attach NOTES - LeetHub --- 1207-unique-number-of-occurrences/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1207-unique-number-of-occurrences/NOTES.md diff --git a/1207-unique-number-of-occurrences/NOTES.md b/1207-unique-number-of-occurrences/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1207-unique-number-of-occurrences/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From a3702da2ae61784a0eae3b9d86065e65195ab7fb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 18 Jan 2024 19:56:39 +0530 Subject: [PATCH 0635/1399] Create README - LeetHub --- 0070-climbing-stairs/README.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 0070-climbing-stairs/README.md diff --git a/0070-climbing-stairs/README.md b/0070-climbing-stairs/README.md new file mode 100644 index 00000000..c864654b --- /dev/null +++ b/0070-climbing-stairs/README.md @@ -0,0 +1,31 @@ +

70. Climbing Stairs

Easy


You are climbing a staircase. It takes n steps to reach the top.

+ +

Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?

+ +

 

+

Example 1:

+ +
Input: n = 2
+Output: 2
+Explanation: There are two ways to climb to the top.
+1. 1 step + 1 step
+2. 2 steps
+
+ +

Example 2:

+ +
Input: n = 3
+Output: 3
+Explanation: There are three ways to climb to the top.
+1. 1 step + 1 step + 1 step
+2. 1 step + 2 steps
+3. 2 steps + 1 step
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= 45
  • +
+
\ No newline at end of file From d94abc6377dbbd47f1ed4bd668a0331eb913fc66 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 18 Jan 2024 19:56:43 +0530 Subject: [PATCH 0636/1399] Time: 0 ms (100.00%), Space: 6.4 MB (59.54%) - LeetHub --- 0070-climbing-stairs/0070-climbing-stairs.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 0070-climbing-stairs/0070-climbing-stairs.cpp diff --git a/0070-climbing-stairs/0070-climbing-stairs.cpp b/0070-climbing-stairs/0070-climbing-stairs.cpp new file mode 100644 index 00000000..fbaea400 --- /dev/null +++ b/0070-climbing-stairs/0070-climbing-stairs.cpp @@ -0,0 +1,19 @@ +class Solution { +public: + int climbStairs(int n) { + + if(n == 1) + return 1; + + int prev2 = 1, prev = 1, res = 1; + + for(int i = 2; i <= n; ++i) + { + res = prev2 + prev; + prev = prev2; + prev2 = res; + } + + return res; + } +}; \ No newline at end of file From 5684bfab4a433c66e05e01a59975c0d5349ae419 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 20 Jan 2024 01:41:55 +0530 Subject: [PATCH 0637/1399] Create README - LeetHub --- 0931-minimum-falling-path-sum/README.md | 28 +++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 0931-minimum-falling-path-sum/README.md diff --git a/0931-minimum-falling-path-sum/README.md b/0931-minimum-falling-path-sum/README.md new file mode 100644 index 00000000..64228ea5 --- /dev/null +++ b/0931-minimum-falling-path-sum/README.md @@ -0,0 +1,28 @@ +

931. Minimum Falling Path Sum

Medium


Given an n x n array of integers matrix, return the minimum sum of any falling path through matrix.

+ +

A falling path starts at any element in the first row and chooses the element in the next row that is either directly below or diagonally left/right. Specifically, the next element from position (row, col) will be (row + 1, col - 1), (row + 1, col), or (row + 1, col + 1).

+ +

 

+

Example 1:

+ +
Input: matrix = [[2,1,3],[6,5,4],[7,8,9]]
+Output: 13
+Explanation: There are two falling paths with a minimum sum as shown.
+
+ +

Example 2:

+ +
Input: matrix = [[-19,57],[-40,-5]]
+Output: -59
+Explanation: The falling path with a minimum sum is shown.
+
+ +

 

+

Constraints:

+ +
    +
  • n == matrix.length == matrix[i].length
  • +
  • 1 <= n <= 100
  • +
  • -100 <= matrix[i][j] <= 100
  • +
+
\ No newline at end of file From 7b9b9d69cbfafe0b57c4747ccf9b26beaecde5ff Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 20 Jan 2024 01:41:55 +0530 Subject: [PATCH 0638/1399] Attach NOTES - LeetHub --- 0931-minimum-falling-path-sum/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0931-minimum-falling-path-sum/NOTES.md diff --git a/0931-minimum-falling-path-sum/NOTES.md b/0931-minimum-falling-path-sum/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0931-minimum-falling-path-sum/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 4532228220a08e2a300433ff7208c257299341a8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 20 Jan 2024 01:41:59 +0530 Subject: [PATCH 0639/1399] Time: 11 ms (80.62%), Space: 13.2 MB (5.08%) - LeetHub --- .../0931-minimum-falling-path-sum.cpp | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 0931-minimum-falling-path-sum/0931-minimum-falling-path-sum.cpp diff --git a/0931-minimum-falling-path-sum/0931-minimum-falling-path-sum.cpp b/0931-minimum-falling-path-sum/0931-minimum-falling-path-sum.cpp new file mode 100644 index 00000000..f480dd49 --- /dev/null +++ b/0931-minimum-falling-path-sum/0931-minimum-falling-path-sum.cpp @@ -0,0 +1,21 @@ +class Solution { +public: + int minFallingPathSum(vector>& A) { + int m = A.size(); + vector> t(m, vector(m)); + + for(int col = 0; col Date: Sun, 21 Jan 2024 21:38:53 +0530 Subject: [PATCH 0640/1399] Create README - LeetHub --- .../README.md | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 0987-vertical-order-traversal-of-a-binary-tree/README.md diff --git a/0987-vertical-order-traversal-of-a-binary-tree/README.md b/0987-vertical-order-traversal-of-a-binary-tree/README.md new file mode 100644 index 00000000..6ca03394 --- /dev/null +++ b/0987-vertical-order-traversal-of-a-binary-tree/README.md @@ -0,0 +1,50 @@ +

987. Vertical Order Traversal of a Binary Tree

Hard


Given the root of a binary tree, calculate the vertical order traversal of the binary tree.

+ +

For each node at position (row, col), its left and right children will be at positions (row + 1, col - 1) and (row + 1, col + 1) respectively. The root of the tree is at (0, 0).

+ +

The vertical order traversal of a binary tree is a list of top-to-bottom orderings for each column index starting from the leftmost column and ending on the rightmost column. There may be multiple nodes in the same row and same column. In such a case, sort these nodes by their values.

+ +

Return the vertical order traversal of the binary tree.

+ +

 

+

Example 1:

+ +
Input: root = [3,9,20,null,null,15,7]
+Output: [[9],[3,15],[20],[7]]
+Explanation:
+Column -1: Only node 9 is in this column.
+Column 0: Nodes 3 and 15 are in this column in that order from top to bottom.
+Column 1: Only node 20 is in this column.
+Column 2: Only node 7 is in this column.
+ +

Example 2:

+ +
Input: root = [1,2,3,4,5,6,7]
+Output: [[4],[2],[1,5,6],[3],[7]]
+Explanation:
+Column -2: Only node 4 is in this column.
+Column -1: Only node 2 is in this column.
+Column 0: Nodes 1, 5, and 6 are in this column.
+          1 is at the top, so it comes first.
+          5 and 6 are at the same position (2, 0), so we order them by their value, 5 before 6.
+Column 1: Only node 3 is in this column.
+Column 2: Only node 7 is in this column.
+
+ +

Example 3:

+ +
Input: root = [1,2,3,4,6,5,7]
+Output: [[4],[2],[1,5,6],[3],[7]]
+Explanation:
+This case is the exact same as example 2, but with nodes 5 and 6 swapped.
+Note that the solution remains the same since 5 and 6 are in the same location and should be ordered by their values.
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is in the range [1, 1000].
  • +
  • 0 <= Node.val <= 1000
  • +
+
\ No newline at end of file From 399cd5de495e9761bd47b135df6742a790ccfba0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 21 Jan 2024 21:38:57 +0530 Subject: [PATCH 0641/1399] Time: 9 ms (16.40%), Space: 14.8 MB (14.12%) - LeetHub --- ...tical-order-traversal-of-a-binary-tree.cpp | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 0987-vertical-order-traversal-of-a-binary-tree/0987-vertical-order-traversal-of-a-binary-tree.cpp diff --git a/0987-vertical-order-traversal-of-a-binary-tree/0987-vertical-order-traversal-of-a-binary-tree.cpp b/0987-vertical-order-traversal-of-a-binary-tree/0987-vertical-order-traversal-of-a-binary-tree.cpp new file mode 100644 index 00000000..2fa6c38d --- /dev/null +++ b/0987-vertical-order-traversal-of-a-binary-tree/0987-vertical-order-traversal-of-a-binary-tree.cpp @@ -0,0 +1,45 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + + map>> mp; + + void helper(TreeNode* root, int id, int level) + { + if(root) + { + mp[id][level].insert(root->val); + helper(root->left, id-1, level + 1); + helper(root->right, id+1, level + 1); + } + } + + vector> verticalTraversal(TreeNode* root) { + + int id = 0; + helper(root, id, 0); + + vector> ans; + + for(auto&[f, s] : mp) + { + vector col; + for(auto& ms : s) + col.insert(col.end(),ms.second.begin(),ms.second.end()); + ans.push_back(col); + } + + return ans; + + } +}; \ No newline at end of file From a091e0ab3f818b170d26de5b84ecb1ec004b75a2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 22 Jan 2024 16:30:29 +0530 Subject: [PATCH 0642/1399] Create README - LeetHub --- 0645-set-mismatch/README.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 0645-set-mismatch/README.md diff --git a/0645-set-mismatch/README.md b/0645-set-mismatch/README.md new file mode 100644 index 00000000..78699612 --- /dev/null +++ b/0645-set-mismatch/README.md @@ -0,0 +1,22 @@ +

645. Set Mismatch

Easy


You have a set of integers s, which originally contains all the numbers from 1 to n. Unfortunately, due to some error, one of the numbers in s got duplicated to another number in the set, which results in repetition of one number and loss of another number.

+ +

You are given an integer array nums representing the data status of this set after the error.

+ +

Find the number that occurs twice and the number that is missing and return them in the form of an array.

+ +

 

+

Example 1:

+
Input: nums = [1,2,2,4]
+Output: [2,3]
+

Example 2:

+
Input: nums = [1,1]
+Output: [1,2]
+
+

 

+

Constraints:

+ +
    +
  • 2 <= nums.length <= 104
  • +
  • 1 <= nums[i] <= 104
  • +
+
\ No newline at end of file From 333c97e57b81642c53a90c5b2ceab0253b337f01 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 22 Jan 2024 16:30:33 +0530 Subject: [PATCH 0643/1399] Time: 19 ms (89.24%), Space: 23.7 MB (36.81%) - LeetHub --- 0645-set-mismatch/0645-set-mismatch.cpp | 28 +++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 0645-set-mismatch/0645-set-mismatch.cpp diff --git a/0645-set-mismatch/0645-set-mismatch.cpp b/0645-set-mismatch/0645-set-mismatch.cpp new file mode 100644 index 00000000..37bdfca6 --- /dev/null +++ b/0645-set-mismatch/0645-set-mismatch.cpp @@ -0,0 +1,28 @@ +class Solution { +public: + vector findErrorNums(vector& nums) { + + int n = nums.size(); + int missing = -1, repeat = -1; + + for(int i = 0; i < n; ++i) + { + if(nums[abs(nums[i])-1] < 0) + { + repeat = abs(nums[i]); + } + else + { + nums[abs(nums[i])-1] *= -1; + } + } + + for(int i = 0; i < n; ++i) + { + if(nums[i] > 0) + missing = i + 1; + } + + return {repeat, missing}; + } +}; \ No newline at end of file From 7f65b82893fe8ba8e27db89b75d02c118f46efe6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 22 Jan 2024 16:30:39 +0530 Subject: [PATCH 0644/1399] Attach NOTES - LeetHub --- 0645-set-mismatch/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0645-set-mismatch/NOTES.md diff --git a/0645-set-mismatch/NOTES.md b/0645-set-mismatch/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0645-set-mismatch/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From a05cf7300458fce66fbcc4cac3b8798cb0d14aea Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 23 Jan 2024 23:32:30 +0530 Subject: [PATCH 0645/1399] Create README - LeetHub --- .../README.md | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 1239-maximum-length-of-a-concatenated-string-with-unique-characters/README.md diff --git a/1239-maximum-length-of-a-concatenated-string-with-unique-characters/README.md b/1239-maximum-length-of-a-concatenated-string-with-unique-characters/README.md new file mode 100644 index 00000000..32b1a080 --- /dev/null +++ b/1239-maximum-length-of-a-concatenated-string-with-unique-characters/README.md @@ -0,0 +1,44 @@ +

1239. Maximum Length of a Concatenated String with Unique Characters

Medium


You are given an array of strings arr. A string s is formed by the concatenation of a subsequence of arr that has unique characters.

+ +

Return the maximum possible length of s.

+ +

A subsequence is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.

+ +

 

+

Example 1:

+ +
Input: arr = ["un","iq","ue"]
+Output: 4
+Explanation: All the valid concatenations are:
+- ""
+- "un"
+- "iq"
+- "ue"
+- "uniq" ("un" + "iq")
+- "ique" ("iq" + "ue")
+Maximum length is 4.
+
+ +

Example 2:

+ +
Input: arr = ["cha","r","act","ers"]
+Output: 6
+Explanation: Possible longest valid concatenations are "chaers" ("cha" + "ers") and "acters" ("act" + "ers").
+
+ +

Example 3:

+ +
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
+Output: 26
+Explanation: The only string in arr has all 26 characters.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= arr.length <= 16
  • +
  • 1 <= arr[i].length <= 26
  • +
  • arr[i] contains only lowercase English letters.
  • +
+
\ No newline at end of file From 43858fd8257472fc4be6c27c345fd135149522b4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 23 Jan 2024 23:32:34 +0530 Subject: [PATCH 0646/1399] Time: 166 ms (35.99%), Space: 62.5 MB (35.27%) - LeetHub --- ...concatenated-string-with-unique-characters.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 1239-maximum-length-of-a-concatenated-string-with-unique-characters/1239-maximum-length-of-a-concatenated-string-with-unique-characters.cpp diff --git a/1239-maximum-length-of-a-concatenated-string-with-unique-characters/1239-maximum-length-of-a-concatenated-string-with-unique-characters.cpp b/1239-maximum-length-of-a-concatenated-string-with-unique-characters/1239-maximum-length-of-a-concatenated-string-with-unique-characters.cpp new file mode 100644 index 00000000..081c955c --- /dev/null +++ b/1239-maximum-length-of-a-concatenated-string-with-unique-characters/1239-maximum-length-of-a-concatenated-string-with-unique-characters.cpp @@ -0,0 +1,15 @@ +class Solution { +public: + int maxLength(vector& arr, string str = "", int index = 0) { + + unordered_sets(str.begin(), str.end()); + if (s.size() != ((int)str.length())) + return 0; + + int ret = str.length(); + for (int i = index; i < arr.size(); i++) + ret = max(ret, maxLength(arr, str+arr[i], i+1)); + + return ret; + } +}; \ No newline at end of file From 6d0a1bd1fdc622360c61656566dc312e71d380ab Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 24 Jan 2024 20:48:02 +0530 Subject: [PATCH 0647/1399] Create README - LeetHub --- .../README.md | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 1457-pseudo-palindromic-paths-in-a-binary-tree/README.md diff --git a/1457-pseudo-palindromic-paths-in-a-binary-tree/README.md b/1457-pseudo-palindromic-paths-in-a-binary-tree/README.md new file mode 100644 index 00000000..746fb259 --- /dev/null +++ b/1457-pseudo-palindromic-paths-in-a-binary-tree/README.md @@ -0,0 +1,37 @@ +

1457. Pseudo-Palindromic Paths in a Binary Tree

Medium


Given a binary tree where node values are digits from 1 to 9. A path in the binary tree is said to be pseudo-palindromic if at least one permutation of the node values in the path is a palindrome.

+ +

Return the number of pseudo-palindromic paths going from the root node to leaf nodes.

+ +

 

+

Example 1:

+ +

+ +
Input: root = [2,3,1,3,1,null,1]
+Output: 2 
+Explanation: The figure above represents the given binary tree. There are three paths going from the root node to leaf nodes: the red path [2,3,3], the green path [2,1,1], and the path [2,3,1]. Among these paths only red path and green path are pseudo-palindromic paths since the red path [2,3,3] can be rearranged in [3,2,3] (palindrome) and the green path [2,1,1] can be rearranged in [1,2,1] (palindrome).
+
+ +

Example 2:

+ +

+ +
Input: root = [2,1,1,1,3,null,null,null,null,null,1]
+Output: 1 
+Explanation: The figure above represents the given binary tree. There are three paths going from the root node to leaf nodes: the green path [2,1,1], the path [2,1,3,1], and the path [2,1]. Among these paths only the green path is pseudo-palindromic since [2,1,1] can be rearranged in [1,2,1] (palindrome).
+
+ +

Example 3:

+ +
Input: root = [9]
+Output: 1
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is in the range [1, 105].
  • +
  • 1 <= Node.val <= 9
  • +
+
\ No newline at end of file From 8b512b8c4fb6ca2277d696d8f5214772b527fc17 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 24 Jan 2024 20:48:08 +0530 Subject: [PATCH 0648/1399] Time: 1988 ms (5.09%), Space: 448.5 MB (8.04%) - LeetHub --- ...udo-palindromic-paths-in-a-binary-tree.cpp | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 1457-pseudo-palindromic-paths-in-a-binary-tree/1457-pseudo-palindromic-paths-in-a-binary-tree.cpp diff --git a/1457-pseudo-palindromic-paths-in-a-binary-tree/1457-pseudo-palindromic-paths-in-a-binary-tree.cpp b/1457-pseudo-palindromic-paths-in-a-binary-tree/1457-pseudo-palindromic-paths-in-a-binary-tree.cpp new file mode 100644 index 00000000..1370abd3 --- /dev/null +++ b/1457-pseudo-palindromic-paths-in-a-binary-tree/1457-pseudo-palindromic-paths-in-a-binary-tree.cpp @@ -0,0 +1,51 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + + void helper(TreeNode* root, vector freq, int& cnt) + { + if(root) + { + ++freq[root->val]; + + helper(root->left, freq, cnt); + helper(root->right, freq, cnt); + + int odd = 0; + + if(!root->left and !root->right) + { + for(int i = 0; i < 10; ++i) + { + odd += (freq[i] & 1); + } + + cnt += (odd == 1 or odd == 0); + } + + --freq[root->val]; + } + } + + int pseudoPalindromicPaths (TreeNode* root) { + + int cnt = 0; + + vector freq(10, 0); + + helper(root, freq, cnt); + + return cnt; + + } +}; \ No newline at end of file From 0e323e5928c632e487ffce7e028fc20820f6ed3b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 25 Jan 2024 19:21:19 +0530 Subject: [PATCH 0649/1399] Attach NOTES - LeetHub --- 1143-longest-common-subsequence/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1143-longest-common-subsequence/NOTES.md diff --git a/1143-longest-common-subsequence/NOTES.md b/1143-longest-common-subsequence/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1143-longest-common-subsequence/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From a15d0d194685f9e971748306eaa3a2cd847c341d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 26 Jan 2024 08:24:05 +0530 Subject: [PATCH 0650/1399] Create README - LeetHub --- 0576-out-of-boundary-paths/README.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 0576-out-of-boundary-paths/README.md diff --git a/0576-out-of-boundary-paths/README.md b/0576-out-of-boundary-paths/README.md new file mode 100644 index 00000000..25a688e2 --- /dev/null +++ b/0576-out-of-boundary-paths/README.md @@ -0,0 +1,27 @@ +

576. Out of Boundary Paths

Medium


There is an m x n grid with a ball. The ball is initially at the position [startRow, startColumn]. You are allowed to move the ball to one of the four adjacent cells in the grid (possibly out of the grid crossing the grid boundary). You can apply at most maxMove moves to the ball.

+ +

Given the five integers m, n, maxMove, startRow, startColumn, return the number of paths to move the ball out of the grid boundary. Since the answer can be very large, return it modulo 109 + 7.

+ +

 

+

Example 1:

+ +
Input: m = 2, n = 2, maxMove = 2, startRow = 0, startColumn = 0
+Output: 6
+
+ +

Example 2:

+ +
Input: m = 1, n = 3, maxMove = 3, startRow = 0, startColumn = 1
+Output: 12
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= m, n <= 50
  • +
  • 0 <= maxMove <= 50
  • +
  • 0 <= startRow < m
  • +
  • 0 <= startColumn < n
  • +
+
\ No newline at end of file From 72bc031e708131dd498dce808505fea1073cf352 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 26 Jan 2024 08:24:06 +0530 Subject: [PATCH 0651/1399] Attach NOTES - LeetHub --- 0576-out-of-boundary-paths/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0576-out-of-boundary-paths/NOTES.md diff --git a/0576-out-of-boundary-paths/NOTES.md b/0576-out-of-boundary-paths/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0576-out-of-boundary-paths/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 7f00936f3f616f903a65de2a270c51547b6c6445 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 26 Jan 2024 08:24:09 +0530 Subject: [PATCH 0652/1399] Time: 5 ms (77.51%), Space: 7.8 MB (57.43%) - LeetHub --- .../0576-out-of-boundary-paths.cpp | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 0576-out-of-boundary-paths/0576-out-of-boundary-paths.cpp diff --git a/0576-out-of-boundary-paths/0576-out-of-boundary-paths.cpp b/0576-out-of-boundary-paths/0576-out-of-boundary-paths.cpp new file mode 100644 index 00000000..379ead6e --- /dev/null +++ b/0576-out-of-boundary-paths/0576-out-of-boundary-paths.cpp @@ -0,0 +1,36 @@ +class Solution { +public: + + int dp[55][55][55]; + + const int mod = 1e9+7; + + int boundaryPaths(int i, int j, int n, int m, int maxMove) + { + if(maxMove >= 0 and i < 0 or j < 0 or i >= n or j >= m) + return 1; + + if(dp[i][j][maxMove] != -1) + return dp[i][j][maxMove]; + + int up = 0, right = 0, down = 0, left = 0; + + if(maxMove) + { + up = boundaryPaths(i-1, j, n, m, maxMove-1); + right = boundaryPaths(i, j+1, n, m, maxMove-1); + down = boundaryPaths(i+1, j, n, m, maxMove-1); + left = boundaryPaths(i, j-1, n, m, maxMove-1); + } + + return dp[i][j][maxMove] = ((((up%mod + right%mod)%mod + down%mod)%mod + left%mod) % mod); + } + + int findPaths(int m, int n, int maxMove, int startRow, int startColumn) { + + memset(dp, -1, sizeof(dp)); + + return boundaryPaths(startRow, startColumn, m, n, maxMove); + + } +}; \ No newline at end of file From c2722da4f22ab60c10078b395433173312dc10e4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 26 Jan 2024 21:24:49 +0530 Subject: [PATCH 0653/1399] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1239-maximum-length-of-a-concatenated-string-with-unique-characters/NOTES.md diff --git a/1239-maximum-length-of-a-concatenated-string-with-unique-characters/NOTES.md b/1239-maximum-length-of-a-concatenated-string-with-unique-characters/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1239-maximum-length-of-a-concatenated-string-with-unique-characters/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 9d00210669032aaf39b4bbbce181e178f0f2cc49 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 26 Jan 2024 21:24:53 +0530 Subject: [PATCH 0654/1399] Time: 862 ms (5.01%), Space: 540.5 MB (5.01%) - LeetHub --- ...atenated-string-with-unique-characters.cpp | 63 ++++++++++++++++--- 1 file changed, 53 insertions(+), 10 deletions(-) diff --git a/1239-maximum-length-of-a-concatenated-string-with-unique-characters/1239-maximum-length-of-a-concatenated-string-with-unique-characters.cpp b/1239-maximum-length-of-a-concatenated-string-with-unique-characters/1239-maximum-length-of-a-concatenated-string-with-unique-characters.cpp index 081c955c..dfde3d51 100644 --- a/1239-maximum-length-of-a-concatenated-string-with-unique-characters/1239-maximum-length-of-a-concatenated-string-with-unique-characters.cpp +++ b/1239-maximum-length-of-a-concatenated-string-with-unique-characters/1239-maximum-length-of-a-concatenated-string-with-unique-characters.cpp @@ -1,15 +1,58 @@ class Solution { public: - int maxLength(vector& arr, string str = "", int index = 0) { - - unordered_sets(str.begin(), str.end()); - if (s.size() != ((int)str.length())) - return 0; - - int ret = str.length(); - for (int i = index; i < arr.size(); i++) - ret = max(ret, maxLength(arr, str+arr[i], i+1)); + + void helper(int idx, int n, string str, set st, int& maxLength, vector& arr) + { + if(idx == n) + { + maxLength = max(maxLength, (int)str.size()); + return; + } + + helper(idx+1, n, str, st, maxLength, arr); + + bool canTake = true; + + set curr; + + for(auto& ch : arr[idx]) + { + if(curr.count(ch)) + return; + else + curr.insert(ch); + } + + if(canTake) + { + for(auto& ch : curr) + { + if(st.count(ch)) + return; + } + } + + + for(auto& ch : curr) + st.insert(ch); + + helper(idx+1, n, str + arr[idx], st, maxLength, arr); - return ret; + for(auto& ch : curr) + st.erase(ch); + + } + + int maxLength(vector& arr) { + + int n = arr.size(); + + set st; + string str; + int maxLength = 0; + + helper(0, n, str, st, maxLength, arr); + + return maxLength; } }; \ No newline at end of file From 791db53f7625c121f391a3cc1a91624ebae764e9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 27 Jan 2024 23:19:00 +0530 Subject: [PATCH 0655/1399] Create README - LeetHub --- 0629-k-inverse-pairs-array/README.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 0629-k-inverse-pairs-array/README.md diff --git a/0629-k-inverse-pairs-array/README.md b/0629-k-inverse-pairs-array/README.md new file mode 100644 index 00000000..b9c1e1ff --- /dev/null +++ b/0629-k-inverse-pairs-array/README.md @@ -0,0 +1,27 @@ +

629. K Inverse Pairs Array

Hard


For an integer array nums, an inverse pair is a pair of integers [i, j] where 0 <= i < j < nums.length and nums[i] > nums[j].

+ +

Given two integers n and k, return the number of different arrays consist of numbers from 1 to n such that there are exactly k inverse pairs. Since the answer can be huge, return it modulo 109 + 7.

+ +

 

+

Example 1:

+ +
Input: n = 3, k = 0
+Output: 1
+Explanation: Only the array [1,2,3] which consists of numbers from 1 to 3 has exactly 0 inverse pairs.
+
+ +

Example 2:

+ +
Input: n = 3, k = 1
+Output: 2
+Explanation: The array [1,3,2] and [2,1,3] have exactly 1 inverse pair.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= 1000
  • +
  • 0 <= k <= 1000
  • +
+
\ No newline at end of file From 7dab5db81ffb164f290cbc02e21d13cb13a139e9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 27 Jan 2024 23:19:04 +0530 Subject: [PATCH 0656/1399] Time: 15 ms (66.48%), Space: 17.5 MB (13.19%) - LeetHub --- .../0629-k-inverse-pairs-array.cpp | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 0629-k-inverse-pairs-array/0629-k-inverse-pairs-array.cpp diff --git a/0629-k-inverse-pairs-array/0629-k-inverse-pairs-array.cpp b/0629-k-inverse-pairs-array/0629-k-inverse-pairs-array.cpp new file mode 100644 index 00000000..44bda9a0 --- /dev/null +++ b/0629-k-inverse-pairs-array/0629-k-inverse-pairs-array.cpp @@ -0,0 +1,24 @@ +class Solution { +public: + int mod = 1e9+7; + int kInversePairs(int n, int K) { + //dp[i][j] denote till i numbers how many jth inversion pair-containing permutations are there + vector> dp(n+1,vector(K+1,0)); + dp[0][0]=0; + for(int i=1;i<=K;i++){ + dp[1][i]=0; + } + for(int i=1;i<=n;i++){ + dp[i][0]=1; + } +for(int i=2;i<=n;i++){ + for(int j=1;j<=K;j++){ + dp[i][j]=(dp[i-1][j]+dp[i][j-1])%mod; + if(j-i>=0){ + dp[i][j]=(dp[i][j]-dp[i-1][j-i]+mod)%mod; + } + } + } + return dp[n][K]; + } +}; From fa5ea8838a4a7d3a5657b9adb992d24977144d3a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 28 Jan 2024 23:18:47 +0530 Subject: [PATCH 0657/1399] Attach NOTES - LeetHub --- 1074-number-of-submatrices-that-sum-to-target/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1074-number-of-submatrices-that-sum-to-target/NOTES.md diff --git a/1074-number-of-submatrices-that-sum-to-target/NOTES.md b/1074-number-of-submatrices-that-sum-to-target/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1074-number-of-submatrices-that-sum-to-target/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 61830fea462114c7bcc127ccbedfe481853af7e5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 28 Jan 2024 23:18:52 +0530 Subject: [PATCH 0658/1399] Time: 678 ms (49.88%), Space: 257 MB (18.43%) - LeetHub --- ...mber-of-submatrices-that-sum-to-target.cpp | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 1074-number-of-submatrices-that-sum-to-target/1074-number-of-submatrices-that-sum-to-target.cpp diff --git a/1074-number-of-submatrices-that-sum-to-target/1074-number-of-submatrices-that-sum-to-target.cpp b/1074-number-of-submatrices-that-sum-to-target/1074-number-of-submatrices-that-sum-to-target.cpp new file mode 100644 index 00000000..1fe999aa --- /dev/null +++ b/1074-number-of-submatrices-that-sum-to-target/1074-number-of-submatrices-that-sum-to-target.cpp @@ -0,0 +1,45 @@ +class Solution { +public: + int numSubmatrixSumTarget(vector>& matrix, int target) { + + int m = matrix.size(); + int n = matrix[0].size(); + + // prefix sum + + for(int row = 0; row < m; ++row) + { + for(int col = 1; col < n; ++col) + { + matrix[row][col] += matrix[row][col-1]; + } + } + + int count = 0; + + // fix 2 colums + + for(int c1 = 0; c1 < n; ++c1) + { + for(int c2 = c1; c2 < n; ++c2) + { + unordered_map mp; + int sum = 0; + mp.insert({0,1}); + + for(int row = 0; row < m; ++row) + { + sum += matrix[row][c2] - (c1 > 0 ? matrix[row][c1-1] : 0); + count += mp[sum-target]; + + if(mp.find(sum) != mp.end()) + ++mp[sum]; + else + mp[sum] = 1; + } + } + } + + return count; + } +}; \ No newline at end of file From c7ac455f3bd688bb9e4beede8be8c448e72930a0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 30 Jan 2024 09:40:30 +0530 Subject: [PATCH 0659/1399] Attach NOTES - LeetHub --- 0150-evaluate-reverse-polish-notation/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0150-evaluate-reverse-polish-notation/NOTES.md diff --git a/0150-evaluate-reverse-polish-notation/NOTES.md b/0150-evaluate-reverse-polish-notation/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0150-evaluate-reverse-polish-notation/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From b07f91cc877a017fb09c8c2f4ee2d1491ff43b23 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 1 Feb 2024 20:12:52 +0530 Subject: [PATCH 0660/1399] Create README - LeetHub --- .../README.md | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 2966-divide-array-into-arrays-with-max-difference/README.md diff --git a/2966-divide-array-into-arrays-with-max-difference/README.md b/2966-divide-array-into-arrays-with-max-difference/README.md new file mode 100644 index 00000000..8e0024aa --- /dev/null +++ b/2966-divide-array-into-arrays-with-max-difference/README.md @@ -0,0 +1,39 @@ +

2966. Divide Array Into Arrays With Max Difference

Medium


You are given an integer array nums of size n and a positive integer k.

+ +

Divide the array into one or more arrays of size 3 satisfying the following conditions:

+ +
    +
  • Each element of nums should be in exactly one array.
  • +
  • The difference between any two elements in one array is less than or equal to k.
  • +
+ +

Return a 2D array containing all the arrays. If it is impossible to satisfy the conditions, return an empty array. And if there are multiple answers, return any of them.

+ +

 

+

Example 1:

+ +
Input: nums = [1,3,4,8,7,9,3,5,1], k = 2
+Output: [[1,1,3],[3,4,5],[7,8,9]]
+Explanation: We can divide the array into the following arrays: [1,1,3], [3,4,5] and [7,8,9].
+The difference between any two elements in each array is less than or equal to 2.
+Note that the order of elements is not important.
+
+ +

Example 2:

+ +
Input: nums = [1,3,3,2,7,3], k = 3
+Output: []
+Explanation: It is not possible to divide the array satisfying all the conditions.
+
+ +

 

+

Constraints:

+ +
    +
  • n == nums.length
  • +
  • 1 <= n <= 105
  • +
  • n is a multiple of 3.
  • +
  • 1 <= nums[i] <= 105
  • +
  • 1 <= k <= 105
  • +
+
\ No newline at end of file From 64567becb732b9842814719449a0f8d22314d25e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 1 Feb 2024 20:12:56 +0530 Subject: [PATCH 0661/1399] Time: 232 ms (23.66%), Space: 142.3 MB (7.82%) - LeetHub --- ...-array-into-arrays-with-max-difference.cpp | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 2966-divide-array-into-arrays-with-max-difference/2966-divide-array-into-arrays-with-max-difference.cpp diff --git a/2966-divide-array-into-arrays-with-max-difference/2966-divide-array-into-arrays-with-max-difference.cpp b/2966-divide-array-into-arrays-with-max-difference/2966-divide-array-into-arrays-with-max-difference.cpp new file mode 100644 index 00000000..0353e6b5 --- /dev/null +++ b/2966-divide-array-into-arrays-with-max-difference/2966-divide-array-into-arrays-with-max-difference.cpp @@ -0,0 +1,35 @@ +class Solution { +public: + vector> divideArray(vector& nums, int k) { + + int n = nums.size(); + + if(n < 3) + return {}; + + vector> output; + + sort(nums.begin(), nums.end()); + + int part = n/3; + + for(int i = 0; i < n-2; i += 3) + { + vector curr; + + if(nums[i+1] - nums[i] <= k and nums[i+2] - nums[i+1] <= k and nums[i+2] - nums[i] <= k) + { + curr.push_back(nums[i]); + curr.push_back(nums[i+1]); + curr.push_back(nums[i+2]); + } + + if(!curr.empty()) + output.push_back(curr); + } + + if((int)output.size() != part) + return {}; + return output; + } +}; \ No newline at end of file From 4e0a66b17acf519cd890ec0449c00c3e5b6cce85 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 1 Feb 2024 20:14:06 +0530 Subject: [PATCH 0662/1399] Attach NOTES - LeetHub --- 2966-divide-array-into-arrays-with-max-difference/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2966-divide-array-into-arrays-with-max-difference/NOTES.md diff --git a/2966-divide-array-into-arrays-with-max-difference/NOTES.md b/2966-divide-array-into-arrays-with-max-difference/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2966-divide-array-into-arrays-with-max-difference/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 8ebb66c3683dd626e6082876cbc221dac00eb282 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 1 Feb 2024 20:14:10 +0530 Subject: [PATCH 0663/1399] Time: 232 ms (23.66%), Space: 142.3 MB (7.82%) - LeetHub From 5e61ed21d0510eae65f87e0b3e755f1ddada6574 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 2 Feb 2024 20:30:28 +0530 Subject: [PATCH 0664/1399] Attach NOTES - LeetHub --- 1291-sequential-digits/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1291-sequential-digits/NOTES.md diff --git a/1291-sequential-digits/NOTES.md b/1291-sequential-digits/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1291-sequential-digits/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From be9c5f472b26a70fcfeb035e57fafbbca9fe123a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 2 Feb 2024 20:30:31 +0530 Subject: [PATCH 0665/1399] Time: 0 ms (100.00%), Space: 7.4 MB (9.62%) - LeetHub --- .../1291-sequential-digits.cpp | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 1291-sequential-digits/1291-sequential-digits.cpp diff --git a/1291-sequential-digits/1291-sequential-digits.cpp b/1291-sequential-digits/1291-sequential-digits.cpp new file mode 100644 index 00000000..180d3292 --- /dev/null +++ b/1291-sequential-digits/1291-sequential-digits.cpp @@ -0,0 +1,23 @@ +class Solution { +public: + vector sequentialDigits(int low, int high) { + + vector ans; + + for(int i = 1; i <=8; ++i) + { + int currNum = i; + for(int j = i+1; j <= 9; ++j) + { + currNum = (currNum * 10) + j; + + if(currNum >= low and currNum <= high) + ans.push_back(currNum); + } + } + + sort(ans.begin(), ans.end()); + + return ans; + } +}; \ No newline at end of file From 1c3b775d660dfdbdbc7aef635d2e413e4543d4d6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 4 Feb 2024 20:29:29 +0530 Subject: [PATCH 0666/1399] Create README - LeetHub --- 0076-minimum-window-substring/README.md | 40 +++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 0076-minimum-window-substring/README.md diff --git a/0076-minimum-window-substring/README.md b/0076-minimum-window-substring/README.md new file mode 100644 index 00000000..0f0823a6 --- /dev/null +++ b/0076-minimum-window-substring/README.md @@ -0,0 +1,40 @@ +

76. Minimum Window Substring

Hard


Given two strings s and t of lengths m and n respectively, return the minimum window substring of s such that every character in t (including duplicates) is included in the window. If there is no such substring, return the empty string "".

+ +

The testcases will be generated such that the answer is unique.

+ +

 

+

Example 1:

+ +
Input: s = "ADOBECODEBANC", t = "ABC"
+Output: "BANC"
+Explanation: The minimum window substring "BANC" includes 'A', 'B', and 'C' from string t.
+
+ +

Example 2:

+ +
Input: s = "a", t = "a"
+Output: "a"
+Explanation: The entire string s is the minimum window.
+
+ +

Example 3:

+ +
Input: s = "a", t = "aa"
+Output: ""
+Explanation: Both 'a's from t must be included in the window.
+Since the largest window of s only has one 'a', return empty string.
+
+ +

 

+

Constraints:

+ +
    +
  • m == s.length
  • +
  • n == t.length
  • +
  • 1 <= m, n <= 105
  • +
  • s and t consist of uppercase and lowercase English letters.
  • +
+ +

 

+

Follow up: Could you find an algorithm that runs in O(m + n) time?

+
\ No newline at end of file From cbbf5953b713c0c6eada741de398f83bda2258dd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 4 Feb 2024 20:29:32 +0530 Subject: [PATCH 0667/1399] Time: 222 ms (5.03%), Space: 372.4 MB (5.28%) - LeetHub --- .../0076-minimum-window-substring.cpp | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 0076-minimum-window-substring/0076-minimum-window-substring.cpp diff --git a/0076-minimum-window-substring/0076-minimum-window-substring.cpp b/0076-minimum-window-substring/0076-minimum-window-substring.cpp new file mode 100644 index 00000000..c0755a6f --- /dev/null +++ b/0076-minimum-window-substring/0076-minimum-window-substring.cpp @@ -0,0 +1,71 @@ +class Solution { +public: + string minWindow(string s, string t) { + + int n = s.size(); + + map freq, curr; + + for(auto& ele : t) + { + ++freq[ele]; + } + + string ans; + + int i = 0, j = 0; + + while(j < n) + { + ++curr[s[j]]; + + bool ok = true; + + for(auto&[ch, f] : freq) + { + if(curr[ch] < f) + { + ok = false; + break; + } + } + + if(ok) + { + while(true) + { + if(freq.find(s[i]) == freq.end()) + { + --curr[s[i]]; + ++i; + } + else if(curr[s[i]] > freq[s[i]]) + { + --curr[s[i]]; + ++i; + } + else + break; + + } + + if(ans.empty()) + { + ans = s.substr(i, j - i + 1); + } + else{ + string curr = s.substr(i, j - i + 1); + if(curr.size() < ans.size()) + { + ans = curr; + } + } + } + ++j; + } + + + + return ans; + } +}; \ No newline at end of file From 7ea41d29a696ae9ecb89b8a8fa082608e83a85a5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 5 Feb 2024 09:17:04 +0530 Subject: [PATCH 0668/1399] Create README - LeetHub --- .../README.md | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 0387-first-unique-character-in-a-string/README.md diff --git a/0387-first-unique-character-in-a-string/README.md b/0387-first-unique-character-in-a-string/README.md new file mode 100644 index 00000000..e06661f4 --- /dev/null +++ b/0387-first-unique-character-in-a-string/README.md @@ -0,0 +1,21 @@ +

387. First Unique Character in a String

Easy


Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1.

+ +

 

+

Example 1:

+
Input: s = "leetcode"
+Output: 0
+

Example 2:

+
Input: s = "loveleetcode"
+Output: 2
+

Example 3:

+
Input: s = "aabb"
+Output: -1
+
+

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 105
  • +
  • s consists of only lowercase English letters.
  • +
+
\ No newline at end of file From 8593544cf15064f0bb66a5db71ecffd9da2eff14 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 5 Feb 2024 09:17:05 +0530 Subject: [PATCH 0669/1399] Attach NOTES - LeetHub --- 0387-first-unique-character-in-a-string/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0387-first-unique-character-in-a-string/NOTES.md diff --git a/0387-first-unique-character-in-a-string/NOTES.md b/0387-first-unique-character-in-a-string/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0387-first-unique-character-in-a-string/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From c29f5bae408800cdaeeb95dcbcfe4e5cafb0f198 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 5 Feb 2024 09:17:08 +0530 Subject: [PATCH 0670/1399] Time: 56 ms (17.72%), Space: 13.2 MB (10.77%) - LeetHub --- ...387-first-unique-character-in-a-string.cpp | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 0387-first-unique-character-in-a-string/0387-first-unique-character-in-a-string.cpp diff --git a/0387-first-unique-character-in-a-string/0387-first-unique-character-in-a-string.cpp b/0387-first-unique-character-in-a-string/0387-first-unique-character-in-a-string.cpp new file mode 100644 index 00000000..654d516d --- /dev/null +++ b/0387-first-unique-character-in-a-string/0387-first-unique-character-in-a-string.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + int firstUniqChar(string s) { + + int n = s.size(); + + map mp; + + for(auto& ch : s) + ++mp[ch]; + + for(int i = 0; i < n; ++i) + { + if(mp[s[i]] == 1) + return i; + } + + return -1; + } +}; \ No newline at end of file From 9d726dcec9eac6d7b972198dff71c73025b4ccb7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 5 Feb 2024 09:17:58 +0530 Subject: [PATCH 0671/1399] Attach NOTES - LeetHub From f49757eb0be568fe6f1bc43a4d1b26e215371dfe Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 6 Feb 2024 08:43:28 +0530 Subject: [PATCH 0672/1399] Create README - LeetHub --- 0049-group-anagrams/README.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 0049-group-anagrams/README.md diff --git a/0049-group-anagrams/README.md b/0049-group-anagrams/README.md new file mode 100644 index 00000000..3da623eb --- /dev/null +++ b/0049-group-anagrams/README.md @@ -0,0 +1,24 @@ +

49. Group Anagrams

Medium


Given an array of strings strs, group the anagrams together. You can return the answer in any order.

+ +

An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

+ +

 

+

Example 1:

+
Input: strs = ["eat","tea","tan","ate","nat","bat"]
+Output: [["bat"],["nat","tan"],["ate","eat","tea"]]
+

Example 2:

+
Input: strs = [""]
+Output: [[""]]
+

Example 3:

+
Input: strs = ["a"]
+Output: [["a"]]
+
+

 

+

Constraints:

+ +
    +
  • 1 <= strs.length <= 104
  • +
  • 0 <= strs[i].length <= 100
  • +
  • strs[i] consists of lowercase English letters.
  • +
+
\ No newline at end of file From c61c8d7e607c19b61e795a831d7bdc3bdf8a2342 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 8 Feb 2024 22:16:28 +0530 Subject: [PATCH 0673/1399] Create README - LeetHub --- 0279-perfect-squares/README.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 0279-perfect-squares/README.md diff --git a/0279-perfect-squares/README.md b/0279-perfect-squares/README.md new file mode 100644 index 00000000..cfd0d433 --- /dev/null +++ b/0279-perfect-squares/README.md @@ -0,0 +1,26 @@ +

279. Perfect Squares

Medium


Given an integer n, return the least number of perfect square numbers that sum to n.

+ +

A perfect square is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, 1, 4, 9, and 16 are perfect squares while 3 and 11 are not.

+ +

 

+

Example 1:

+ +
Input: n = 12
+Output: 3
+Explanation: 12 = 4 + 4 + 4.
+
+ +

Example 2:

+ +
Input: n = 13
+Output: 2
+Explanation: 13 = 4 + 9.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= 104
  • +
+
\ No newline at end of file From 3897f76e3c61bdc4b7d3cc4f237e7ba9bd882c2f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 10 Feb 2024 00:26:40 +0530 Subject: [PATCH 0674/1399] Create README - LeetHub --- 0368-largest-divisible-subset/README.md | 32 +++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 0368-largest-divisible-subset/README.md diff --git a/0368-largest-divisible-subset/README.md b/0368-largest-divisible-subset/README.md new file mode 100644 index 00000000..ae432a88 --- /dev/null +++ b/0368-largest-divisible-subset/README.md @@ -0,0 +1,32 @@ +

368. Largest Divisible Subset

Medium


Given a set of distinct positive integers nums, return the largest subset answer such that every pair (answer[i], answer[j]) of elements in this subset satisfies:

+ +
    +
  • answer[i] % answer[j] == 0, or
  • +
  • answer[j] % answer[i] == 0
  • +
+ +

If there are multiple solutions, return any of them.

+ +

 

+

Example 1:

+ +
Input: nums = [1,2,3]
+Output: [1,2]
+Explanation: [1,3] is also accepted.
+
+ +

Example 2:

+ +
Input: nums = [1,2,4,8]
+Output: [1,2,4,8]
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 1000
  • +
  • 1 <= nums[i] <= 2 * 109
  • +
  • All the integers in nums are unique.
  • +
+
\ No newline at end of file From e984f0bb2ab0db5062101d13ae8d99eb35635b4d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 10 Feb 2024 00:26:44 +0530 Subject: [PATCH 0675/1399] Time: 18 ms (71.92%), Space: 11.3 MB (40.54%) - LeetHub --- .../0368-largest-divisible-subset.cpp | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 0368-largest-divisible-subset/0368-largest-divisible-subset.cpp diff --git a/0368-largest-divisible-subset/0368-largest-divisible-subset.cpp b/0368-largest-divisible-subset/0368-largest-divisible-subset.cpp new file mode 100644 index 00000000..d5cc332d --- /dev/null +++ b/0368-largest-divisible-subset/0368-largest-divisible-subset.cpp @@ -0,0 +1,43 @@ +class Solution { +public: + vector largestDivisibleSubset(vector& nums) { + + int n = nums.size(); + + vector dp(n+1,1); + vector hash(n); + int lastIndex = -1; + int res = 1; + sort(nums.begin(),nums.end()); + + for(int i = 0; i dp[i]) + { + dp[i] = dp[j] + 1; + hash[i] = j; + } + } + if(dp[i] > res) + { + lastIndex = i; + res = dp[i]; + } + } + if(res == 1) + return {nums[0]}; + vector ans; + ans.push_back(nums[lastIndex]); + + while(hash[lastIndex] != lastIndex) + { + lastIndex = hash[lastIndex]; + ans.push_back(nums[lastIndex]); + } + + return ans; + } +}; \ No newline at end of file From d91f5ea5477b4357b5d8aaa7de615531e7e65be5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 10 Feb 2024 11:27:27 +0530 Subject: [PATCH 0676/1399] Create README - LeetHub --- 0647-palindromic-substrings/README.md | 29 +++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 0647-palindromic-substrings/README.md diff --git a/0647-palindromic-substrings/README.md b/0647-palindromic-substrings/README.md new file mode 100644 index 00000000..f833b17e --- /dev/null +++ b/0647-palindromic-substrings/README.md @@ -0,0 +1,29 @@ +

647. Palindromic Substrings

Medium


Given a string s, return the number of palindromic substrings in it.

+ +

A string is a palindrome when it reads the same backward as forward.

+ +

A substring is a contiguous sequence of characters within the string.

+ +

 

+

Example 1:

+ +
Input: s = "abc"
+Output: 3
+Explanation: Three palindromic strings: "a", "b", "c".
+
+ +

Example 2:

+ +
Input: s = "aaa"
+Output: 6
+Explanation: Six palindromic strings: "a", "a", "a", "aa", "aa", "aaa".
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 1000
  • +
  • s consists of lowercase English letters.
  • +
+
\ No newline at end of file From beac8a8c2f71f1c41a3c7ea762cca531328f63c0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 10 Feb 2024 11:27:31 +0530 Subject: [PATCH 0677/1399] Time: 16 ms (35.55%), Space: 21.6 MB (12.92%) - LeetHub --- .../0647-palindromic-substrings.cpp | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 0647-palindromic-substrings/0647-palindromic-substrings.cpp diff --git a/0647-palindromic-substrings/0647-palindromic-substrings.cpp b/0647-palindromic-substrings/0647-palindromic-substrings.cpp new file mode 100644 index 00000000..750babdc --- /dev/null +++ b/0647-palindromic-substrings/0647-palindromic-substrings.cpp @@ -0,0 +1,35 @@ +class Solution { +public: + int countSubstrings(string s) { + + int n = s.size(); + + vector> dp(n+1, vector(n+1, 0)); + + int cnt = 0; + + for(int diff = 0; diff < n; ++diff) + { + for(int i = 0, j = diff; i < n and j < n; ++i, ++j) + { + if(i == j) + { + ++cnt; + dp[i][j] = 1; + } + else if(diff == 1 and s[i] == s[j]) + { + ++cnt; + dp[i][j] = 2; + } + else if(diff > 1 and s[i] == s[j] and dp[i+1][j-1]) + { + ++cnt; + dp[i][j] = dp[i+1][j-1] + 2; + } + } + } + + return cnt; + } +}; \ No newline at end of file From e6c26d1c74afd68d9b3ec8f90072ecc57dfb4adb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 11 Feb 2024 17:38:47 +0530 Subject: [PATCH 0678/1399] Create README - LeetHub --- .../README.md | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 3035-maximum-palindromes-after-operations/README.md diff --git a/3035-maximum-palindromes-after-operations/README.md b/3035-maximum-palindromes-after-operations/README.md new file mode 100644 index 00000000..d4aba112 --- /dev/null +++ b/3035-maximum-palindromes-after-operations/README.md @@ -0,0 +1,51 @@ +

3035. Maximum Palindromes After Operations

Medium


You are given a 0-indexed string array words having length n and containing 0-indexed strings.

+ +

You are allowed to perform the following operation any number of times (including zero):

+ +
    +
  • Choose integers i, j, x, and y such that 0 <= i, j < n, 0 <= x < words[i].length, 0 <= y < words[j].length, and swap the characters words[i][x] and words[j][y].
  • +
+ +

Return an integer denoting the maximum number of palindromes words can contain, after performing some operations.

+ +

Note: i and j may be equal during an operation.

+ +

 

+

Example 1:

+ +
Input: words = ["abbb","ba","aa"]
+Output: 3
+Explanation: In this example, one way to get the maximum number of palindromes is:
+Choose i = 0, j = 1, x = 0, y = 0, so we swap words[0][0] and words[1][0]. words becomes ["bbbb","aa","aa"].
+All strings in words are now palindromes.
+Hence, the maximum number of palindromes achievable is 3.
+ +

Example 2:

+ +
Input: words = ["abc","ab"]
+Output: 2
+Explanation: In this example, one way to get the maximum number of palindromes is: 
+Choose i = 0, j = 1, x = 1, y = 0, so we swap words[0][1] and words[1][0]. words becomes ["aac","bb"].
+Choose i = 0, j = 0, x = 1, y = 2, so we swap words[0][1] and words[0][2]. words becomes ["aca","bb"].
+Both strings are now palindromes.
+Hence, the maximum number of palindromes achievable is 2.
+
+ +

Example 3:

+ +
Input: words = ["cd","ef","a"]
+Output: 1
+Explanation: In this example, there is no need to perform any operation.
+There is one palindrome in words "a".
+It can be shown that it is not possible to get more than one palindrome after any number of operations.
+Hence, the answer is 1.
+ +

 

+

Constraints:

+ +
    +
  • 1 <= words.length <= 1000
  • +
  • 1 <= words[i].length <= 100
  • +
  • words[i] consists only of lowercase English letters.
  • +
+
\ No newline at end of file From 115a9bc7a3fb760a087085092b89c2253941c6ac Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 11 Feb 2024 17:38:47 +0530 Subject: [PATCH 0679/1399] Attach NOTES - LeetHub --- 3035-maximum-palindromes-after-operations/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 3035-maximum-palindromes-after-operations/NOTES.md diff --git a/3035-maximum-palindromes-after-operations/NOTES.md b/3035-maximum-palindromes-after-operations/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/3035-maximum-palindromes-after-operations/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From df1d1a36cb08586817e46b505d9aeda795842fcf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 11 Feb 2024 17:38:51 +0530 Subject: [PATCH 0680/1399] Time: 81 ms (25.00%), Space: 28.3 MB (37.50%) - LeetHub --- ...5-maximum-palindromes-after-operations.cpp | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 3035-maximum-palindromes-after-operations/3035-maximum-palindromes-after-operations.cpp diff --git a/3035-maximum-palindromes-after-operations/3035-maximum-palindromes-after-operations.cpp b/3035-maximum-palindromes-after-operations/3035-maximum-palindromes-after-operations.cpp new file mode 100644 index 00000000..9fd03360 --- /dev/null +++ b/3035-maximum-palindromes-after-operations/3035-maximum-palindromes-after-operations.cpp @@ -0,0 +1,64 @@ +class Solution { +public: + + int maxPalindromesAfterOperations(vector& words) { + + int n = words.size(); + + map mp; + + for(auto& word : words) + { + for(auto& ch : word) + ++mp[ch]; + } + + long long ev = 0, odd = 0; + + for(auto& [f,e] : mp) + { + if(e & 1) + { + ev += (e-1); + ++odd; + } + else + ev += e; + } + + int cnt = 0; + + sort(words.begin(), words.end(), [&](const auto& a, const auto& b){ + return a.size() < b.size(); + }); + + for(int i = 0; i < words.size(); ++i) + { + int sz = words[i].size(); + + if(sz & 1) + { + if(odd > 0) + { + --odd; + } + else if(ev >= 2) + { + ++odd; + ev -= 2; + } + --sz; + } + + if(ev >= sz) + { + ev -= sz; + ++cnt; + } + + + } + + return cnt; + } +}; \ No newline at end of file From a5a6fd5e7f1f0cc129c7127f1d19c9d38dd5accc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 11 Feb 2024 17:48:02 +0530 Subject: [PATCH 0681/1399] Attach NOTES - LeetHub From 9bb974b2a28a7d5119e928962c7f201cef3cc1b2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 11 Feb 2024 17:48:06 +0530 Subject: [PATCH 0682/1399] Time: 67 ms (25.00%), Space: 28.8 MB (37.50%) - LeetHub --- ...5-maximum-palindromes-after-operations.cpp | 51 ++++--------------- 1 file changed, 10 insertions(+), 41 deletions(-) diff --git a/3035-maximum-palindromes-after-operations/3035-maximum-palindromes-after-operations.cpp b/3035-maximum-palindromes-after-operations/3035-maximum-palindromes-after-operations.cpp index 9fd03360..da125d45 100644 --- a/3035-maximum-palindromes-after-operations/3035-maximum-palindromes-after-operations.cpp +++ b/3035-maximum-palindromes-after-operations/3035-maximum-palindromes-after-operations.cpp @@ -1,62 +1,31 @@ class Solution { public: - int maxPalindromesAfterOperations(vector& words) { - int n = words.size(); - - map mp; + vector length; + map mp; for(auto& word : words) { for(auto& ch : word) ++mp[ch]; + length.push_back((int)word.size()/2); } - long long ev = 0, odd = 0; - - for(auto& [f,e] : mp) - { - if(e & 1) - { - ev += (e-1); - ++odd; - } - else - ev += e; - } + int matching = 0, cnt = 0; - int cnt = 0; + for(auto&[f, e] : mp) + matching += e/2; - sort(words.begin(), words.end(), [&](const auto& a, const auto& b){ - return a.size() < b.size(); - }); + sort(length.begin(), length.end()); - for(int i = 0; i < words.size(); ++i) + for(int i = 0; i < length.size(); ++i) { - int sz = words[i].size(); - - if(sz & 1) - { - if(odd > 0) - { - --odd; - } - else if(ev >= 2) - { - ++odd; - ev -= 2; - } - --sz; - } - - if(ev >= sz) + if(matching >= length[i]) { - ev -= sz; + matching -= length[i]; ++cnt; } - - } return cnt; From b8298f8fa39ed0fdfce240c67b265ed51c87417b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 12 Feb 2024 00:43:16 +0530 Subject: [PATCH 0683/1399] Attach NOTES - LeetHub --- 1463-cherry-pickup-ii/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1463-cherry-pickup-ii/NOTES.md diff --git a/1463-cherry-pickup-ii/NOTES.md b/1463-cherry-pickup-ii/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1463-cherry-pickup-ii/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 2bbbfc7d729f64a8e7b162c9096763b7332f0dd1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 12 Feb 2024 09:03:47 +0530 Subject: [PATCH 0684/1399] Create README - LeetHub --- 0169-majority-element/README.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 0169-majority-element/README.md diff --git a/0169-majority-element/README.md b/0169-majority-element/README.md new file mode 100644 index 00000000..0159f3e8 --- /dev/null +++ b/0169-majority-element/README.md @@ -0,0 +1,23 @@ +

169. Majority Element

Easy


Given an array nums of size n, return the majority element.

+ +

The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array.

+ +

 

+

Example 1:

+
Input: nums = [3,2,3]
+Output: 3
+

Example 2:

+
Input: nums = [2,2,1,1,1,2,2]
+Output: 2
+
+

 

+

Constraints:

+ +
    +
  • n == nums.length
  • +
  • 1 <= n <= 5 * 104
  • +
  • -109 <= nums[i] <= 109
  • +
+ +

 

+Follow-up: Could you solve the problem in linear time and in O(1) space?
\ No newline at end of file From 5fd05ed6de1a5b806bc81a2f20f4770dc323e8bd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 12 Feb 2024 09:03:47 +0530 Subject: [PATCH 0685/1399] Attach NOTES - LeetHub --- 0169-majority-element/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0169-majority-element/NOTES.md diff --git a/0169-majority-element/NOTES.md b/0169-majority-element/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0169-majority-element/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 794ea06d80a246f3751f7ec37f03f9e3d0de5e1b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 12 Feb 2024 09:03:50 +0530 Subject: [PATCH 0686/1399] Time: 7 ms (97.54%), Space: 22 MB (33.81%) - LeetHub --- .../0169-majority-element.cpp | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 0169-majority-element/0169-majority-element.cpp diff --git a/0169-majority-element/0169-majority-element.cpp b/0169-majority-element/0169-majority-element.cpp new file mode 100644 index 00000000..9b85e7db --- /dev/null +++ b/0169-majority-element/0169-majority-element.cpp @@ -0,0 +1,22 @@ +class Solution { +public: + int majorityElement(vector& nums) { + + int cnt = 0, num = -1; + + for(auto& ele : nums) + { + if(cnt == 0) + { + num = ele; + cnt = 1; + } + else if(ele == num) + ++cnt; + else + --cnt; + } + + return num; + } +}; \ No newline at end of file From 11eeb540b2c75f222460f347b65235e6580f5928 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 13 Feb 2024 08:47:43 +0530 Subject: [PATCH 0687/1399] Create README - LeetHub --- .../README.md | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 2108-find-first-palindromic-string-in-the-array/README.md diff --git a/2108-find-first-palindromic-string-in-the-array/README.md b/2108-find-first-palindromic-string-in-the-array/README.md new file mode 100644 index 00000000..cdaba2f0 --- /dev/null +++ b/2108-find-first-palindromic-string-in-the-array/README.md @@ -0,0 +1,36 @@ +

2108. Find First Palindromic String in the Array

Easy


Given an array of strings words, return the first palindromic string in the array. If there is no such string, return an empty string "".

+ +

A string is palindromic if it reads the same forward and backward.

+ +

 

+

Example 1:

+ +
Input: words = ["abc","car","ada","racecar","cool"]
+Output: "ada"
+Explanation: The first string that is palindromic is "ada".
+Note that "racecar" is also palindromic, but it is not the first.
+
+ +

Example 2:

+ +
Input: words = ["notapalindrome","racecar"]
+Output: "racecar"
+Explanation: The first and only string that is palindromic is "racecar".
+
+ +

Example 3:

+ +
Input: words = ["def","ghi"]
+Output: ""
+Explanation: There are no palindromic strings, so the empty string is returned.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= words.length <= 100
  • +
  • 1 <= words[i].length <= 100
  • +
  • words[i] consists only of lowercase English letters.
  • +
+
\ No newline at end of file From dffbc0c2436549a5392d4d5f57205b41764e3e1a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 13 Feb 2024 08:47:46 +0530 Subject: [PATCH 0688/1399] Time: 42 ms (87.48%), Space: 23.3 MB (61.43%) - LeetHub --- ...-first-palindromic-string-in-the-array.cpp | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 2108-find-first-palindromic-string-in-the-array/2108-find-first-palindromic-string-in-the-array.cpp diff --git a/2108-find-first-palindromic-string-in-the-array/2108-find-first-palindromic-string-in-the-array.cpp b/2108-find-first-palindromic-string-in-the-array/2108-find-first-palindromic-string-in-the-array.cpp new file mode 100644 index 00000000..51c0d7d2 --- /dev/null +++ b/2108-find-first-palindromic-string-in-the-array/2108-find-first-palindromic-string-in-the-array.cpp @@ -0,0 +1,25 @@ +class Solution { +public: + string firstPalindrome(vector& words) { + + for(auto& word : words) + { + int i = 0, j = word.size()-1; + bool ok = true; + + while(i < j) + { + if(word[i] != word[j]) + { + ok = false; + break; + } + ++i, --j; + } + + if(ok) return word; + } + + return ""; + } +}; \ No newline at end of file From 691a31a51647080a4402a0c2177bd4f6ecf41bd0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 14 Feb 2024 09:23:18 +0530 Subject: [PATCH 0689/1399] Create README - LeetHub --- .../README.md | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 2149-rearrange-array-elements-by-sign/README.md diff --git a/2149-rearrange-array-elements-by-sign/README.md b/2149-rearrange-array-elements-by-sign/README.md new file mode 100644 index 00000000..06869c95 --- /dev/null +++ b/2149-rearrange-array-elements-by-sign/README.md @@ -0,0 +1,42 @@ +

2149. Rearrange Array Elements by Sign

Medium


You are given a 0-indexed integer array nums of even length consisting of an equal number of positive and negative integers.

+ +

You should rearrange the elements of nums such that the modified array follows the given conditions:

+ +
    +
  1. Every consecutive pair of integers have opposite signs.
  2. +
  3. For all integers with the same sign, the order in which they were present in nums is preserved.
  4. +
  5. The rearranged array begins with a positive integer.
  6. +
+ +

Return the modified array after rearranging the elements to satisfy the aforementioned conditions.

+ +

 

+

Example 1:

+ +
Input: nums = [3,1,-2,-5,2,-4]
+Output: [3,-2,1,-5,2,-4]
+Explanation:
+The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4].
+The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4].
+Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions.  
+
+ +

Example 2:

+ +
Input: nums = [-1,1]
+Output: [1,-1]
+Explanation:
+1 is the only positive integer and -1 the only negative integer in nums.
+So nums is rearranged to [1,-1].
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= nums.length <= 2 * 105
  • +
  • nums.length is even
  • +
  • 1 <= |nums[i]| <= 105
  • +
  • nums consists of equal number of positive and negative integers.
  • +
+
\ No newline at end of file From ecda9e7a432e727bab012b7b1e6e973aef257308 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 14 Feb 2024 09:23:19 +0530 Subject: [PATCH 0690/1399] Attach NOTES - LeetHub --- 2149-rearrange-array-elements-by-sign/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2149-rearrange-array-elements-by-sign/NOTES.md diff --git a/2149-rearrange-array-elements-by-sign/NOTES.md b/2149-rearrange-array-elements-by-sign/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2149-rearrange-array-elements-by-sign/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From b9fff462cd5c16a1d93ca226ecb7d5cf24f7b35f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 14 Feb 2024 09:23:22 +0530 Subject: [PATCH 0691/1399] Time: 131 ms (94.75%), Space: 137.1 MB (18.51%) - LeetHub --- .../2149-rearrange-array-elements-by-sign.cpp | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 2149-rearrange-array-elements-by-sign/2149-rearrange-array-elements-by-sign.cpp diff --git a/2149-rearrange-array-elements-by-sign/2149-rearrange-array-elements-by-sign.cpp b/2149-rearrange-array-elements-by-sign/2149-rearrange-array-elements-by-sign.cpp new file mode 100644 index 00000000..0e568641 --- /dev/null +++ b/2149-rearrange-array-elements-by-sign/2149-rearrange-array-elements-by-sign.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + vector rearrangeArray(vector& nums) { + + vector pos, neg; + + for(auto& ele : nums) + { + if(ele < 0) + neg.push_back(ele); + else + pos.push_back(ele); + } + + vector ans(nums.size()); + + int k = 0, i = 0, j = 0; + + while(i < pos.size() and j < pos.size()) + { + ans[k++] = pos[i++]; + ans[k++] = neg[j++]; + } + + return ans; + } +}; \ No newline at end of file From d54273d19c14488765b1bb926db6eaaba8afbf92 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 14 Feb 2024 09:26:34 +0530 Subject: [PATCH 0692/1399] Attach NOTES - LeetHub From 62937670570d81233b34ae66c15a47cfe4c28352 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 14 Feb 2024 09:26:39 +0530 Subject: [PATCH 0693/1399] Time: 148 ms (71.51%), Space: 126.5 MB (41.45%) - LeetHub --- .../2149-rearrange-array-elements-by-sign.cpp | 29 +++++++++---------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/2149-rearrange-array-elements-by-sign/2149-rearrange-array-elements-by-sign.cpp b/2149-rearrange-array-elements-by-sign/2149-rearrange-array-elements-by-sign.cpp index 0e568641..156fbe4d 100644 --- a/2149-rearrange-array-elements-by-sign/2149-rearrange-array-elements-by-sign.cpp +++ b/2149-rearrange-array-elements-by-sign/2149-rearrange-array-elements-by-sign.cpp @@ -2,24 +2,23 @@ class Solution { public: vector rearrangeArray(vector& nums) { - vector pos, neg; + int n = nums.size(); + vector ans(n); - for(auto& ele : nums) - { - if(ele < 0) - neg.push_back(ele); - else - pos.push_back(ele); - } + int even = 0, odd = 1; - vector ans(nums.size()); - - int k = 0, i = 0, j = 0; - - while(i < pos.size() and j < pos.size()) + for(int i = 0; i < n; ++i) { - ans[k++] = pos[i++]; - ans[k++] = neg[j++]; + if(nums[i] < 0) + { + ans[odd] = nums[i]; + odd += 2; + } + else + { + ans[even] = nums[i]; + even += 2; + } } return ans; From 4ff72d74ea02aba49cdb9914332125aa6550f06f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 15 Feb 2024 08:51:10 +0530 Subject: [PATCH 0694/1399] Create README - LeetHub --- .../README.md | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 2971-find-polygon-with-the-largest-perimeter/README.md diff --git a/2971-find-polygon-with-the-largest-perimeter/README.md b/2971-find-polygon-with-the-largest-perimeter/README.md new file mode 100644 index 00000000..c7d742d1 --- /dev/null +++ b/2971-find-polygon-with-the-largest-perimeter/README.md @@ -0,0 +1,42 @@ +

2971. Find Polygon With the Largest Perimeter

Medium


You are given an array of positive integers nums of length n.

+ +

A polygon is a closed plane figure that has at least 3 sides. The longest side of a polygon is smaller than the sum of its other sides.

+ +

Conversely, if you have k (k >= 3) positive real numbers a1, a2, a3, ..., ak where a1 <= a2 <= a3 <= ... <= ak and a1 + a2 + a3 + ... + ak-1 > ak, then there always exists a polygon with k sides whose lengths are a1, a2, a3, ..., ak.

+ +

The perimeter of a polygon is the sum of lengths of its sides.

+ +

Return the largest possible perimeter of a polygon whose sides can be formed from nums, or -1 if it is not possible to create a polygon.

+ +

 

+

Example 1:

+ +
Input: nums = [5,5,5]
+Output: 15
+Explanation: The only possible polygon that can be made from nums has 3 sides: 5, 5, and 5. The perimeter is 5 + 5 + 5 = 15.
+
+ +

Example 2:

+ +
Input: nums = [1,12,1,2,5,50,3]
+Output: 12
+Explanation: The polygon with the largest perimeter which can be made from nums has 5 sides: 1, 1, 2, 3, and 5. The perimeter is 1 + 1 + 2 + 3 + 5 = 12.
+We cannot have a polygon with either 12 or 50 as the longest side because it is not possible to include 2 or more smaller sides that have a greater sum than either of them.
+It can be shown that the largest possible perimeter is 12.
+
+ +

Example 3:

+ +
Input: nums = [5,5,50]
+Output: -1
+Explanation: There is no possible way to form a polygon from nums, as a polygon has at least 3 sides and 50 > 5 + 5.
+
+ +

 

+

Constraints:

+ +
    +
  • 3 <= n <= 105
  • +
  • 1 <= nums[i] <= 109
  • +
+
\ No newline at end of file From 77af2e2003379757819a0c62efe3490c60f5c3a3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 15 Feb 2024 08:51:10 +0530 Subject: [PATCH 0695/1399] Attach NOTES - LeetHub --- 2971-find-polygon-with-the-largest-perimeter/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2971-find-polygon-with-the-largest-perimeter/NOTES.md diff --git a/2971-find-polygon-with-the-largest-perimeter/NOTES.md b/2971-find-polygon-with-the-largest-perimeter/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2971-find-polygon-with-the-largest-perimeter/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 8446f70b953239be93117194b9c93a21bc7bd577 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 15 Feb 2024 08:51:14 +0530 Subject: [PATCH 0696/1399] Time: 153 ms (27.15%), Space: 88.5 MB (7.40%) - LeetHub --- ...ind-polygon-with-the-largest-perimeter.cpp | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 2971-find-polygon-with-the-largest-perimeter/2971-find-polygon-with-the-largest-perimeter.cpp diff --git a/2971-find-polygon-with-the-largest-perimeter/2971-find-polygon-with-the-largest-perimeter.cpp b/2971-find-polygon-with-the-largest-perimeter/2971-find-polygon-with-the-largest-perimeter.cpp new file mode 100644 index 00000000..7acb465d --- /dev/null +++ b/2971-find-polygon-with-the-largest-perimeter/2971-find-polygon-with-the-largest-perimeter.cpp @@ -0,0 +1,25 @@ +class Solution { +public: + long long largestPerimeter(vector& nums) { + + int n = nums.size(); + + sort(nums.begin(), nums.end()); + + vector pref(n, 0); + + pref[0] = nums[0]; + + for(int i = 1; i < n; ++i) + pref[i] = (pref[i-1] + nums[i]); + + for(int i = n-2; i >= 1; --i) + { + if(pref[i] > (pref[i+1] - pref[i])) + return pref[i+1]; + } + + return -1; + + } +}; \ No newline at end of file From ebe191940691ea32d1ee085b0789e3258b7160a1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 16 Feb 2024 08:13:55 +0530 Subject: [PATCH 0697/1399] Create README - LeetHub --- .../README.md | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 1481-least-number-of-unique-integers-after-k-removals/README.md diff --git a/1481-least-number-of-unique-integers-after-k-removals/README.md b/1481-least-number-of-unique-integers-after-k-removals/README.md new file mode 100644 index 00000000..a8ace42d --- /dev/null +++ b/1481-least-number-of-unique-integers-after-k-removals/README.md @@ -0,0 +1,26 @@ +

1481. Least Number of Unique Integers after K Removals

Medium


Given an array of integers arr and an integer k. Find the least number of unique integers after removing exactly k elements.

+ +
    +
+ +

 

+

Example 1:

+ +
Input: arr = [5,5,4], k = 1
+Output: 1
+Explanation: Remove the single 4, only 5 is left.
+
+Example 2: + +
Input: arr = [4,3,1,1,3,3,2], k = 3
+Output: 2
+Explanation: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.
+ +

 

+

Constraints:

+ +
    +
  • 1 <= arr.length <= 10^5
  • +
  • 1 <= arr[i] <= 10^9
  • +
  • 0 <= k <= arr.length
  • +
\ No newline at end of file From dad74660b192e698105ed18fbe1e99c19cd8e4c8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 16 Feb 2024 08:13:59 +0530 Subject: [PATCH 0698/1399] Time: 187 ms (39.40%), Space: 70.1 MB (24.10%) - LeetHub --- ...er-of-unique-integers-after-k-removals.cpp | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 1481-least-number-of-unique-integers-after-k-removals/1481-least-number-of-unique-integers-after-k-removals.cpp diff --git a/1481-least-number-of-unique-integers-after-k-removals/1481-least-number-of-unique-integers-after-k-removals.cpp b/1481-least-number-of-unique-integers-after-k-removals/1481-least-number-of-unique-integers-after-k-removals.cpp new file mode 100644 index 00000000..c378674a --- /dev/null +++ b/1481-least-number-of-unique-integers-after-k-removals/1481-least-number-of-unique-integers-after-k-removals.cpp @@ -0,0 +1,33 @@ +class Solution { +public: + int findLeastNumOfUniqueInts(vector& arr, int k) { + + map mp; + + for(auto& ele : arr) + ++mp[ele]; + + priority_queue, vector>, greater>> pq; + + for(auto&[f, e] : mp) + pq.push({e, f}); + + while(!pq.empty()) + { + auto curr = pq.top(); + pq.pop(); + + if(curr.first <= k) + { + k -= curr.first; + } + else + { + pq.push(curr); + break; + } + } + + return (int)pq.size(); + } +}; \ No newline at end of file From aa28815fad28d904d1054757e946650632305e7a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 17 Feb 2024 23:37:23 +0530 Subject: [PATCH 0699/1399] Create README - LeetHub --- .../README.md | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 1642-furthest-building-you-can-reach/README.md diff --git a/1642-furthest-building-you-can-reach/README.md b/1642-furthest-building-you-can-reach/README.md new file mode 100644 index 00000000..57d2a1eb --- /dev/null +++ b/1642-furthest-building-you-can-reach/README.md @@ -0,0 +1,48 @@ +

1642. Furthest Building You Can Reach

Medium


You are given an integer array heights representing the heights of buildings, some bricks, and some ladders.

+ +

You start your journey from building 0 and move to the next building by possibly using bricks or ladders.

+ +

While moving from building i to building i+1 (0-indexed),

+ +
    +
  • If the current building's height is greater than or equal to the next building's height, you do not need a ladder or bricks.
  • +
  • If the current building's height is less than the next building's height, you can either use one ladder or (h[i+1] - h[i]) bricks.
  • +
+ +

Return the furthest building index (0-indexed) you can reach if you use the given ladders and bricks optimally.

+ +

 

+

Example 1:

+ +
Input: heights = [4,2,7,6,9,14,12], bricks = 5, ladders = 1
+Output: 4
+Explanation: Starting at building 0, you can follow these steps:
+- Go to building 1 without using ladders nor bricks since 4 >= 2.
+- Go to building 2 using 5 bricks. You must use either bricks or ladders because 2 < 7.
+- Go to building 3 without using ladders nor bricks since 7 >= 6.
+- Go to building 4 using your only ladder. You must use either bricks or ladders because 6 < 9.
+It is impossible to go beyond building 4 because you do not have any more bricks or ladders.
+
+ +

Example 2:

+ +
Input: heights = [4,12,2,7,3,18,20,3,19], bricks = 10, ladders = 2
+Output: 7
+
+ +

Example 3:

+ +
Input: heights = [14,3,19,3], bricks = 17, ladders = 0
+Output: 3
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= heights.length <= 105
  • +
  • 1 <= heights[i] <= 106
  • +
  • 0 <= bricks <= 109
  • +
  • 0 <= ladders <= heights.length
  • +
+
\ No newline at end of file From da2827bce2220fc29a858407d160bfab9fbfb6da Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 17 Feb 2024 23:37:26 +0530 Subject: [PATCH 0700/1399] Time: 82 ms (45.58%), Space: 57.5 MB (30.10%) - LeetHub --- .../1642-furthest-building-you-can-reach.cpp | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 1642-furthest-building-you-can-reach/1642-furthest-building-you-can-reach.cpp diff --git a/1642-furthest-building-you-can-reach/1642-furthest-building-you-can-reach.cpp b/1642-furthest-building-you-can-reach/1642-furthest-building-you-can-reach.cpp new file mode 100644 index 00000000..d94b970b --- /dev/null +++ b/1642-furthest-building-you-can-reach/1642-furthest-building-you-can-reach.cpp @@ -0,0 +1,28 @@ +class Solution { +public: + int furthestBuilding(vector& heights, int bricks, int ladders) { + priority_queue pq; + + int i = 0; + for(i = 0; i Date: Sun, 18 Feb 2024 11:14:23 +0530 Subject: [PATCH 0701/1399] Create README - LeetHub --- 0354-russian-doll-envelopes/README.md | 31 +++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 0354-russian-doll-envelopes/README.md diff --git a/0354-russian-doll-envelopes/README.md b/0354-russian-doll-envelopes/README.md new file mode 100644 index 00000000..561fbe15 --- /dev/null +++ b/0354-russian-doll-envelopes/README.md @@ -0,0 +1,31 @@ +

354. Russian Doll Envelopes

Hard


You are given a 2D array of integers envelopes where envelopes[i] = [wi, hi] represents the width and the height of an envelope.

+ +

One envelope can fit into another if and only if both the width and height of one envelope are greater than the other envelope's width and height.

+ +

Return the maximum number of envelopes you can Russian doll (i.e., put one inside the other).

+ +

Note: You cannot rotate an envelope.

+ +

 

+

Example 1:

+ +
Input: envelopes = [[5,4],[6,4],[6,7],[2,3]]
+Output: 3
+Explanation: The maximum number of envelopes you can Russian doll is 3 ([2,3] => [5,4] => [6,7]).
+
+ +

Example 2:

+ +
Input: envelopes = [[1,1],[1,1],[1,1]]
+Output: 1
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= envelopes.length <= 105
  • +
  • envelopes[i].length == 2
  • +
  • 1 <= wi, hi <= 105
  • +
+
\ No newline at end of file From 1bd05fbd787909acf5d2a8c95ac190352f93abb9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 18 Feb 2024 11:14:24 +0530 Subject: [PATCH 0702/1399] Attach NOTES - LeetHub --- 0354-russian-doll-envelopes/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0354-russian-doll-envelopes/NOTES.md diff --git a/0354-russian-doll-envelopes/NOTES.md b/0354-russian-doll-envelopes/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0354-russian-doll-envelopes/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 8a54af9d20c459679fe525d67d37d7b67557ab6f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 19 Feb 2024 01:25:38 +0530 Subject: [PATCH 0703/1399] Create README - LeetHub --- 2402-meeting-rooms-iii/README.md | 57 ++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 2402-meeting-rooms-iii/README.md diff --git a/2402-meeting-rooms-iii/README.md b/2402-meeting-rooms-iii/README.md new file mode 100644 index 00000000..fffc9ee8 --- /dev/null +++ b/2402-meeting-rooms-iii/README.md @@ -0,0 +1,57 @@ +

2402. Meeting Rooms III

Hard


You are given an integer n. There are n rooms numbered from 0 to n - 1.

+ +

You are given a 2D integer array meetings where meetings[i] = [starti, endi] means that a meeting will be held during the half-closed time interval [starti, endi). All the values of starti are unique.

+ +

Meetings are allocated to rooms in the following manner:

+ +
    +
  1. Each meeting will take place in the unused room with the lowest number.
  2. +
  3. If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the same duration as the original meeting.
  4. +
  5. When a room becomes unused, meetings that have an earlier original start time should be given the room.
  6. +
+ +

Return the number of the room that held the most meetings. If there are multiple rooms, return the room with the lowest number.

+ +

A half-closed interval [a, b) is the interval between a and b including a and not including b.

+ +

 

+

Example 1:

+ +
Input: n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
+Output: 0
+Explanation:
+- At time 0, both rooms are not being used. The first meeting starts in room 0.
+- At time 1, only room 1 is not being used. The second meeting starts in room 1.
+- At time 2, both rooms are being used. The third meeting is delayed.
+- At time 3, both rooms are being used. The fourth meeting is delayed.
+- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
+- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
+Both rooms 0 and 1 held 2 meetings, so we return 0. 
+
+ +

Example 2:

+ +
Input: n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
+Output: 1
+Explanation:
+- At time 1, all three rooms are not being used. The first meeting starts in room 0.
+- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
+- At time 3, only room 2 is not being used. The third meeting starts in room 2.
+- At time 4, all three rooms are being used. The fourth meeting is delayed.
+- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
+- At time 6, all three rooms are being used. The fifth meeting is delayed.
+- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
+Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1. 
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= 100
  • +
  • 1 <= meetings.length <= 105
  • +
  • meetings[i].length == 2
  • +
  • 0 <= starti < endi <= 5 * 105
  • +
  • All the values of starti are unique.
  • +
+
\ No newline at end of file From 2a5bcbe7a10ccd5f3c69b76a9be2499e63b2d755 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 19 Feb 2024 01:25:38 +0530 Subject: [PATCH 0704/1399] Attach NOTES - LeetHub --- 2402-meeting-rooms-iii/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2402-meeting-rooms-iii/NOTES.md diff --git a/2402-meeting-rooms-iii/NOTES.md b/2402-meeting-rooms-iii/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2402-meeting-rooms-iii/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From b3b52795125a30165f05d84c9dbf23f651592a1c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 19 Feb 2024 01:25:42 +0530 Subject: [PATCH 0705/1399] Time: 302 ms (87.90%), Space: 100.5 MB (60.04%) - LeetHub --- .../2402-meeting-rooms-iii.cpp | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 2402-meeting-rooms-iii/2402-meeting-rooms-iii.cpp diff --git a/2402-meeting-rooms-iii/2402-meeting-rooms-iii.cpp b/2402-meeting-rooms-iii/2402-meeting-rooms-iii.cpp new file mode 100644 index 00000000..bc237167 --- /dev/null +++ b/2402-meeting-rooms-iii/2402-meeting-rooms-iii.cpp @@ -0,0 +1,66 @@ + +class Solution { +public: + int mostBooked(int n, vector>& meetings) { + + int m = meetings.size(); + + int roomOccupied = 0; + + sort(meetings.begin(), meetings.end()); + + vector roomCount(n, 0); + + priority_queue, vector>, greater>> meetingRooms; // EndTime, RoomNumber + + priority_queue, greater> availableRooms; + + for(int room = 0; room < n; ++room) + availableRooms.push(room); + + for(auto& meet : meetings) + { + long long start = meet[0]; + long long end = meet[1]; + long long duration = end - start; + + while(!meetingRooms.empty() and meetingRooms.top().first <= start) + { + int room = meetingRooms.top().second; + availableRooms.push(room); + meetingRooms.pop(); + } + + if(!availableRooms.empty()) + { + int room = availableRooms.top(); + availableRooms.pop(); + meetingRooms.push({end, room}); + ++roomCount[room]; + } + else + { + int room = meetingRooms.top().second; + long long endTime = meetingRooms.top().first; + meetingRooms.pop(); + endTime += duration; + meetingRooms.push({endTime, room}); + ++roomCount[room]; + } + } + + int resultRoom = -1; + int maxUse = 0; + + for(int i = 0; i < n; ++i) + { + if(roomCount[i] > maxUse) + { + resultRoom = i; + maxUse = roomCount[i]; + } + } + + return resultRoom; + } +}; \ No newline at end of file From b651b6855e5ac1275bdf5676dc45d214edef3036 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 19 Feb 2024 08:21:30 +0530 Subject: [PATCH 0706/1399] Create README - LeetHub --- 0231-power-of-two/README.md | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 0231-power-of-two/README.md diff --git a/0231-power-of-two/README.md b/0231-power-of-two/README.md new file mode 100644 index 00000000..b6b0cad9 --- /dev/null +++ b/0231-power-of-two/README.md @@ -0,0 +1,34 @@ +

231. Power of Two

Easy


Given an integer n, return true if it is a power of two. Otherwise, return false.

+ +

An integer n is a power of two, if there exists an integer x such that n == 2x.

+ +

 

+

Example 1:

+ +
Input: n = 1
+Output: true
+Explanation: 20 = 1
+
+ +

Example 2:

+ +
Input: n = 16
+Output: true
+Explanation: 24 = 16
+
+ +

Example 3:

+ +
Input: n = 3
+Output: false
+
+ +

 

+

Constraints:

+ +
    +
  • -231 <= n <= 231 - 1
  • +
+ +

 

+Follow up: Could you solve it without loops/recursion?
\ No newline at end of file From d0af413e6f212d2aa8e7229936286737c2ae6ed3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 19 Feb 2024 08:21:30 +0530 Subject: [PATCH 0707/1399] Attach NOTES - LeetHub --- 0231-power-of-two/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0231-power-of-two/NOTES.md diff --git a/0231-power-of-two/NOTES.md b/0231-power-of-two/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0231-power-of-two/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From eeaf9a7f57276de1e79380ff34aafa2e78bab4a5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 19 Feb 2024 08:21:34 +0530 Subject: [PATCH 0708/1399] Time: 2 ms (39.05%), Space: 7.3 MB (19.05%) - LeetHub --- 0231-power-of-two/0231-power-of-two.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 0231-power-of-two/0231-power-of-two.cpp diff --git a/0231-power-of-two/0231-power-of-two.cpp b/0231-power-of-two/0231-power-of-two.cpp new file mode 100644 index 00000000..183a7700 --- /dev/null +++ b/0231-power-of-two/0231-power-of-two.cpp @@ -0,0 +1,10 @@ +class Solution { +public: + bool isPowerOfTwo(int n) { + if(n == 1) + return true; + if(n & 1 or n == 0) + return false; + return isPowerOfTwo(n/2); + } +}; \ No newline at end of file From 0ca9ad3417ba44d91bf096f80e440a99f107afb4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 20 Feb 2024 08:41:12 +0530 Subject: [PATCH 0709/1399] Create README - LeetHub --- 0268-missing-number/README.md | 37 +++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 0268-missing-number/README.md diff --git a/0268-missing-number/README.md b/0268-missing-number/README.md new file mode 100644 index 00000000..f88e56a1 --- /dev/null +++ b/0268-missing-number/README.md @@ -0,0 +1,37 @@ +

268. Missing Number

Easy


Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array.

+ +

 

+

Example 1:

+ +
Input: nums = [3,0,1]
+Output: 2
+Explanation: n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing number in the range since it does not appear in nums.
+
+ +

Example 2:

+ +
Input: nums = [0,1]
+Output: 2
+Explanation: n = 2 since there are 2 numbers, so all numbers are in the range [0,2]. 2 is the missing number in the range since it does not appear in nums.
+
+ +

Example 3:

+ +
Input: nums = [9,6,4,2,3,5,7,0,1]
+Output: 8
+Explanation: n = 9 since there are 9 numbers, so all numbers are in the range [0,9]. 8 is the missing number in the range since it does not appear in nums.
+
+ +

 

+

Constraints:

+ +
    +
  • n == nums.length
  • +
  • 1 <= n <= 104
  • +
  • 0 <= nums[i] <= n
  • +
  • All the numbers of nums are unique.
  • +
+ +

 

+

Follow up: Could you implement a solution using only O(1) extra space complexity and O(n) runtime complexity?

+
\ No newline at end of file From 2bcdd4f236a769c7e3912b2110e0dd2ba99a3c33 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 20 Feb 2024 08:41:16 +0530 Subject: [PATCH 0710/1399] Time: 15 ms (58.96%), Space: 20.4 MB (18.88%) - LeetHub --- 0268-missing-number/0268-missing-number.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 0268-missing-number/0268-missing-number.cpp diff --git a/0268-missing-number/0268-missing-number.cpp b/0268-missing-number/0268-missing-number.cpp new file mode 100644 index 00000000..dacbf752 --- /dev/null +++ b/0268-missing-number/0268-missing-number.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + int missingNumber(vector& nums) { + + int n = nums.size(); + + int sum = 0; + + for(auto& ele : nums) + sum += ele; + + int totSum = (n * (n+1)) / 2; + + return totSum - sum; + + } +}; \ No newline at end of file From ffb16fcd1a44db421cb7e3d036aeeee6b474dbd4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 20 Feb 2024 08:42:28 +0530 Subject: [PATCH 0711/1399] Attach NOTES - LeetHub --- 0268-missing-number/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0268-missing-number/NOTES.md diff --git a/0268-missing-number/NOTES.md b/0268-missing-number/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0268-missing-number/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 62876500aef7b043cb30df8021d896fcecbda9db Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 20 Feb 2024 08:42:31 +0530 Subject: [PATCH 0712/1399] Time: 15 ms (58.96%), Space: 20.4 MB (18.88%) - LeetHub From e8bb9132be823f18919053e2a30700dbf02e4b65 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 21 Feb 2024 22:55:39 +0530 Subject: [PATCH 0713/1399] Create README - LeetHub --- 0201-bitwise-and-of-numbers-range/README.md | 28 +++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 0201-bitwise-and-of-numbers-range/README.md diff --git a/0201-bitwise-and-of-numbers-range/README.md b/0201-bitwise-and-of-numbers-range/README.md new file mode 100644 index 00000000..5a34dffc --- /dev/null +++ b/0201-bitwise-and-of-numbers-range/README.md @@ -0,0 +1,28 @@ +

201. Bitwise AND of Numbers Range

Medium


Given two integers left and right that represent the range [left, right], return the bitwise AND of all numbers in this range, inclusive.

+ +

 

+

Example 1:

+ +
Input: left = 5, right = 7
+Output: 4
+
+ +

Example 2:

+ +
Input: left = 0, right = 0
+Output: 0
+
+ +

Example 3:

+ +
Input: left = 1, right = 2147483647
+Output: 0
+
+ +

 

+

Constraints:

+ +
    +
  • 0 <= left <= right <= 231 - 1
  • +
+
\ No newline at end of file From d369b1e927088fb808dae3eb7060db10bde697b9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 21 Feb 2024 22:55:39 +0530 Subject: [PATCH 0714/1399] Attach NOTES - LeetHub --- 0201-bitwise-and-of-numbers-range/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0201-bitwise-and-of-numbers-range/NOTES.md diff --git a/0201-bitwise-and-of-numbers-range/NOTES.md b/0201-bitwise-and-of-numbers-range/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0201-bitwise-and-of-numbers-range/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 1ca1b9516f347fa9ef0c71f10d64ace3b827ac9f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 21 Feb 2024 22:55:43 +0530 Subject: [PATCH 0715/1399] Time: 7 ms (64.93%), Space: 8.8 MB (46.79%) - LeetHub --- .../0201-bitwise-and-of-numbers-range.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 0201-bitwise-and-of-numbers-range/0201-bitwise-and-of-numbers-range.cpp diff --git a/0201-bitwise-and-of-numbers-range/0201-bitwise-and-of-numbers-range.cpp b/0201-bitwise-and-of-numbers-range/0201-bitwise-and-of-numbers-range.cpp new file mode 100644 index 00000000..b92b413c --- /dev/null +++ b/0201-bitwise-and-of-numbers-range/0201-bitwise-and-of-numbers-range.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + int rangeBitwiseAnd(int left, int right) { + + int rightShift = 0; + + while(right != left) + { + left >>= 1; + right >>= 1; + ++rightShift; + } + + return right << rightShift; + + } +}; \ No newline at end of file From 350821ec7d5269fc2595737810aed7821f68c495 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 22 Feb 2024 09:05:50 +0530 Subject: [PATCH 0716/1399] Create README - LeetHub --- 0022-generate-parentheses/README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 0022-generate-parentheses/README.md diff --git a/0022-generate-parentheses/README.md b/0022-generate-parentheses/README.md new file mode 100644 index 00000000..154f576c --- /dev/null +++ b/0022-generate-parentheses/README.md @@ -0,0 +1,17 @@ +

22. Generate Parentheses

Medium


Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.

+ +

 

+

Example 1:

+
Input: n = 3
+Output: ["((()))","(()())","(())()","()(())","()()()"]
+

Example 2:

+
Input: n = 1
+Output: ["()"]
+
+

 

+

Constraints:

+ +
    +
  • 1 <= n <= 8
  • +
+
\ No newline at end of file From 2aa23b1f005ad392f900b4f2c8b5f6c2f39ed99f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 22 Feb 2024 09:05:54 +0530 Subject: [PATCH 0717/1399] Time: 4 ms (49.30%), Space: 15 MB (65.11%) - LeetHub --- .../0022-generate-parentheses.cpp | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 0022-generate-parentheses/0022-generate-parentheses.cpp diff --git a/0022-generate-parentheses/0022-generate-parentheses.cpp b/0022-generate-parentheses/0022-generate-parentheses.cpp new file mode 100644 index 00000000..3d3bd40c --- /dev/null +++ b/0022-generate-parentheses/0022-generate-parentheses.cpp @@ -0,0 +1,26 @@ +class Solution { +public: + + void generate(int openBracket, int closeBracket, int n, string ds, vector& answer) + { + if(openBracket == n and closeBracket == n) + { + answer.push_back(ds); + return; + } + + if(openBracket < n) + generate(openBracket+1, closeBracket, n, ds + '(', answer); + if(closeBracket < openBracket) + generate(openBracket, closeBracket+1, n, ds + ')', answer); + } + + vector generateParenthesis(int n) { + + vector answer; + + generate(0, 0, n, "", answer); + + return answer; + } +}; \ No newline at end of file From b21d56f7d6ceb03b9728664e1edaa0f48c143f00 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 23 Feb 2024 23:19:25 +0530 Subject: [PATCH 0718/1399] Attach NOTES - LeetHub --- 0787-cheapest-flights-within-k-stops/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0787-cheapest-flights-within-k-stops/NOTES.md diff --git a/0787-cheapest-flights-within-k-stops/NOTES.md b/0787-cheapest-flights-within-k-stops/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0787-cheapest-flights-within-k-stops/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 54a01a211e6bc1983154011ef74a31bf98aead0c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 24 Feb 2024 23:14:06 +0530 Subject: [PATCH 0719/1399] Create README - LeetHub --- 2092-find-all-people-with-secret/README.md | 57 ++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 2092-find-all-people-with-secret/README.md diff --git a/2092-find-all-people-with-secret/README.md b/2092-find-all-people-with-secret/README.md new file mode 100644 index 00000000..3a4f6ecb --- /dev/null +++ b/2092-find-all-people-with-secret/README.md @@ -0,0 +1,57 @@ +

2092. Find All People With Secret

Hard


You are given an integer n indicating there are n people numbered from 0 to n - 1. You are also given a 0-indexed 2D integer array meetings where meetings[i] = [xi, yi, timei] indicates that person xi and person yi have a meeting at timei. A person may attend multiple meetings at the same time. Finally, you are given an integer firstPerson.

+ +

Person 0 has a secret and initially shares the secret with a person firstPerson at time 0. This secret is then shared every time a meeting takes place with a person that has the secret. More formally, for every meeting, if a person xi has the secret at timei, then they will share the secret with person yi, and vice versa.

+ +

The secrets are shared instantaneously. That is, a person may receive the secret and share it with people in other meetings within the same time frame.

+ +

Return a list of all the people that have the secret after all the meetings have taken place. You may return the answer in any order.

+ +

 

+

Example 1:

+ +
Input: n = 6, meetings = [[1,2,5],[2,3,8],[1,5,10]], firstPerson = 1
+Output: [0,1,2,3,5]
+Explanation:
+At time 0, person 0 shares the secret with person 1.
+At time 5, person 1 shares the secret with person 2.
+At time 8, person 2 shares the secret with person 3.
+At time 10, person 1 shares the secret with person 5.​​​​
+Thus, people 0, 1, 2, 3, and 5 know the secret after all the meetings.
+
+ +

Example 2:

+ +
Input: n = 4, meetings = [[3,1,3],[1,2,2],[0,3,3]], firstPerson = 3
+Output: [0,1,3]
+Explanation:
+At time 0, person 0 shares the secret with person 3.
+At time 2, neither person 1 nor person 2 know the secret.
+At time 3, person 3 shares the secret with person 0 and person 1.
+Thus, people 0, 1, and 3 know the secret after all the meetings.
+
+ +

Example 3:

+ +
Input: n = 5, meetings = [[3,4,2],[1,2,1],[2,3,1]], firstPerson = 1
+Output: [0,1,2,3,4]
+Explanation:
+At time 0, person 0 shares the secret with person 1.
+At time 1, person 1 shares the secret with person 2, and person 2 shares the secret with person 3.
+Note that person 2 can share the secret at the same time as receiving it.
+At time 2, person 3 shares the secret with person 4.
+Thus, people 0, 1, 2, 3, and 4 know the secret after all the meetings.
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= n <= 105
  • +
  • 1 <= meetings.length <= 105
  • +
  • meetings[i].length == 3
  • +
  • 0 <= xi, yi <= n - 1
  • +
  • xi != yi
  • +
  • 1 <= timei <= 105
  • +
  • 1 <= firstPerson <= n - 1
  • +
+
\ No newline at end of file From df91588e11eedcd6af2d8d4da9d65fa9f07de1df Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 24 Feb 2024 23:14:07 +0530 Subject: [PATCH 0720/1399] Attach NOTES - LeetHub --- 2092-find-all-people-with-secret/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2092-find-all-people-with-secret/NOTES.md diff --git a/2092-find-all-people-with-secret/NOTES.md b/2092-find-all-people-with-secret/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2092-find-all-people-with-secret/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From ccbfdf97a0a214d587e51526ffa7d983d5880263 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 24 Feb 2024 23:14:10 +0530 Subject: [PATCH 0721/1399] Time: 445 ms (75.41%), Space: 164.8 MB (77.46%) - LeetHub --- .../2092-find-all-people-with-secret.cpp | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 2092-find-all-people-with-secret/2092-find-all-people-with-secret.cpp diff --git a/2092-find-all-people-with-secret/2092-find-all-people-with-secret.cpp b/2092-find-all-people-with-secret/2092-find-all-people-with-secret.cpp new file mode 100644 index 00000000..a5443361 --- /dev/null +++ b/2092-find-all-people-with-secret/2092-find-all-people-with-secret.cpp @@ -0,0 +1,60 @@ +class Solution { +public: + vector findAllPeople(int n, vector>& meetings, int firstPerson) { + + vector> adj[n+1]; + + int currTime = 0; + + for(auto& meet : meetings) + { + int person1 = meet[0]; + int person2 = meet[1]; + int meetingTime = meet[2]; + + adj[person1].push_back({person2, meetingTime}); + adj[person2].push_back({person1, meetingTime}); + } + + priority_queue, vector>, greater>> q; // currTime node + + vector visited(n+1, false); + + vector ans; + + q.push({0, 0}); + q.push({0, firstPerson}); + + while(!q.empty()) + { + int currTime = q.top().first; + int person1 = q.top().second; + + q.pop(); + + if(visited[person1]) + continue; + + visited[person1] = true; + + for(auto& meets : adj[person1]) + { + int person2 = meets.first; + int meetingTime = meets.second; + + if(!visited[person2] and currTime <= meetingTime) + { + q.push({meetingTime, person2}); + } + } + } + + for(int i = 0; i < n; ++i) + { + if(visited[i]) + ans.push_back(i); + } + + return ans; + } +}; \ No newline at end of file From bb7a9cdac6032fee07086f9036571625a29b9bbe Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 26 Feb 2024 00:43:58 +0530 Subject: [PATCH 0722/1399] Create README - LeetHub --- .../README.md | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 2709-greatest-common-divisor-traversal/README.md diff --git a/2709-greatest-common-divisor-traversal/README.md b/2709-greatest-common-divisor-traversal/README.md new file mode 100644 index 00000000..b66d140a --- /dev/null +++ b/2709-greatest-common-divisor-traversal/README.md @@ -0,0 +1,38 @@ +

2709. Greatest Common Divisor Traversal

Hard


You are given a 0-indexed integer array nums, and you are allowed to traverse between its indices. You can traverse between index i and index j, i != j, if and only if gcd(nums[i], nums[j]) > 1, where gcd is the greatest common divisor.

+ +

Your task is to determine if for every pair of indices i and j in nums, where i < j, there exists a sequence of traversals that can take us from i to j.

+ +

Return true if it is possible to traverse between all such pairs of indices, or false otherwise.

+ +

 

+

Example 1:

+ +
Input: nums = [2,3,6]
+Output: true
+Explanation: In this example, there are 3 possible pairs of indices: (0, 1), (0, 2), and (1, 2).
+To go from index 0 to index 1, we can use the sequence of traversals 0 -> 2 -> 1, where we move from index 0 to index 2 because gcd(nums[0], nums[2]) = gcd(2, 6) = 2 > 1, and then move from index 2 to index 1 because gcd(nums[2], nums[1]) = gcd(6, 3) = 3 > 1.
+To go from index 0 to index 2, we can just go directly because gcd(nums[0], nums[2]) = gcd(2, 6) = 2 > 1. Likewise, to go from index 1 to index 2, we can just go directly because gcd(nums[1], nums[2]) = gcd(3, 6) = 3 > 1.
+
+ +

Example 2:

+ +
Input: nums = [3,9,5]
+Output: false
+Explanation: No sequence of traversals can take us from index 0 to index 2 in this example. So, we return false.
+
+ +

Example 3:

+ +
Input: nums = [4,3,12,8]
+Output: true
+Explanation: There are 6 possible pairs of indices to traverse between: (0, 1), (0, 2), (0, 3), (1, 2), (1, 3), and (2, 3). A valid sequence of traversals exists for each pair, so we return true.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 105
  • +
  • 1 <= nums[i] <= 105
  • +
+
\ No newline at end of file From e6789154d00f8b01cee879ac9bcb8d32e6c5414e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 26 Feb 2024 00:43:59 +0530 Subject: [PATCH 0723/1399] Attach NOTES - LeetHub --- 2709-greatest-common-divisor-traversal/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2709-greatest-common-divisor-traversal/NOTES.md diff --git a/2709-greatest-common-divisor-traversal/NOTES.md b/2709-greatest-common-divisor-traversal/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2709-greatest-common-divisor-traversal/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 373b36be7cd361de1719c35743d7f4b2f41dc470 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 26 Feb 2024 00:44:03 +0530 Subject: [PATCH 0724/1399] Time: 404 ms (71.86%), Space: 91.3 MB (80.84%) - LeetHub --- ...2709-greatest-common-divisor-traversal.cpp | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 2709-greatest-common-divisor-traversal/2709-greatest-common-divisor-traversal.cpp diff --git a/2709-greatest-common-divisor-traversal/2709-greatest-common-divisor-traversal.cpp b/2709-greatest-common-divisor-traversal/2709-greatest-common-divisor-traversal.cpp new file mode 100644 index 00000000..242430ac --- /dev/null +++ b/2709-greatest-common-divisor-traversal/2709-greatest-common-divisor-traversal.cpp @@ -0,0 +1,58 @@ +class Solution { + int getf(vector &f, int x) { + return f[x] == x ? x : (f[x] = getf(f, f[x])); + } + + void merge(vector &f, vector &num, int x, int y) { + x = getf(f, x); + y = getf(f, y); + if (x == y) { + return; + } + if (num[x] < num[y]) { + swap(x, y); + } + f[y] = x; + num[x] += num[y]; + } +public: + bool canTraverseAllPairs(vector& nums) { + const int n = nums.size(); + if (n == 1) { + return true; + } + vector f(n), num(n); + for (int i = 0; i < n; ++i) { + f[i] = i; + num[i] = 1; + } + unordered_map have; + for (int i = 0; i < n; ++i) { + int x = nums[i]; + if (x == 1) { + return false; + } + for (int d = 2; d * d <= x; ++d) { + if (x % d == 0) { + if (have.count(d)) { + merge(f, num, i, have[d]); + } else { + have[d] = i; + } + while (x % d == 0) { + x /= d; + } + } + } + if (x > 1) { + if (have.count(x)) { + merge(f, num, i, have[x]); + } else { + have[x] = i; + } + } + } + return num[getf(f, 0)] == n; + + } +}; \ No newline at end of file From f0aaff7da5ef1768dd32e22c2a294eda718c1509 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 27 Feb 2024 08:28:34 +0530 Subject: [PATCH 0725/1399] Create README - LeetHub --- 0543-diameter-of-binary-tree/README.md | 28 ++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 0543-diameter-of-binary-tree/README.md diff --git a/0543-diameter-of-binary-tree/README.md b/0543-diameter-of-binary-tree/README.md new file mode 100644 index 00000000..26e6fc34 --- /dev/null +++ b/0543-diameter-of-binary-tree/README.md @@ -0,0 +1,28 @@ +

543. Diameter of Binary Tree

Easy


Given the root of a binary tree, return the length of the diameter of the tree.

+ +

The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root.

+ +

The length of a path between two nodes is represented by the number of edges between them.

+ +

 

+

Example 1:

+ +
Input: root = [1,2,3,4,5]
+Output: 3
+Explanation: 3 is the length of the path [4,2,1,3] or [5,2,1,3].
+
+ +

Example 2:

+ +
Input: root = [1,2]
+Output: 1
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is in the range [1, 104].
  • +
  • -100 <= Node.val <= 100
  • +
+
\ No newline at end of file From 8d76f3c8f2f153932d96ab9a13d9b5419316e51f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 27 Feb 2024 08:28:39 +0530 Subject: [PATCH 0726/1399] Time: 0 ms (100.00%), Space: 19 MB (70.17%) - LeetHub --- .../0543-diameter-of-binary-tree.cpp | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 0543-diameter-of-binary-tree/0543-diameter-of-binary-tree.cpp diff --git a/0543-diameter-of-binary-tree/0543-diameter-of-binary-tree.cpp b/0543-diameter-of-binary-tree/0543-diameter-of-binary-tree.cpp new file mode 100644 index 00000000..c381414a --- /dev/null +++ b/0543-diameter-of-binary-tree/0543-diameter-of-binary-tree.cpp @@ -0,0 +1,33 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + + int helper(TreeNode* root, int& maxi) + { + if(!root) + return 0; + + int left = helper(root->left, maxi); + int right = helper(root->right, maxi); + + maxi = max(maxi, left + right); + return 1 + max(left, right); + } + + int diameterOfBinaryTree(TreeNode* root) { + + int maxi = 0; + helper(root, maxi); + return maxi; + } +}; \ No newline at end of file From abac4eb30905749f1de9deb5e13e785c82fa9d2a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 27 Feb 2024 08:31:47 +0530 Subject: [PATCH 0727/1399] Attach NOTES - LeetHub --- 0543-diameter-of-binary-tree/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0543-diameter-of-binary-tree/NOTES.md diff --git a/0543-diameter-of-binary-tree/NOTES.md b/0543-diameter-of-binary-tree/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0543-diameter-of-binary-tree/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From f984f1c174f69d6c7d2a1104b11307562dcd163b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 28 Feb 2024 08:54:46 +0530 Subject: [PATCH 0728/1399] Create README - LeetHub --- 0513-find-bottom-left-tree-value/README.md | 23 ++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 0513-find-bottom-left-tree-value/README.md diff --git a/0513-find-bottom-left-tree-value/README.md b/0513-find-bottom-left-tree-value/README.md new file mode 100644 index 00000000..a733cb60 --- /dev/null +++ b/0513-find-bottom-left-tree-value/README.md @@ -0,0 +1,23 @@ +

513. Find Bottom Left Tree Value

Medium


Given the root of a binary tree, return the leftmost value in the last row of the tree.

+ +

 

+

Example 1:

+ +
Input: root = [2,1,3]
+Output: 1
+
+ +

Example 2:

+ +
Input: root = [1,2,3,4,null,5,6,null,null,7]
+Output: 7
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is in the range [1, 104].
  • +
  • -231 <= Node.val <= 231 - 1
  • +
+
\ No newline at end of file From bca6d4be3cf1c02c3bcc05372a3f8dad81ed2373 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 28 Feb 2024 08:54:46 +0530 Subject: [PATCH 0729/1399] Attach NOTES - LeetHub --- 0513-find-bottom-left-tree-value/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0513-find-bottom-left-tree-value/NOTES.md diff --git a/0513-find-bottom-left-tree-value/NOTES.md b/0513-find-bottom-left-tree-value/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0513-find-bottom-left-tree-value/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 8fac285a983ce98fa6cd93512faff9d5ce68efb8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 28 Feb 2024 08:54:49 +0530 Subject: [PATCH 0730/1399] Time: 13 ms (22.41%), Space: 20.1 MB (76.07%) - LeetHub --- .../0513-find-bottom-left-tree-value.cpp | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 0513-find-bottom-left-tree-value/0513-find-bottom-left-tree-value.cpp diff --git a/0513-find-bottom-left-tree-value/0513-find-bottom-left-tree-value.cpp b/0513-find-bottom-left-tree-value/0513-find-bottom-left-tree-value.cpp new file mode 100644 index 00000000..2859c4be --- /dev/null +++ b/0513-find-bottom-left-tree-value/0513-find-bottom-left-tree-value.cpp @@ -0,0 +1,39 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + + map mp; + + void findLeftValue(TreeNode* root, int currLevel , int& maxLevel) + { + if(root) + { + findLeftValue(root->left, currLevel + 1, maxLevel); + findLeftValue(root->right, currLevel + 1, maxLevel); + + maxLevel = max(maxLevel , currLevel); + + if(mp.find(maxLevel) == mp.end()) + mp[maxLevel] = root->val; + } + } + + int findBottomLeftValue(TreeNode* root) { + + int maxLevel = 0; + + findLeftValue(root, 0, maxLevel); + + return mp[maxLevel]; + } +}; \ No newline at end of file From 9970cc774ea0064ba70119892d6db378a0679ac6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 28 Feb 2024 08:57:02 +0530 Subject: [PATCH 0731/1399] Attach NOTES - LeetHub From 6012647329134c912cc777be3e3dfeb510d5ebdb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 28 Feb 2024 08:57:05 +0530 Subject: [PATCH 0732/1399] Time: 13 ms (22.41%), Space: 20.1 MB (76.07%) - LeetHub From 0d0e719a131e17397016ee54aca43e116a03baa3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 29 Feb 2024 23:21:28 +0530 Subject: [PATCH 0733/1399] Create README - LeetHub --- 1609-even-odd-tree/README.md | 49 ++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 1609-even-odd-tree/README.md diff --git a/1609-even-odd-tree/README.md b/1609-even-odd-tree/README.md new file mode 100644 index 00000000..706e4f8f --- /dev/null +++ b/1609-even-odd-tree/README.md @@ -0,0 +1,49 @@ +

1609. Even Odd Tree

Medium


A binary tree is named Even-Odd if it meets the following conditions:

+ +
    +
  • The root of the binary tree is at level index 0, its children are at level index 1, their children are at level index 2, etc.
  • +
  • For every even-indexed level, all nodes at the level have odd integer values in strictly increasing order (from left to right).
  • +
  • For every odd-indexed level, all nodes at the level have even integer values in strictly decreasing order (from left to right).
  • +
+ +

Given the root of a binary tree, return true if the binary tree is Even-Odd, otherwise return false.

+ +

 

+

Example 1:

+ +
Input: root = [1,10,4,3,null,7,9,12,8,6,null,null,2]
+Output: true
+Explanation: The node values on each level are:
+Level 0: [1]
+Level 1: [10,4]
+Level 2: [3,7,9]
+Level 3: [12,8,6,2]
+Since levels 0 and 2 are all odd and increasing and levels 1 and 3 are all even and decreasing, the tree is Even-Odd.
+
+ +

Example 2:

+ +
Input: root = [5,4,2,3,3,7]
+Output: false
+Explanation: The node values on each level are:
+Level 0: [5]
+Level 1: [4,2]
+Level 2: [3,3,7]
+Node values in level 2 must be in strictly increasing order, so the tree is not Even-Odd.
+
+ +

Example 3:

+ +
Input: root = [5,9,1,3,5,7]
+Output: false
+Explanation: Node values in the level 1 should be even integers.
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is in the range [1, 105].
  • +
  • 1 <= Node.val <= 106
  • +
+
\ No newline at end of file From 54f718da899d2475f15650869d412a6ebad67f3f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 29 Feb 2024 23:21:32 +0530 Subject: [PATCH 0734/1399] Time: 702 ms (5.00%), Space: 158.2 MB (21.32%) - LeetHub --- 1609-even-odd-tree/1609-even-odd-tree.cpp | 98 +++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 1609-even-odd-tree/1609-even-odd-tree.cpp diff --git a/1609-even-odd-tree/1609-even-odd-tree.cpp b/1609-even-odd-tree/1609-even-odd-tree.cpp new file mode 100644 index 00000000..ccdc9a04 --- /dev/null +++ b/1609-even-odd-tree/1609-even-odd-tree.cpp @@ -0,0 +1,98 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + + bool checkAscending(vector& currLevel) + { + int n = currLevel.size(); + + if(currLevel[0] % 2 == 0) + return false; + + for(int i = 1; i < n; ++i) + { + if(currLevel[i] <= currLevel[i-1]) + return false; + if(currLevel[i] % 2 == 0) + return false; + } + return true; + } + + bool checkDescending(vector& currLevel) + { + int n = currLevel.size(); + + if(currLevel[0] & 1) + return false; + for(int i = 1; i < n; ++i) + { + if(currLevel[i] >= currLevel[i-1]) + return false; + if(currLevel[i] & 1) + return false; + } + return true; + } + + bool isEvenOddTree(TreeNode* root) { + + int level = 0; + + queue q; + + q.push(root); + + bool isEvOddTree = true; + + while(!q.empty()) + { + int size = q.size(); + + vector currLevel; + + for(int i = 0; i < size; ++i) + { + TreeNode* curr = q.front(); + q.pop(); + + currLevel.push_back(curr->val); + + if(curr->left) + q.push(curr->left); + if(curr->right) + q.push(curr->right); + + if(level & 1) + { + if(!checkDescending(currLevel)) + { + isEvOddTree = false; + break; + } + } + else + { + if(!checkAscending(currLevel)) + { + isEvOddTree = false; + break; + } + } + } + ++level; + } + + return isEvOddTree; + } +}; \ No newline at end of file From 6565f8c45a2183265b75f034baef05444cb5f9f4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 29 Feb 2024 23:22:57 +0530 Subject: [PATCH 0735/1399] Attach NOTES - LeetHub --- 1609-even-odd-tree/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1609-even-odd-tree/NOTES.md diff --git a/1609-even-odd-tree/NOTES.md b/1609-even-odd-tree/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1609-even-odd-tree/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From e77cd15dc5f6293cb879634eb23ace70c54279e6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 29 Feb 2024 23:23:01 +0530 Subject: [PATCH 0736/1399] Time: 702 ms (5.00%), Space: 158.2 MB (21.32%) - LeetHub From a2cd0413213c551f72a8a5c964b7db4ee62a2b06 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 1 Mar 2024 23:43:33 +0530 Subject: [PATCH 0737/1399] Create README - LeetHub --- 2864-maximum-odd-binary-number/README.md | 32 ++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 2864-maximum-odd-binary-number/README.md diff --git a/2864-maximum-odd-binary-number/README.md b/2864-maximum-odd-binary-number/README.md new file mode 100644 index 00000000..598ac213 --- /dev/null +++ b/2864-maximum-odd-binary-number/README.md @@ -0,0 +1,32 @@ +

2864. Maximum Odd Binary Number

Easy


You are given a binary string s that contains at least one '1'.

+ +

You have to rearrange the bits in such a way that the resulting binary number is the maximum odd binary number that can be created from this combination.

+ +

Return a string representing the maximum odd binary number that can be created from the given combination.

+ +

Note that the resulting string can have leading zeros.

+ +

 

+

Example 1:

+ +
Input: s = "010"
+Output: "001"
+Explanation: Because there is just one '1', it must be in the last position. So the answer is "001".
+
+ +

Example 2:

+ +
Input: s = "0101"
+Output: "1001"
+Explanation: One of the '1's must be in the last position. The maximum number that can be made with the remaining digits is "100". So the answer is "1001".
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 100
  • +
  • s consists only of '0' and '1'.
  • +
  • s contains at least one '1'.
  • +
+
\ No newline at end of file From 3e38f406fd3c349f7d42134ef60304845639c811 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 1 Mar 2024 23:43:37 +0530 Subject: [PATCH 0738/1399] Time: 0 ms (100.00%), Space: 8.3 MB (55.89%) - LeetHub --- .../2864-maximum-odd-binary-number.cpp | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 2864-maximum-odd-binary-number/2864-maximum-odd-binary-number.cpp diff --git a/2864-maximum-odd-binary-number/2864-maximum-odd-binary-number.cpp b/2864-maximum-odd-binary-number/2864-maximum-odd-binary-number.cpp new file mode 100644 index 00000000..949cc660 --- /dev/null +++ b/2864-maximum-odd-binary-number/2864-maximum-odd-binary-number.cpp @@ -0,0 +1,26 @@ +class Solution { +public: + string maximumOddBinaryNumber(string s) { + + int n = s.size(); + int start = 0, end = n-1; + + while(start <= end) + { + if(s[start] == '1') + ++start; + else if(s[end] == '1') + { + swap(s[start], s[end]); + --end; + ++start; + } + else + --end; + } + swap(s[start-1], s[n-1]); + + return s; + + } +}; \ No newline at end of file From c4828db7ec8590d3b64ceb3badbbcc6778c74379 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 1 Mar 2024 23:44:28 +0530 Subject: [PATCH 0739/1399] Attach NOTES - LeetHub --- 2864-maximum-odd-binary-number/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2864-maximum-odd-binary-number/NOTES.md diff --git a/2864-maximum-odd-binary-number/NOTES.md b/2864-maximum-odd-binary-number/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2864-maximum-odd-binary-number/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 585ba2da91e3eeab60ac0ff4f294dee5ffc7319c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 1 Mar 2024 23:44:31 +0530 Subject: [PATCH 0740/1399] Time: 0 ms (100.00%), Space: 8.3 MB (55.89%) - LeetHub From c201810e408fa12181e870d648bb98d655ba05be Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Mar 2024 08:19:53 +0530 Subject: [PATCH 0741/1399] Attach NOTES - LeetHub --- 0977-squares-of-a-sorted-array/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0977-squares-of-a-sorted-array/NOTES.md diff --git a/0977-squares-of-a-sorted-array/NOTES.md b/0977-squares-of-a-sorted-array/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0977-squares-of-a-sorted-array/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From dad71fb27d76ab7a8e361ee32ef94719907c80f3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Mar 2024 08:19:54 +0530 Subject: [PATCH 0742/1399] Create README - LeetHub --- 0977-squares-of-a-sorted-array/README.md | 28 ++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 0977-squares-of-a-sorted-array/README.md diff --git a/0977-squares-of-a-sorted-array/README.md b/0977-squares-of-a-sorted-array/README.md new file mode 100644 index 00000000..9e0b7c42 --- /dev/null +++ b/0977-squares-of-a-sorted-array/README.md @@ -0,0 +1,28 @@ +

977. Squares of a Sorted Array

Easy


Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order.

+ +

 

+

Example 1:

+ +
Input: nums = [-4,-1,0,3,10]
+Output: [0,1,9,16,100]
+Explanation: After squaring, the array becomes [16,1,0,9,100].
+After sorting, it becomes [0,1,9,16,100].
+
+ +

Example 2:

+ +
Input: nums = [-7,-3,2,3,11]
+Output: [4,9,9,49,121]
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 104
  • +
  • -104 <= nums[i] <= 104
  • +
  • nums is sorted in non-decreasing order.
  • +
+ +

 

+Follow up: Squaring each element and sorting the new array is very trivial, could you find an O(n) solution using a different approach?
\ No newline at end of file From 05cc6520e43a2aaede214399edb7373405ccd6df Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Mar 2024 08:19:57 +0530 Subject: [PATCH 0743/1399] Time: 27 ms (43.10%), Space: 30.6 MB (8.75%) - LeetHub --- .../0977-squares-of-a-sorted-array.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 0977-squares-of-a-sorted-array/0977-squares-of-a-sorted-array.cpp diff --git a/0977-squares-of-a-sorted-array/0977-squares-of-a-sorted-array.cpp b/0977-squares-of-a-sorted-array/0977-squares-of-a-sorted-array.cpp new file mode 100644 index 00000000..ec7edff6 --- /dev/null +++ b/0977-squares-of-a-sorted-array/0977-squares-of-a-sorted-array.cpp @@ -0,0 +1,15 @@ +class Solution { +public: + vector sortedSquares(vector& nums) { + + vector squares; + + for(auto& ele : nums) + squares.push_back(ele * ele); + + sort(squares.begin(), squares.end()); + + return squares; + + } +}; \ No newline at end of file From 90be8e16b588c251503401a2ffa13102a6835da5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Mar 2024 08:20:06 +0530 Subject: [PATCH 0744/1399] Attach NOTES - LeetHub From 94cbf8ec6f114f314a15bc900b4ab5ab2e1d0f6b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Mar 2024 08:20:10 +0530 Subject: [PATCH 0745/1399] Time: 27 ms (43.10%), Space: 30.6 MB (8.75%) - LeetHub From 4c8e32118dc32077ad8fc7843b27b4590061487e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Mar 2024 08:24:36 +0530 Subject: [PATCH 0746/1399] Attach NOTES - LeetHub From c3e70b2dfa69027b543d7c9cb75ae93d225a6be2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Mar 2024 08:24:40 +0530 Subject: [PATCH 0747/1399] Time: 27 ms (43.10%), Space: 30.6 MB (8.75%) - LeetHub From 248f73e56dd40e12385eb2fa7c5eb61609b898e7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Mar 2024 08:29:02 +0530 Subject: [PATCH 0748/1399] Attach NOTES - LeetHub From 2da2fd1c78e2321d12598737081d906b7b173c81 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Mar 2024 08:29:05 +0530 Subject: [PATCH 0749/1399] Time: 27 ms (43.10%), Space: 30.6 MB (8.75%) - LeetHub From 88f994cf4751f95e56e1b4e75047e9a8cc42f38e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Mar 2024 08:32:32 +0530 Subject: [PATCH 0750/1399] Attach NOTES - LeetHub From f11750461eb9a3d9f4faa5671f230b5b25ea09c0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 2 Mar 2024 08:32:36 +0530 Subject: [PATCH 0751/1399] Time: 22 ms (69.10%), Space: 28.4 MB (58.32%) - LeetHub --- .../0977-squares-of-a-sorted-array.cpp | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/0977-squares-of-a-sorted-array/0977-squares-of-a-sorted-array.cpp b/0977-squares-of-a-sorted-array/0977-squares-of-a-sorted-array.cpp index ec7edff6..b31ae1e2 100644 --- a/0977-squares-of-a-sorted-array/0977-squares-of-a-sorted-array.cpp +++ b/0977-squares-of-a-sorted-array/0977-squares-of-a-sorted-array.cpp @@ -2,14 +2,26 @@ class Solution { public: vector sortedSquares(vector& nums) { - vector squares; + int n = nums.size(); + vector squares(n); + + int i = 0, j = n-1, k = n-1; - for(auto& ele : nums) - squares.push_back(ele * ele); - - sort(squares.begin(), squares.end()); + while(k >= 0) + { + if(abs(nums[i]) > abs(nums[j])) + { + squares[k] = nums[i] * nums[i]; + ++i; + } + else + { + squares[k] = nums[j] * nums[j]; + --j; + } + --k; + } return squares; - } }; \ No newline at end of file From 17e300e4712e9bca32d24eaf87296947eea1265f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 3 Mar 2024 22:48:15 +0530 Subject: [PATCH 0752/1399] Create README - LeetHub --- .../README.md | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 0019-remove-nth-node-from-end-of-list/README.md diff --git a/0019-remove-nth-node-from-end-of-list/README.md b/0019-remove-nth-node-from-end-of-list/README.md new file mode 100644 index 00000000..b1ecfb61 --- /dev/null +++ b/0019-remove-nth-node-from-end-of-list/README.md @@ -0,0 +1,34 @@ +

19. Remove Nth Node From End of List

Medium


Given the head of a linked list, remove the nth node from the end of the list and return its head.

+ +

 

+

Example 1:

+ +
Input: head = [1,2,3,4,5], n = 2
+Output: [1,2,3,5]
+
+ +

Example 2:

+ +
Input: head = [1], n = 1
+Output: []
+
+ +

Example 3:

+ +
Input: head = [1,2], n = 1
+Output: [1]
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the list is sz.
  • +
  • 1 <= sz <= 30
  • +
  • 0 <= Node.val <= 100
  • +
  • 1 <= n <= sz
  • +
+ +

 

+

Follow up: Could you do this in one pass?

+
\ No newline at end of file From d912941ac3e5bdacc3b06c8446559d231076d68f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 3 Mar 2024 22:48:15 +0530 Subject: [PATCH 0753/1399] Attach NOTES - LeetHub --- 0019-remove-nth-node-from-end-of-list/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0019-remove-nth-node-from-end-of-list/NOTES.md diff --git a/0019-remove-nth-node-from-end-of-list/NOTES.md b/0019-remove-nth-node-from-end-of-list/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0019-remove-nth-node-from-end-of-list/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 30e21462f79b5768c4ea682b6324fcd060516377 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 3 Mar 2024 22:48:19 +0530 Subject: [PATCH 0754/1399] Time: 7 ms (19.64%), Space: 13.2 MB (66.71%) - LeetHub --- .../0019-remove-nth-node-from-end-of-list.cpp | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 0019-remove-nth-node-from-end-of-list/0019-remove-nth-node-from-end-of-list.cpp diff --git a/0019-remove-nth-node-from-end-of-list/0019-remove-nth-node-from-end-of-list.cpp b/0019-remove-nth-node-from-end-of-list/0019-remove-nth-node-from-end-of-list.cpp new file mode 100644 index 00000000..7059bd67 --- /dev/null +++ b/0019-remove-nth-node-from-end-of-list/0019-remove-nth-node-from-end-of-list.cpp @@ -0,0 +1,33 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + ListNode* removeNthFromEnd(ListNode* head, int n) { + + ListNode* fast = head, *slow = head; + + while(n--) + fast = fast->next; + + if(!fast) + return head->next; + + while(fast->next) + { + fast = fast->next; + slow = slow->next; + } + + slow->next = slow->next->next; + + return head; + } +}; \ No newline at end of file From 3d5b5cd87f75332e996eddab3e9b08aaffe8f16b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Mar 2024 08:44:09 +0530 Subject: [PATCH 0755/1399] Create README - LeetHub --- 0948-bag-of-tokens/README.md | 106 +++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 0948-bag-of-tokens/README.md diff --git a/0948-bag-of-tokens/README.md b/0948-bag-of-tokens/README.md new file mode 100644 index 00000000..968d02c5 --- /dev/null +++ b/0948-bag-of-tokens/README.md @@ -0,0 +1,106 @@ +

948. Bag of Tokens

Medium


You start with an initial power of power, an initial score of 0, and a bag of tokens given as an integer array tokens, where each tokens[i] donates the value of tokeni.

+ +

Your goal is to maximize the total score by strategically playing these tokens. In one move, you can play an unplayed token in one of the two ways (but not both for the same token):

+ +
    +
  • Face-up: If your current power is at least tokens[i], you may play tokeni, losing tokens[i] power and gaining 1 score.
  • +
  • Face-down: If your current score is at least 1, you may play tokeni, gaining tokens[i] power and losing 1 score.
  • +
+ +

Return the maximum possible score you can achieve after playing any number of tokens.

+ +

 

+

Example 1:

+ +
+

Input: tokens = [100], power = 50

+ +

Output: 0

+ +

Explanation: Since your score is 0 initially, you cannot play the token face-down. You also cannot play it face-up since your power (50) is less than tokens[0] (100).

+
+ +

Example 2:

+ +
+

Input: tokens = [200,100], power = 150

+ +

Output: 1

+ +

Explanation: Play token1 (100) face-up, reducing your power to 50 and increasing your score to 1.

+ +

There is no need to play token0, since you cannot play it face-up to add to your score. The maximum score achievable is 1.

+
+ +

Example 3:

+ +
+

Input: tokens = [100,200,300,400], power = 200

+ +

Output: 2

+ +

Explanation: Play the tokens in this order to get a score of 2:

+ +
    +
  1. Play token0 (100) face-up, reducing power to 100 and increasing score to 1.
  2. +
  3. Play token3 (400) face-down, increasing power to 500 and reducing score to 0.
  4. +
  5. Play token1 (200) face-up, reducing power to 300 and increasing score to 1.
  6. +
  7. Play token2 (300) face-up, reducing power to 0 and increasing score to 2.
  8. +
+ +

The maximum score achievable is 2.

+
+ +

 

+

Constraints:

+ +
    +
  • 0 <= tokens.length <= 1000
  • +
  • 0 <= tokens[i], power < 104
  • +
+
\ No newline at end of file From ed01bab98e89d8f0c509f2ad846aef73990b2b6c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Mar 2024 08:44:13 +0530 Subject: [PATCH 0756/1399] Time: 9 ms (18.89%), Space: 13.2 MB (23.53%) - LeetHub --- 0948-bag-of-tokens/0948-bag-of-tokens.cpp | 29 +++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 0948-bag-of-tokens/0948-bag-of-tokens.cpp diff --git a/0948-bag-of-tokens/0948-bag-of-tokens.cpp b/0948-bag-of-tokens/0948-bag-of-tokens.cpp new file mode 100644 index 00000000..b1fbb888 --- /dev/null +++ b/0948-bag-of-tokens/0948-bag-of-tokens.cpp @@ -0,0 +1,29 @@ +class Solution { +public: + int bagOfTokensScore(vector& tokens, int power) { + + int start = 0, end = tokens.size()-1; + int score = 0, maxScore = 0; + + sort(tokens.begin(), tokens.end()); + + while(start <= end) + { + if(power >= tokens[start]) + { + ++score; + maxScore = max(maxScore, score); + power -= tokens[start++]; + } + else if(score > 0) + { + power += tokens[end--]; + --score; + } + else + break; + } + + return maxScore; + } +}; \ No newline at end of file From 6c9bd3e02165ad621ec2946dc08638b8654d2876 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Mar 2024 08:48:40 +0530 Subject: [PATCH 0757/1399] Attach NOTES - LeetHub --- 0948-bag-of-tokens/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0948-bag-of-tokens/NOTES.md diff --git a/0948-bag-of-tokens/NOTES.md b/0948-bag-of-tokens/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0948-bag-of-tokens/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 93ef9308020301dc10dad5014ba505e7d575f673 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Mar 2024 08:48:43 +0530 Subject: [PATCH 0758/1399] Time: 9 ms (18.89%), Space: 13.2 MB (23.53%) - LeetHub From 3061cb3f5c3f1a237ff81f3ac9c816dfba6642f3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Mar 2024 19:08:29 +0530 Subject: [PATCH 0759/1399] Create README - LeetHub --- 2680-maximum-or/README.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 2680-maximum-or/README.md diff --git a/2680-maximum-or/README.md b/2680-maximum-or/README.md new file mode 100644 index 00000000..1d8976a3 --- /dev/null +++ b/2680-maximum-or/README.md @@ -0,0 +1,30 @@ +

2680. Maximum OR

Medium


You are given a 0-indexed integer array nums of length n and an integer k. In an operation, you can choose an element and multiply it by 2.

+ +

Return the maximum possible value of nums[0] | nums[1] | ... | nums[n - 1] that can be obtained after applying the operation on nums at most k times.

+ +

Note that a | b denotes the bitwise or between two integers a and b.

+ +

 

+

Example 1:

+ +
Input: nums = [12,9], k = 1
+Output: 30
+Explanation: If we apply the operation to index 1, our new array nums will be equal to [12,18]. Thus, we return the bitwise or of 12 and 18, which is 30.
+
+ +

Example 2:

+ +
Input: nums = [8,1,2], k = 2
+Output: 35
+Explanation: If we apply the operation twice on index 0, we yield a new array of [32,1,2]. Thus, we return 32|1|2 = 35.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 105
  • +
  • 1 <= nums[i] <= 109
  • +
  • 1 <= k <= 15
  • +
+
\ No newline at end of file From 9293cb2b85283c36e95262792cb7d00b02cf77e1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Mar 2024 19:08:29 +0530 Subject: [PATCH 0760/1399] Attach NOTES - LeetHub --- 2680-maximum-or/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2680-maximum-or/NOTES.md diff --git a/2680-maximum-or/NOTES.md b/2680-maximum-or/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2680-maximum-or/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 63dba817844aaa72bd0f2ed56351d68d75b05e71 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Mar 2024 19:08:32 +0530 Subject: [PATCH 0761/1399] Time: 214 ms (7.02%), Space: 103 MB (10.53%) - LeetHub --- 2680-maximum-or/2680-maximum-or.cpp | 90 +++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 2680-maximum-or/2680-maximum-or.cpp diff --git a/2680-maximum-or/2680-maximum-or.cpp b/2680-maximum-or/2680-maximum-or.cpp new file mode 100644 index 00000000..45f8f9f2 --- /dev/null +++ b/2680-maximum-or/2680-maximum-or.cpp @@ -0,0 +1,90 @@ +class SEG +{ + public: + int N; + + vector tree, arr; + + SEG(vector v) + { + N = v.size(); + arr.resize(N); + + for(int i = 0; i < N; ++i) + arr[i] = v[i]; + + tree.resize(4*N + 5); + + buildTree(0, 0, N-1); + } + + public: + void buildTree(int idx, int low, int high) + { + if(low == high) + { + tree[idx] = arr[low]; + return; + } + + int mid = (low + high) / 2; + buildTree(2*idx+1, low, mid); + buildTree(2*idx+2, mid+1, high); + + tree[idx] = tree[2*idx+1] | tree[2*idx+2]; + } + + int queryTree(int idx, int low, int high, int l, int r) + { + if(r < low or l > high) + return 0; + + if(low >= l and high <= r) + return tree[idx]; + + int mid = (low + high) / 2; + int left = queryTree(2*idx+1, low, mid, l, r); + int right = queryTree(2*idx+2, mid+1, high, l, r); + + return left | right; + } + + int query(int l, int r) + { + return queryTree(0, 0, N-1, l, r); + } +}; + + +class Solution { +public: + long long maximumOr(vector& nums, int k) { + + int n = nums.size(); + + SEG seg(nums); + + long long res = 0; + + for(int i = 0; i < n; ++i) + { + long long left = seg.query(0, i-1); + long long right = seg.query(i+1, n-1); + + long long curr = nums[i], j = 0; + + while(j < k) + { + curr *= 2; + ++j; + } + + long long ans = curr | left | right; + + res = max(res, ans); + } + + return res; + + } +}; \ No newline at end of file From 2abca6f44108096a9258eea7c800ef8460c0288e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Mar 2024 19:10:00 +0530 Subject: [PATCH 0762/1399] Attach NOTES - LeetHub From 3a17fdde9a54156bc8f9ff121c6cf10529521c67 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Mar 2024 19:10:03 +0530 Subject: [PATCH 0763/1399] Time: 214 ms (7.02%), Space: 103 MB (10.53%) - LeetHub From 20cd051f0478c41a375e2251b0e29b42f239aa53 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Mar 2024 19:10:34 +0530 Subject: [PATCH 0764/1399] Attach NOTES - LeetHub From 2d3388ee702acd5c76efc211a124fd941feaad76 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Mar 2024 19:10:37 +0530 Subject: [PATCH 0765/1399] Time: 214 ms (7.02%), Space: 103 MB (10.53%) - LeetHub From b964a3947718c0f8ef9e0e7707c8d3c6941c169c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Mar 2024 19:51:51 +0530 Subject: [PATCH 0766/1399] Attach NOTES - LeetHub From 54a2121a5edf8e5898b3cbeee8c42464ff9852c0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 4 Mar 2024 19:51:54 +0530 Subject: [PATCH 0767/1399] Time: 109 ms (68.42%), Space: 93 MB (76.75%) - LeetHub --- 2680-maximum-or/2680-maximum-or.cpp | 86 +++++------------------------ 1 file changed, 14 insertions(+), 72 deletions(-) diff --git a/2680-maximum-or/2680-maximum-or.cpp b/2680-maximum-or/2680-maximum-or.cpp index 45f8f9f2..21840949 100644 --- a/2680-maximum-or/2680-maximum-or.cpp +++ b/2680-maximum-or/2680-maximum-or.cpp @@ -1,90 +1,32 @@ -class SEG -{ - public: - int N; - - vector tree, arr; - - SEG(vector v) - { - N = v.size(); - arr.resize(N); - - for(int i = 0; i < N; ++i) - arr[i] = v[i]; - - tree.resize(4*N + 5); - - buildTree(0, 0, N-1); - } - - public: - void buildTree(int idx, int low, int high) - { - if(low == high) - { - tree[idx] = arr[low]; - return; - } - - int mid = (low + high) / 2; - buildTree(2*idx+1, low, mid); - buildTree(2*idx+2, mid+1, high); - - tree[idx] = tree[2*idx+1] | tree[2*idx+2]; - } - - int queryTree(int idx, int low, int high, int l, int r) - { - if(r < low or l > high) - return 0; - - if(low >= l and high <= r) - return tree[idx]; - - int mid = (low + high) / 2; - int left = queryTree(2*idx+1, low, mid, l, r); - int right = queryTree(2*idx+2, mid+1, high, l, r); - - return left | right; - } - - int query(int l, int r) - { - return queryTree(0, 0, N-1, l, r); - } -}; - +using ll = long long; class Solution { public: long long maximumOr(vector& nums, int k) { int n = nums.size(); - SEG seg(nums); + vector pref(n, 0), suff(n, 0); - long long res = 0; + pref[0] = nums[0], suff[n-1] = nums[n-1]; + + for(int i = 1; i < n; ++i) + pref[i] = pref[i-1] | nums[i]; + + for(int i = n-2; i >= 0; --i) + suff[i] = suff[i+1] | nums[i]; + + ll res = 0, mult = 1 << k; for(int i = 0; i < n; ++i) { - long long left = seg.query(0, i-1); - long long right = seg.query(i+1, n-1); - - long long curr = nums[i], j = 0; - - while(j < k) - { - curr *= 2; - ++j; - } - - long long ans = curr | left | right; + ll left = (i == 0 ? 0 : pref[i-1]); + ll right = (i+1 == n ? 0 : suff[i+1]); + ll ans = left | (mult*nums[i]) | right; res = max(res, ans); } return res; - } }; \ No newline at end of file From e6bbedcb5fa0f70322708cc1e6058352f134351e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 5 Mar 2024 23:23:16 +0530 Subject: [PATCH 0768/1399] Create README - LeetHub --- .../README.md | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 1750-minimum-length-of-string-after-deleting-similar-ends/README.md diff --git a/1750-minimum-length-of-string-after-deleting-similar-ends/README.md b/1750-minimum-length-of-string-after-deleting-similar-ends/README.md new file mode 100644 index 00000000..c64aa797 --- /dev/null +++ b/1750-minimum-length-of-string-after-deleting-similar-ends/README.md @@ -0,0 +1,47 @@ +

1750. Minimum Length of String After Deleting Similar Ends

Medium


Given a string s consisting only of characters 'a', 'b', and 'c'. You are asked to apply the following algorithm on the string any number of times:

+ +
    +
  1. Pick a non-empty prefix from the string s where all the characters in the prefix are equal.
  2. +
  3. Pick a non-empty suffix from the string s where all the characters in this suffix are equal.
  4. +
  5. The prefix and the suffix should not intersect at any index.
  6. +
  7. The characters from the prefix and suffix must be the same.
  8. +
  9. Delete both the prefix and the suffix.
  10. +
+ +

Return the minimum length of s after performing the above operation any number of times (possibly zero times).

+ +

 

+

Example 1:

+ +
Input: s = "ca"
+Output: 2
+Explanation: You can't remove any characters, so the string stays as is.
+
+ +

Example 2:

+ +
Input: s = "cabaabac"
+Output: 0
+Explanation: An optimal sequence of operations is:
+- Take prefix = "c" and suffix = "c" and remove them, s = "abaaba".
+- Take prefix = "a" and suffix = "a" and remove them, s = "baab".
+- Take prefix = "b" and suffix = "b" and remove them, s = "aa".
+- Take prefix = "a" and suffix = "a" and remove them, s = "".
+ +

Example 3:

+ +
Input: s = "aabccabba"
+Output: 3
+Explanation: An optimal sequence of operations is:
+- Take prefix = "aa" and suffix = "a" and remove them, s = "bccabb".
+- Take prefix = "b" and suffix = "bb" and remove them, s = "cca".
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 105
  • +
  • s only consists of characters 'a', 'b', and 'c'.
  • +
+
\ No newline at end of file From f146a02b9313daabedf4acd1e6c42d6cf4763ac0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 5 Mar 2024 23:23:19 +0530 Subject: [PATCH 0769/1399] Time: 19 ms (96.90%), Space: 14 MB (13.45%) - LeetHub --- ...-of-string-after-deleting-similar-ends.cpp | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 1750-minimum-length-of-string-after-deleting-similar-ends/1750-minimum-length-of-string-after-deleting-similar-ends.cpp diff --git a/1750-minimum-length-of-string-after-deleting-similar-ends/1750-minimum-length-of-string-after-deleting-similar-ends.cpp b/1750-minimum-length-of-string-after-deleting-similar-ends/1750-minimum-length-of-string-after-deleting-similar-ends.cpp new file mode 100644 index 00000000..b5a5ce2c --- /dev/null +++ b/1750-minimum-length-of-string-after-deleting-similar-ends/1750-minimum-length-of-string-after-deleting-similar-ends.cpp @@ -0,0 +1,26 @@ +class Solution { +public: + int minimumLength(string s) { + + int n = s.size(); + int i = 0, j = n-1; + + while(i < j) + { + char pref = s[i]; + char suff = s[j]; + + if(s[i] != s[j]) + break; + + while(j >= 0 and s[j] == pref) + --j; + while(i < j and s[i] == suff) + ++i; + } + + int ans = j - i + 1; + + return max(ans, 0); + } +}; \ No newline at end of file From 547253fb1823e9dc2e8c8acc796e7439adec6b14 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 7 Mar 2024 07:58:17 +0530 Subject: [PATCH 0770/1399] Create README - LeetHub --- 0876-middle-of-the-linked-list/README.md | 27 ++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 0876-middle-of-the-linked-list/README.md diff --git a/0876-middle-of-the-linked-list/README.md b/0876-middle-of-the-linked-list/README.md new file mode 100644 index 00000000..bb08722c --- /dev/null +++ b/0876-middle-of-the-linked-list/README.md @@ -0,0 +1,27 @@ +

876. Middle of the Linked List

Easy


Given the head of a singly linked list, return the middle node of the linked list.

+ +

If there are two middle nodes, return the second middle node.

+ +

 

+

Example 1:

+ +
Input: head = [1,2,3,4,5]
+Output: [3,4,5]
+Explanation: The middle node of the list is node 3.
+
+ +

Example 2:

+ +
Input: head = [1,2,3,4,5,6]
+Output: [4,5,6]
+Explanation: Since the list has two middle nodes with values 3 and 4, we return the second one.
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the list is in the range [1, 100].
  • +
  • 1 <= Node.val <= 100
  • +
+
\ No newline at end of file From 128ca9ce58f6de4fb6b91ecce668600bd6fece90 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 7 Mar 2024 07:58:21 +0530 Subject: [PATCH 0771/1399] Time: 4 ms (30.24%), Space: 8.6 MB (66.29%) - LeetHub --- .../0876-middle-of-the-linked-list.cpp | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 0876-middle-of-the-linked-list/0876-middle-of-the-linked-list.cpp diff --git a/0876-middle-of-the-linked-list/0876-middle-of-the-linked-list.cpp b/0876-middle-of-the-linked-list/0876-middle-of-the-linked-list.cpp new file mode 100644 index 00000000..71c8ab29 --- /dev/null +++ b/0876-middle-of-the-linked-list/0876-middle-of-the-linked-list.cpp @@ -0,0 +1,25 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + ListNode* middleNode(ListNode* head) { + + ListNode* fast = head, *slow = head; + + while(fast and fast->next) + { + slow = slow->next; + fast = fast->next->next; + } + + return slow; + } +}; \ No newline at end of file From dde31514b7ad414c8f64b8c3080ab8077e174712 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 8 Mar 2024 09:35:52 +0530 Subject: [PATCH 0772/1399] Create README - LeetHub --- .../README.md | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 3005-count-elements-with-maximum-frequency/README.md diff --git a/3005-count-elements-with-maximum-frequency/README.md b/3005-count-elements-with-maximum-frequency/README.md new file mode 100644 index 00000000..2b9d7d58 --- /dev/null +++ b/3005-count-elements-with-maximum-frequency/README.md @@ -0,0 +1,31 @@ +

3005. Count Elements With Maximum Frequency

Easy


You are given an array nums consisting of positive integers.

+ +

Return the total frequencies of elements in nums such that those elements all have the maximum frequency.

+ +

The frequency of an element is the number of occurrences of that element in the array.

+ +

 

+

Example 1:

+ +
Input: nums = [1,2,2,3,1,4]
+Output: 4
+Explanation: The elements 1 and 2 have a frequency of 2 which is the maximum frequency in the array.
+So the number of elements in the array with maximum frequency is 4.
+
+ +

Example 2:

+ +
Input: nums = [1,2,3,4,5]
+Output: 5
+Explanation: All elements of the array have a frequency of 1 which is the maximum.
+So the number of elements in the array with maximum frequency is 5.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 100
  • +
  • 1 <= nums[i] <= 100
  • +
+
\ No newline at end of file From 896fe9b10165e7531974d5765235241d547f70b3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 8 Mar 2024 09:35:56 +0530 Subject: [PATCH 0773/1399] Time: 4 ms (68.91%), Space: 22 MB (14.32%) - LeetHub --- ...-count-elements-with-maximum-frequency.cpp | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 3005-count-elements-with-maximum-frequency/3005-count-elements-with-maximum-frequency.cpp diff --git a/3005-count-elements-with-maximum-frequency/3005-count-elements-with-maximum-frequency.cpp b/3005-count-elements-with-maximum-frequency/3005-count-elements-with-maximum-frequency.cpp new file mode 100644 index 00000000..9b517485 --- /dev/null +++ b/3005-count-elements-with-maximum-frequency/3005-count-elements-with-maximum-frequency.cpp @@ -0,0 +1,25 @@ +class Solution { +public: + int maxFrequencyElements(vector& nums) { + + int n = nums.size(); + + vector freq(101, 0); + + int sameFrequencyNum = 0, maxi = 0; + + for(int i = 0; i < n; ++i) + { + ++freq[nums[i]]; + maxi = max(maxi, freq[nums[i]]); + } + + for(int i = 0; i < 101; ++i) + { + if(freq[i] == maxi) + sameFrequencyNum += freq[i]; + } + + return sameFrequencyNum; + } +}; \ No newline at end of file From 785dee411abe66b09d699571c0613d3173e82345 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 9 Mar 2024 11:50:48 +0530 Subject: [PATCH 0774/1399] Create README - LeetHub --- 2540-minimum-common-value/README.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 2540-minimum-common-value/README.md diff --git a/2540-minimum-common-value/README.md b/2540-minimum-common-value/README.md new file mode 100644 index 00000000..acdefbbf --- /dev/null +++ b/2540-minimum-common-value/README.md @@ -0,0 +1,28 @@ +

2540. Minimum Common Value

Easy


Given two integer arrays nums1 and nums2, sorted in non-decreasing order, return the minimum integer common to both arrays. If there is no common integer amongst nums1 and nums2, return -1.

+ +

Note that an integer is said to be common to nums1 and nums2 if both arrays have at least one occurrence of that integer.

+ +

 

+

Example 1:

+ +
Input: nums1 = [1,2,3], nums2 = [2,4]
+Output: 2
+Explanation: The smallest element common to both arrays is 2, so we return 2.
+
+ +

Example 2:

+ +
Input: nums1 = [1,2,3,6], nums2 = [2,3,4,5]
+Output: 2
+Explanation: There are two common elements in the array 2 and 3 out of which 2 is the smallest, so 2 is returned.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums1.length, nums2.length <= 105
  • +
  • 1 <= nums1[i], nums2[j] <= 109
  • +
  • Both nums1 and nums2 are sorted in non-decreasing order.
  • +
+
\ No newline at end of file From cab21d82a642dced227e6a35f5bf3509d6b84186 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 9 Mar 2024 11:50:52 +0530 Subject: [PATCH 0775/1399] Time: 72 ms (54.08%), Space: 53 MB (29.59%) - LeetHub --- .../2540-minimum-common-value.cpp | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 2540-minimum-common-value/2540-minimum-common-value.cpp diff --git a/2540-minimum-common-value/2540-minimum-common-value.cpp b/2540-minimum-common-value/2540-minimum-common-value.cpp new file mode 100644 index 00000000..116f4f9c --- /dev/null +++ b/2540-minimum-common-value/2540-minimum-common-value.cpp @@ -0,0 +1,24 @@ +class Solution { +public: + int getCommon(vector& nums1, vector& nums2) { + + int i = nums1.size() - 1, j = nums2.size() - 1; + + int cnt = 0, ans = -1; + + while(i >= 0 and j >= 0) + { + if(nums1[i] == nums2[j]) + { + ans = nums1[i]; + --i, --j; + } + else if(nums1[i] > nums2[j]) + --i; + else + --j; + } + + return ans; + } +}; \ No newline at end of file From e20f5669919b6a2c2da97eef64f8da7fcc62c1e9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 10 Mar 2024 09:42:03 +0530 Subject: [PATCH 0776/1399] Create README - LeetHub --- 0349-intersection-of-two-arrays/README.md | 24 +++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 0349-intersection-of-two-arrays/README.md diff --git a/0349-intersection-of-two-arrays/README.md b/0349-intersection-of-two-arrays/README.md new file mode 100644 index 00000000..24bc7284 --- /dev/null +++ b/0349-intersection-of-two-arrays/README.md @@ -0,0 +1,24 @@ +

349. Intersection of Two Arrays

Easy


Given two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must be unique and you may return the result in any order.

+ +

 

+

Example 1:

+ +
Input: nums1 = [1,2,2,1], nums2 = [2,2]
+Output: [2]
+
+ +

Example 2:

+ +
Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
+Output: [9,4]
+Explanation: [4,9] is also accepted.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums1.length, nums2.length <= 1000
  • +
  • 0 <= nums1[i], nums2[i] <= 1000
  • +
+
\ No newline at end of file From 1d7fbe02f78ee8acb1df9a4f49ffd7afd5b2762b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 10 Mar 2024 09:42:06 +0530 Subject: [PATCH 0777/1399] Time: 4 ms (68.61%), Space: 12.9 MB (37.74%) - LeetHub --- .../0349-intersection-of-two-arrays.cpp | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 0349-intersection-of-two-arrays/0349-intersection-of-two-arrays.cpp diff --git a/0349-intersection-of-two-arrays/0349-intersection-of-two-arrays.cpp b/0349-intersection-of-two-arrays/0349-intersection-of-two-arrays.cpp new file mode 100644 index 00000000..c4128d15 --- /dev/null +++ b/0349-intersection-of-two-arrays/0349-intersection-of-two-arrays.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + vector intersection(vector& nums1, vector& nums2) { + + vector res; + + set have(nums1.begin(),nums1.end()); + + for(auto& ele : nums2) + { + if(have.count(ele)) + { + res.push_back(ele); + have.erase(ele); + } + } + + return res; + } +}; \ No newline at end of file From b8ee81d9dfd0d03171abcb7d7ae68d0c3e99bf09 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 10 Mar 2024 09:43:04 +0530 Subject: [PATCH 0778/1399] Attach NOTES - LeetHub --- 0349-intersection-of-two-arrays/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0349-intersection-of-two-arrays/NOTES.md diff --git a/0349-intersection-of-two-arrays/NOTES.md b/0349-intersection-of-two-arrays/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0349-intersection-of-two-arrays/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From b7272d3c3d920033f22d4f6be562c9a4b386c601 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 11 Mar 2024 11:34:58 +0530 Subject: [PATCH 0779/1399] Create README - LeetHub --- 0791-custom-sort-string/README.md | 41 +++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 0791-custom-sort-string/README.md diff --git a/0791-custom-sort-string/README.md b/0791-custom-sort-string/README.md new file mode 100644 index 00000000..df4199fe --- /dev/null +++ b/0791-custom-sort-string/README.md @@ -0,0 +1,41 @@ +

791. Custom Sort String

Medium


You are given two strings order and s. All the characters of order are unique and were sorted in some custom order previously.

+ +

Permute the characters of s so that they match the order that order was sorted. More specifically, if a character x occurs before a character y in order, then x should occur before y in the permuted string.

+ +

Return any permutation of s that satisfies this property.

+ +

 

+

Example 1:

+ +
+

Input: order = "cba", s = "abcd"

+ +

Output: "cbad"

+ +

Explanation: "a", "b", "c" appear in order, so the order of "a", "b", "c" should be "c", "b", and "a".

+ +

Since "d" does not appear in order, it can be at any position in the returned string. "dcba", "cdba", "cbda" are also valid outputs.

+
+ +

Example 2:

+ +
+

Input: order = "bcafg", s = "abcd"

+ +

Output: "bcad"

+ +

Explanation: The characters "b", "c", and "a" from order dictate the order for the characters in s. The character "d" in s does not appear in order, so its position is flexible.

+ +

Following the order of appearance in order, "b", "c", and "a" from s should be arranged as "b", "c", "a". "d" can be placed at any position since it's not in order. The output "bcad" correctly follows this rule. Other arrangements like "bacd" or "bcda" would also be valid, as long as "b", "c", "a" maintain their order.

+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= order.length <= 26
  • +
  • 1 <= s.length <= 200
  • +
  • order and s consist of lowercase English letters.
  • +
  • All the characters of order are unique.
  • +
+
\ No newline at end of file From 7c97038136c8131ca0dd8ddd902eb4744c8919a5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 11 Mar 2024 11:35:02 +0530 Subject: [PATCH 0780/1399] Time: 0 ms (100.00%), Space: 8.6 MB (17.36%) - LeetHub --- .../0791-custom-sort-string.cpp | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 0791-custom-sort-string/0791-custom-sort-string.cpp diff --git a/0791-custom-sort-string/0791-custom-sort-string.cpp b/0791-custom-sort-string/0791-custom-sort-string.cpp new file mode 100644 index 00000000..8d7a7fdb --- /dev/null +++ b/0791-custom-sort-string/0791-custom-sort-string.cpp @@ -0,0 +1,30 @@ +class Solution { +public: + string customSortString(string order, string s) { + + map mp; + + int id = 0; + + for(int i = 0; i < order.size(); ++i) + { + if(mp.find(order[i]) == mp.end()) + mp[order[i]] = id++; + } + + vector> vp; + + for(auto&ch : s) + vp.push_back({mp[ch], ch}); + + sort(vp.begin(), vp.end()); + + string ans; + + for(auto& ch : vp) + ans += ch.second; + + return ans; + + } +}; \ No newline at end of file From 8c94f686c5e3271970dd417148bd0f514aef37c3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 11 Mar 2024 11:35:46 +0530 Subject: [PATCH 0781/1399] Attach NOTES - LeetHub --- 0791-custom-sort-string/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0791-custom-sort-string/NOTES.md diff --git a/0791-custom-sort-string/NOTES.md b/0791-custom-sort-string/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0791-custom-sort-string/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 98d5cf6c4f4d212d16176f6ec8d58a683ff79d94 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 11 Mar 2024 11:35:49 +0530 Subject: [PATCH 0782/1399] Time: 0 ms (100.00%), Space: 8.6 MB (17.36%) - LeetHub From baee2061a9ac9b65315e4ed4a24a6e04532f23bd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 11 Mar 2024 11:36:49 +0530 Subject: [PATCH 0783/1399] Attach NOTES - LeetHub From a689ba380b898c723fbcf8ccdd33b9ac5fee6170 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 11 Mar 2024 11:36:52 +0530 Subject: [PATCH 0784/1399] Time: 0 ms (100.00%), Space: 8.7 MB (17.36%) - LeetHub --- .../0791-custom-sort-string.cpp | 22 +++++-------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/0791-custom-sort-string/0791-custom-sort-string.cpp b/0791-custom-sort-string/0791-custom-sort-string.cpp index 8d7a7fdb..7124d4f8 100644 --- a/0791-custom-sort-string/0791-custom-sort-string.cpp +++ b/0791-custom-sort-string/0791-custom-sort-string.cpp @@ -3,28 +3,18 @@ class Solution { string customSortString(string order, string s) { map mp; - - int id = 0; - + for(int i = 0; i < order.size(); ++i) { if(mp.find(order[i]) == mp.end()) - mp[order[i]] = id++; + mp[order[i]] = i; } - vector> vp; - - for(auto&ch : s) - vp.push_back({mp[ch], ch}); - - sort(vp.begin(), vp.end()); - - string ans; - - for(auto& ch : vp) - ans += ch.second; + sort(s.begin(), s.end(),[&](const auto& a, const auto& b){ + return mp[a] < mp[b]; + }); - return ans; + return s; } }; \ No newline at end of file From 91b482b14e91059d64967f631d85f795de5fb9b0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 12 Mar 2024 23:28:26 +0530 Subject: [PATCH 0785/1399] Create README - LeetHub --- .../README.md | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 1171-remove-zero-sum-consecutive-nodes-from-linked-list/README.md diff --git a/1171-remove-zero-sum-consecutive-nodes-from-linked-list/README.md b/1171-remove-zero-sum-consecutive-nodes-from-linked-list/README.md new file mode 100644 index 00000000..a851823f --- /dev/null +++ b/1171-remove-zero-sum-consecutive-nodes-from-linked-list/README.md @@ -0,0 +1,34 @@ +

1171. Remove Zero Sum Consecutive Nodes from Linked List

Medium


Given the head of a linked list, we repeatedly delete consecutive sequences of nodes that sum to 0 until there are no such sequences.

+ +

After doing so, return the head of the final linked list.  You may return any such answer.

+ +

 

+

(Note that in the examples below, all sequences are serializations of ListNode objects.)

+ +

Example 1:

+ +
Input: head = [1,2,-3,3,1]
+Output: [3,1]
+Note: The answer [1,2,1] would also be accepted.
+
+ +

Example 2:

+ +
Input: head = [1,2,3,-3,4]
+Output: [1,2,4]
+
+ +

Example 3:

+ +
Input: head = [1,2,3,-3,-2]
+Output: [1]
+
+ +

 

+

Constraints:

+ +
    +
  • The given linked list will contain between 1 and 1000 nodes.
  • +
  • Each node in the linked list has -1000 <= node.val <= 1000.
  • +
+
\ No newline at end of file From c8c45cdc3956c0b3d53d02b516341926e58739c0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 12 Mar 2024 23:28:26 +0530 Subject: [PATCH 0786/1399] Attach NOTES - LeetHub --- 1171-remove-zero-sum-consecutive-nodes-from-linked-list/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1171-remove-zero-sum-consecutive-nodes-from-linked-list/NOTES.md diff --git a/1171-remove-zero-sum-consecutive-nodes-from-linked-list/NOTES.md b/1171-remove-zero-sum-consecutive-nodes-from-linked-list/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1171-remove-zero-sum-consecutive-nodes-from-linked-list/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From e3870b343767e5f6c401ea726fee741c828b68ac Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 12 Mar 2024 23:28:29 +0530 Subject: [PATCH 0787/1399] Time: 11 ms (31.41%), Space: 14.4 MB (55.92%) - LeetHub --- ...sum-consecutive-nodes-from-linked-list.cpp | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 1171-remove-zero-sum-consecutive-nodes-from-linked-list/1171-remove-zero-sum-consecutive-nodes-from-linked-list.cpp diff --git a/1171-remove-zero-sum-consecutive-nodes-from-linked-list/1171-remove-zero-sum-consecutive-nodes-from-linked-list.cpp b/1171-remove-zero-sum-consecutive-nodes-from-linked-list/1171-remove-zero-sum-consecutive-nodes-from-linked-list.cpp new file mode 100644 index 00000000..40d9fa70 --- /dev/null +++ b/1171-remove-zero-sum-consecutive-nodes-from-linked-list/1171-remove-zero-sum-consecutive-nodes-from-linked-list.cpp @@ -0,0 +1,41 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + ListNode* removeZeroSumSublists(ListNode* head) { + ListNode *dummynode=new ListNode(0),*curr=dummynode; + dummynode->next=head; + map mp; + int sum=0; + while(curr) + { + sum+=curr->val; + if(mp.count(sum)) + { + curr=mp[sum]->next; + int p=sum+curr->val; + while(p!=sum) + { + mp.erase(p); + curr=curr->next; + p+=curr->val; + } + mp[sum]->next=curr->next; + } + else + { + mp[sum]=curr; + } + curr=curr->next; + } + return dummynode->next; + } +}; \ No newline at end of file From db6c6ef2bb37db1eb1fa59f873f997b62b4e5573 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 13 Mar 2024 07:49:09 +0530 Subject: [PATCH 0788/1399] Create README - LeetHub --- 2485-find-the-pivot-integer/README.md | 37 +++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 2485-find-the-pivot-integer/README.md diff --git a/2485-find-the-pivot-integer/README.md b/2485-find-the-pivot-integer/README.md new file mode 100644 index 00000000..12c9294c --- /dev/null +++ b/2485-find-the-pivot-integer/README.md @@ -0,0 +1,37 @@ +

2485. Find the Pivot Integer

Easy


Given a positive integer n, find the pivot integer x such that:

+ +
    +
  • The sum of all elements between 1 and x inclusively equals the sum of all elements between x and n inclusively.
  • +
+ +

Return the pivot integer x. If no such integer exists, return -1. It is guaranteed that there will be at most one pivot index for the given input.

+ +

 

+

Example 1:

+ +
Input: n = 8
+Output: 6
+Explanation: 6 is the pivot integer since: 1 + 2 + 3 + 4 + 5 + 6 = 6 + 7 + 8 = 21.
+
+ +

Example 2:

+ +
Input: n = 1
+Output: 1
+Explanation: 1 is the pivot integer since: 1 = 1.
+
+ +

Example 3:

+ +
Input: n = 4
+Output: -1
+Explanation: It can be proved that no such integer exist.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= 1000
  • +
+
\ No newline at end of file From 27599f47c83f5c20dca0fbc1ac39332bd84093a4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 13 Mar 2024 07:49:15 +0530 Subject: [PATCH 0789/1399] Time: 0 ms (100.00%), Space: 7.3 MB (15.68%) - LeetHub --- .../2485-find-the-pivot-integer.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 2485-find-the-pivot-integer/2485-find-the-pivot-integer.cpp diff --git a/2485-find-the-pivot-integer/2485-find-the-pivot-integer.cpp b/2485-find-the-pivot-integer/2485-find-the-pivot-integer.cpp new file mode 100644 index 00000000..2c01acd0 --- /dev/null +++ b/2485-find-the-pivot-integer/2485-find-the-pivot-integer.cpp @@ -0,0 +1,18 @@ +class Solution { +public: + int pivotInteger(int n) { + + int tot = (n*(n+1))/2; + + for(int i = 1; i <= n; ++i) + { + int left = (i*(i+1))/2; + int right = tot - left + i; + + if(left == right) + return i; + } + + return -1; + } +}; \ No newline at end of file From 9007d51abe8f2f214e5f233f63f4599c1f98ced3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 13 Mar 2024 07:50:56 +0530 Subject: [PATCH 0790/1399] Attach NOTES - LeetHub --- 2485-find-the-pivot-integer/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2485-find-the-pivot-integer/NOTES.md diff --git a/2485-find-the-pivot-integer/NOTES.md b/2485-find-the-pivot-integer/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2485-find-the-pivot-integer/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 4a7e835107e85a5c1b8f80ae6044430dfc7e9a4c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 13 Mar 2024 07:51:53 +0530 Subject: [PATCH 0791/1399] Attach NOTES - LeetHub From 68b1d56748468ab66bc3012ec97d8d68323fe434 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 13 Mar 2024 07:51:57 +0530 Subject: [PATCH 0792/1399] Time: 0 ms (100.00%), Space: 7.3 MB (15.68%) - LeetHub From 81bf2a5f451825c81991b27ad8c47464d99a5d8f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 13 Mar 2024 07:52:33 +0530 Subject: [PATCH 0793/1399] Attach NOTES - LeetHub From 1d650ff87948c76238fff02221d5b786839c8baf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 13 Mar 2024 07:52:37 +0530 Subject: [PATCH 0794/1399] Time: 0 ms (100.00%), Space: 7.3 MB (15.68%) - LeetHub From 33b538e3c9229b1a3dba6f2582485aad88af6dfb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 14 Mar 2024 12:03:23 +0530 Subject: [PATCH 0795/1399] Create README - LeetHub --- 0930-binary-subarrays-with-sum/README.md | 30 ++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 0930-binary-subarrays-with-sum/README.md diff --git a/0930-binary-subarrays-with-sum/README.md b/0930-binary-subarrays-with-sum/README.md new file mode 100644 index 00000000..63710621 --- /dev/null +++ b/0930-binary-subarrays-with-sum/README.md @@ -0,0 +1,30 @@ +

930. Binary Subarrays With Sum

Medium


Given a binary array nums and an integer goal, return the number of non-empty subarrays with a sum goal.

+ +

A subarray is a contiguous part of the array.

+ +

 

+

Example 1:

+ +
Input: nums = [1,0,1,0,1], goal = 2
+Output: 4
+Explanation: The 4 subarrays are bolded and underlined below:
+[1,0,1,0,1]
+[1,0,1,0,1]
+[1,0,1,0,1]
+[1,0,1,0,1]
+
+ +

Example 2:

+ +
Input: nums = [0,0,0,0,0], goal = 0
+Output: 15
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 3 * 104
  • +
  • nums[i] is either 0 or 1.
  • +
  • 0 <= goal <= nums.length
  • +
\ No newline at end of file From 7f911ab17c9f9fff69277364a012dcf1e463292e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 14 Mar 2024 12:03:23 +0530 Subject: [PATCH 0796/1399] Attach NOTES - LeetHub --- 0930-binary-subarrays-with-sum/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0930-binary-subarrays-with-sum/NOTES.md diff --git a/0930-binary-subarrays-with-sum/NOTES.md b/0930-binary-subarrays-with-sum/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0930-binary-subarrays-with-sum/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 30e8bc8fe02ead4833f09f2f99a7dccc2730385d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 14 Mar 2024 12:03:27 +0530 Subject: [PATCH 0797/1399] Time: 66 ms (20.89%), Space: 37.8 MB (40.56%) - LeetHub --- .../0930-binary-subarrays-with-sum.cpp | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 0930-binary-subarrays-with-sum/0930-binary-subarrays-with-sum.cpp diff --git a/0930-binary-subarrays-with-sum/0930-binary-subarrays-with-sum.cpp b/0930-binary-subarrays-with-sum/0930-binary-subarrays-with-sum.cpp new file mode 100644 index 00000000..d9a7b100 --- /dev/null +++ b/0930-binary-subarrays-with-sum/0930-binary-subarrays-with-sum.cpp @@ -0,0 +1,25 @@ +class Solution { +public: + int numSubarraysWithSum(vector& nums, int goal) { + + int ans = 0, n = nums.size(); + + map mp; + mp.insert({0, 1}); + + int sum = 0; + + for(int j = 0; j < n; ++j) + { + sum += nums[j]; + if(mp.find(sum - goal) != mp.end()) + { + ans += mp[sum - goal]; + } + + ++mp[sum]; + } + + return ans; + } +}; From 96848b366c768f23c4cb3203f7d53a530abe1cdd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 16 Mar 2024 00:04:51 +0530 Subject: [PATCH 0798/1399] Create README - LeetHub --- 0238-product-of-array-except-self/README.md | 26 +++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 0238-product-of-array-except-self/README.md diff --git a/0238-product-of-array-except-self/README.md b/0238-product-of-array-except-self/README.md new file mode 100644 index 00000000..494c527b --- /dev/null +++ b/0238-product-of-array-except-self/README.md @@ -0,0 +1,26 @@ +

238. Product of Array Except Self

Medium


Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i].

+ +

The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.

+ +

You must write an algorithm that runs in O(n) time and without using the division operation.

+ +

 

+

Example 1:

+
Input: nums = [1,2,3,4]
+Output: [24,12,8,6]
+

Example 2:

+
Input: nums = [-1,1,0,-3,3]
+Output: [0,0,9,0,0]
+
+

 

+

Constraints:

+ +
    +
  • 2 <= nums.length <= 105
  • +
  • -30 <= nums[i] <= 30
  • +
  • The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.
  • +
+ +

 

+

Follow up: Can you solve the problem in O(1) extra space complexity? (The output array does not count as extra space for space complexity analysis.)

+
\ No newline at end of file From 1310aaff6e11d8fbc9a3085271a06e123c528db4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 16 Mar 2024 00:04:55 +0530 Subject: [PATCH 0799/1399] Time: 25 ms (6.72%), Space: 26.3 MB (86.12%) - LeetHub --- .../0238-product-of-array-except-self.cpp | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 0238-product-of-array-except-self/0238-product-of-array-except-self.cpp diff --git a/0238-product-of-array-except-self/0238-product-of-array-except-self.cpp b/0238-product-of-array-except-self/0238-product-of-array-except-self.cpp new file mode 100644 index 00000000..5dae18ab --- /dev/null +++ b/0238-product-of-array-except-self/0238-product-of-array-except-self.cpp @@ -0,0 +1,25 @@ +class Solution { +public: + vector productExceptSelf(vector& nums) { + + int n = nums.size(); + vector ans(n, 1); + + int pref = 1, suff = 1; + + for(int i = 0; i < n; ++i) + { + ans[i] *= pref; + pref *= nums[i]; + } + + for(int i = n-1; i >= 0; --i) + { + ans[i] *= suff; + suff *= nums[i]; + } + + return ans; + + } +}; \ No newline at end of file From bab8be5ba8b27c802126f62112147341334a2cd8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 16 Mar 2024 19:54:25 +0530 Subject: [PATCH 0800/1399] Attach NOTES - LeetHub --- 0525-contiguous-array/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0525-contiguous-array/NOTES.md diff --git a/0525-contiguous-array/NOTES.md b/0525-contiguous-array/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0525-contiguous-array/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 6efff499220f434313305cc82fccb995141bcc59 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 16 Mar 2024 19:54:28 +0530 Subject: [PATCH 0801/1399] Time: 114 ms (16.52%), Space: 87 MB (85.88%) - LeetHub --- .../0525-contiguous-array.cpp | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 0525-contiguous-array/0525-contiguous-array.cpp diff --git a/0525-contiguous-array/0525-contiguous-array.cpp b/0525-contiguous-array/0525-contiguous-array.cpp new file mode 100644 index 00000000..1d4d98ec --- /dev/null +++ b/0525-contiguous-array/0525-contiguous-array.cpp @@ -0,0 +1,23 @@ +class Solution { +public: + int findMaxLength(vector& nums) { + + map mp; + + mp.insert({0, -1}); + + int sum = 0, ans = 0, n = nums.size(); + + for(int i = 0; i < n; ++i) + { + sum += (nums[i] == 1 ? 1 : -1); + if(mp.find(sum) != mp.end()) + ans = max(ans, i - mp[sum]); + else + mp[sum] = i; + } + + return ans; + + } +}; \ No newline at end of file From 89af5d6691406caa75b20a1258ca0358f6f9b973 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 17 Mar 2024 23:46:43 +0530 Subject: [PATCH 0802/1399] Attach NOTES - LeetHub --- 0057-insert-interval/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0057-insert-interval/NOTES.md diff --git a/0057-insert-interval/NOTES.md b/0057-insert-interval/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0057-insert-interval/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 4295ba9444365cc0b63477df46374cb7e0b55e82 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 17 Mar 2024 23:48:20 +0530 Subject: [PATCH 0803/1399] Attach NOTES - LeetHub From c12dcda0294e1c5214a95ecfb5bb51769f4f10a7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 17 Mar 2024 23:57:44 +0530 Subject: [PATCH 0804/1399] Attach NOTES - LeetHub From 71b5f6148a168e61b0a1aa662be61e85fe43ee91 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 20 Mar 2024 01:00:39 +0530 Subject: [PATCH 0805/1399] Create README - LeetHub --- 0621-task-scheduler/README.md | 95 +++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 0621-task-scheduler/README.md diff --git a/0621-task-scheduler/README.md b/0621-task-scheduler/README.md new file mode 100644 index 00000000..a449ed32 --- /dev/null +++ b/0621-task-scheduler/README.md @@ -0,0 +1,95 @@ +

621. Task Scheduler

Medium


You are given an array of CPU tasks, each represented by letters A to Z, and a cooling time, n. Each cycle or interval allows the completion of one task. Tasks can be completed in any order, but there's a constraint: identical tasks must be separated by at least n intervals due to cooling time.

+ +

​Return the minimum number of intervals required to complete all tasks.

+ +

 

+

Example 1:

+ +
+

Input: tasks = ["A","A","A","B","B","B"], n = 2

+ +

Output: 8

+ +

Explanation: A possible sequence is: A -> B -> idle -> A -> B -> idle -> A -> B.

+ +

After completing task A, you must wait two cycles before doing A again. The same applies to task B. In the 3rd interval, neither A nor B can be done, so you idle. By the 4th cycle, you can do A again as 2 intervals have passed.

+
+ +

Example 2:

+ +
+

Input: tasks = ["A","C","A","B","D","B"], n = 1

+ +

Output: 6

+ +

Explanation: A possible sequence is: A -> B -> C -> D -> A -> B.

+ +

With a cooling interval of 1, you can repeat a task after just one other task.

+
+ +

Example 3:

+ +
+

Input: tasks = ["A","A","A", "B","B","B"], n = 3

+ +

Output: 10

+ +

Explanation: A possible sequence is: A -> B -> idle -> idle -> A -> B -> idle -> idle -> A -> B.

+ +

There are only two types of tasks, A and B, which need to be separated by 3 intervals. This leads to idling twice between repetitions of these tasks.

+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= tasks.length <= 104
  • +
  • tasks[i] is an uppercase English letter.
  • +
  • 0 <= n <= 100
  • +
+
\ No newline at end of file From dedc85e565b03e42f626ad498500d5ad47017363 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 20 Mar 2024 01:00:42 +0530 Subject: [PATCH 0806/1399] Time: 57 ms (69.92%), Space: 37.8 MB (94.23%) - LeetHub --- 0621-task-scheduler/0621-task-scheduler.cpp | 24 +++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 0621-task-scheduler/0621-task-scheduler.cpp diff --git a/0621-task-scheduler/0621-task-scheduler.cpp b/0621-task-scheduler/0621-task-scheduler.cpp new file mode 100644 index 00000000..1b34b091 --- /dev/null +++ b/0621-task-scheduler/0621-task-scheduler.cpp @@ -0,0 +1,24 @@ +class Solution { +public: + int leastInterval(vector& tasks, int n) { + + mapmp; + int count = 0; + + for(auto ele : tasks) + { + mp[ele]++; + count = max(count, mp[ele]); + } + + int ans = (count-1)*(n+1); + + for(auto e : mp) + { + if(e.second == count) + ans++; + } + + return max((int)tasks.size(), ans); + } +}; \ No newline at end of file From d1c2dc56f1f646cff26cc99df52d9e237cdbaa9a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 20 Mar 2024 23:20:50 +0530 Subject: [PATCH 0807/1399] Create README - LeetHub --- 1669-merge-in-between-linked-lists/README.md | 32 ++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 1669-merge-in-between-linked-lists/README.md diff --git a/1669-merge-in-between-linked-lists/README.md b/1669-merge-in-between-linked-lists/README.md new file mode 100644 index 00000000..80ce1897 --- /dev/null +++ b/1669-merge-in-between-linked-lists/README.md @@ -0,0 +1,32 @@ +

1669. Merge In Between Linked Lists

Medium


You are given two linked lists: list1 and list2 of sizes n and m respectively.

+ +

Remove list1's nodes from the ath node to the bth node, and put list2 in their place.

+ +

The blue edges and nodes in the following figure indicate the result:

+ +

Build the result list and return its head.

+ +

 

+

Example 1:

+ +
Input: list1 = [10,1,13,6,9,5], a = 3, b = 4, list2 = [1000000,1000001,1000002]
+Output: [10,1,13,1000000,1000001,1000002,5]
+Explanation: We remove the nodes 3 and 4 and put the entire list2 in their place. The blue edges and nodes in the above figure indicate the result.
+
+ +

Example 2:

+ +
Input: list1 = [0,1,2,3,4,5,6], a = 2, b = 5, list2 = [1000000,1000001,1000002,1000003,1000004]
+Output: [0,1,1000000,1000001,1000002,1000003,1000004,6]
+Explanation: The blue edges and nodes in the above figure indicate the result.
+
+ +

 

+

Constraints:

+ +
    +
  • 3 <= list1.length <= 104
  • +
  • 1 <= a <= b < list1.length - 1
  • +
  • 1 <= list2.length <= 104
  • +
+
\ No newline at end of file From 09ed672e78c545260328669f1d4cdfc0c2c47c63 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 20 Mar 2024 23:20:54 +0530 Subject: [PATCH 0808/1399] Time: 189 ms (25.82%), Space: 98 MB (44.80%) - LeetHub --- .../1669-merge-in-between-linked-lists.cpp | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 1669-merge-in-between-linked-lists/1669-merge-in-between-linked-lists.cpp diff --git a/1669-merge-in-between-linked-lists/1669-merge-in-between-linked-lists.cpp b/1669-merge-in-between-linked-lists/1669-merge-in-between-linked-lists.cpp new file mode 100644 index 00000000..2da26e6b --- /dev/null +++ b/1669-merge-in-between-linked-lists/1669-merge-in-between-linked-lists.cpp @@ -0,0 +1,46 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + ListNode* mergeInBetween(ListNode* list1, int a, int b, ListNode* list2) { + + ListNode* temp = list1, *temp2 = list2, *prev = temp2; + + while(temp2) + { + prev = temp2; + temp2 = temp2->next; + } + + ListNode* half = nullptr, *nex = nullptr; + + int curr = 0; + + while(temp) + { + ++curr; + if(curr == a) + { + half = temp; + } + if(curr == b+2) + { + nex = temp; + } + temp = temp->next; + } + + half->next = list2; + prev->next = nex; + + return list1; + } +}; \ No newline at end of file From 34765298574d46e4d2cd646339b07988119d0a06 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 21 Mar 2024 12:20:27 +0530 Subject: [PATCH 0809/1399] Attach NOTES - LeetHub --- 0206-reverse-linked-list/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0206-reverse-linked-list/NOTES.md diff --git a/0206-reverse-linked-list/NOTES.md b/0206-reverse-linked-list/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0206-reverse-linked-list/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From d9bbb601bab0a62454a010603fe35ae9f24a5a83 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 21 Mar 2024 12:20:30 +0530 Subject: [PATCH 0810/1399] Time: 8 ms (16.90%), Space: 11.6 MB (67.59%) - LeetHub --- .../0206-reverse-linked-list.cpp | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 0206-reverse-linked-list/0206-reverse-linked-list.cpp diff --git a/0206-reverse-linked-list/0206-reverse-linked-list.cpp b/0206-reverse-linked-list/0206-reverse-linked-list.cpp new file mode 100644 index 00000000..2418f3bc --- /dev/null +++ b/0206-reverse-linked-list/0206-reverse-linked-list.cpp @@ -0,0 +1,27 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + ListNode* reverseList(ListNode* head) { + + ListNode* prev = nullptr; + + while(head) + { + ListNode* nex = head->next; + head->next = prev; + prev = head; + head = nex; + } + + return prev; + } +}; \ No newline at end of file From e78191d150a0efe07a9af8612900df593dff7b28 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 22 Mar 2024 08:38:11 +0530 Subject: [PATCH 0811/1399] Create README - LeetHub --- 0234-palindrome-linked-list/README.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 0234-palindrome-linked-list/README.md diff --git a/0234-palindrome-linked-list/README.md b/0234-palindrome-linked-list/README.md new file mode 100644 index 00000000..d71facb1 --- /dev/null +++ b/0234-palindrome-linked-list/README.md @@ -0,0 +1,25 @@ +

234. Palindrome Linked List

Easy


Given the head of a singly linked list, return true if it is a palindrome or false otherwise.

+ +

 

+

Example 1:

+ +
Input: head = [1,2,2,1]
+Output: true
+
+ +

Example 2:

+ +
Input: head = [1,2]
+Output: false
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the list is in the range [1, 105].
  • +
  • 0 <= Node.val <= 9
  • +
+ +

 

+Follow up: Could you do it in O(n) time and O(1) space?
\ No newline at end of file From 10a5df3e66091203a6b1344578062a257f673b21 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 22 Mar 2024 08:38:15 +0530 Subject: [PATCH 0812/1399] Time: 155 ms (84.20%), Space: 116.5 MB (92.96%) - LeetHub --- .../0234-palindrome-linked-list.cpp | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 0234-palindrome-linked-list/0234-palindrome-linked-list.cpp diff --git a/0234-palindrome-linked-list/0234-palindrome-linked-list.cpp b/0234-palindrome-linked-list/0234-palindrome-linked-list.cpp new file mode 100644 index 00000000..a1d444cd --- /dev/null +++ b/0234-palindrome-linked-list/0234-palindrome-linked-list.cpp @@ -0,0 +1,46 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + bool isPalindrome(ListNode* head) { + + ListNode* fast = head, *slow = head; + + while(fast and fast->next) + { + fast = fast->next->next; + slow = slow->next; + } + + ListNode* nexHalf = slow; + + ListNode* prev = nullptr; + + while(nexHalf) + { + ListNode* nex = nexHalf->next; + nexHalf->next = prev; + prev = nexHalf; + nexHalf = nex; + } + + while(head != slow) + { + if(head->val != prev->val) + return false; + head = head->next; + prev = prev->next; + } + + return true; + + } +}; \ No newline at end of file From 5121b754c8969d9a2939d7e43160845101a027cc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 23 Mar 2024 20:09:33 +0530 Subject: [PATCH 0813/1399] Create README - LeetHub --- 0143-reorder-list/README.md | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 0143-reorder-list/README.md diff --git a/0143-reorder-list/README.md b/0143-reorder-list/README.md new file mode 100644 index 00000000..fa1bf802 --- /dev/null +++ b/0143-reorder-list/README.md @@ -0,0 +1,33 @@ +

143. Reorder List

Medium


You are given the head of a singly linked-list. The list can be represented as:

+ +
L0 → L1 → … → Ln - 1 → Ln
+
+ +

Reorder the list to be on the following form:

+ +
L0 → Ln → L1 → Ln - 1 → L2 → Ln - 2 → …
+
+ +

You may not modify the values in the list's nodes. Only nodes themselves may be changed.

+ +

 

+

Example 1:

+ +
Input: head = [1,2,3,4]
+Output: [1,4,2,3]
+
+ +

Example 2:

+ +
Input: head = [1,2,3,4,5]
+Output: [1,5,2,4,3]
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the list is in the range [1, 5 * 104].
  • +
  • 1 <= Node.val <= 1000
  • +
+
\ No newline at end of file From 06a98a536ad7220454a934bd42c94d008b741892 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 23 Mar 2024 20:09:36 +0530 Subject: [PATCH 0814/1399] Time: 21 ms (81.42%), Space: 21.1 MB (99.47%) - LeetHub --- 0143-reorder-list/0143-reorder-list.cpp | 50 +++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 0143-reorder-list/0143-reorder-list.cpp diff --git a/0143-reorder-list/0143-reorder-list.cpp b/0143-reorder-list/0143-reorder-list.cpp new file mode 100644 index 00000000..1795ab34 --- /dev/null +++ b/0143-reorder-list/0143-reorder-list.cpp @@ -0,0 +1,50 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + void reorderList(ListNode* head) { + + if(!head or !head->next) + return; + + ListNode* slow = head, *fast = head, *first = nullptr; + + while(fast and fast->next) + { + first = slow; + slow = slow->next; + fast = fast->next->next; + } + + first->next = nullptr; + + ListNode* prev = nullptr; + + while(slow) + { + ListNode* nex = slow->next; + slow->next = prev; + prev = slow; + slow = nex; + } + + while(head) + { + ListNode* nex = head->next; + head->next = prev; + ListNode* nex2 = prev->next; + if(nex) + prev->next = nex; + head = nex; + prev = nex2; + } + } +}; \ No newline at end of file From 7b239337eb617bc9b8b44ec727712512ec7f0fe6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 25 Mar 2024 18:19:56 +0530 Subject: [PATCH 0815/1399] Create README - LeetHub --- 3093-longest-common-suffix-queries/README.md | 56 ++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 3093-longest-common-suffix-queries/README.md diff --git a/3093-longest-common-suffix-queries/README.md b/3093-longest-common-suffix-queries/README.md new file mode 100644 index 00000000..14bd5b40 --- /dev/null +++ b/3093-longest-common-suffix-queries/README.md @@ -0,0 +1,56 @@ +

3093. Longest Common Suffix Queries

Hard


You are given two arrays of strings wordsContainer and wordsQuery.

+ +

For each wordsQuery[i], you need to find a string from wordsContainer that has the longest common suffix with wordsQuery[i]. If there are two or more strings in wordsContainer that share the longest common suffix, find the string that is the smallest in length. If there are two or more such strings that have the same smallest length, find the one that occurred earlier in wordsContainer.

+ +

Return an array of integers ans, where ans[i] is the index of the string in wordsContainer that has the longest common suffix with wordsQuery[i].

+ +

 

+

Example 1:

+ +
+

Input: wordsContainer = ["abcd","bcd","xbcd"], wordsQuery = ["cd","bcd","xyz"]

+ +

Output: [1,1,1]

+ +

Explanation:

+ +

Let's look at each wordsQuery[i] separately:

+ +
    +
  • For wordsQuery[0] = "cd", strings from wordsContainer that share the longest common suffix "cd" are at indices 0, 1, and 2. Among these, the answer is the string at index 1 because it has the shortest length of 3.
  • +
  • For wordsQuery[1] = "bcd", strings from wordsContainer that share the longest common suffix "bcd" are at indices 0, 1, and 2. Among these, the answer is the string at index 1 because it has the shortest length of 3.
  • +
  • For wordsQuery[2] = "xyz", there is no string from wordsContainer that shares a common suffix. Hence the longest common suffix is "", that is shared with strings at index 0, 1, and 2. Among these, the answer is the string at index 1 because it has the shortest length of 3.
  • +
+
+ +

Example 2:

+ +
+

Input: wordsContainer = ["abcdefgh","poiuygh","ghghgh"], wordsQuery = ["gh","acbfgh","acbfegh"]

+ +

Output: [2,0,2]

+ +

Explanation:

+ +

Let's look at each wordsQuery[i] separately:

+ +
    +
  • For wordsQuery[0] = "gh", strings from wordsContainer that share the longest common suffix "gh" are at indices 0, 1, and 2. Among these, the answer is the string at index 2 because it has the shortest length of 6.
  • +
  • For wordsQuery[1] = "acbfgh", only the string at index 0 shares the longest common suffix "fgh". Hence it is the answer, even though the string at index 2 is shorter.
  • +
  • For wordsQuery[2] = "acbfegh", strings from wordsContainer that share the longest common suffix "gh" are at indices 0, 1, and 2. Among these, the answer is the string at index 2 because it has the shortest length of 6.
  • +
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= wordsContainer.length, wordsQuery.length <= 104
  • +
  • 1 <= wordsContainer[i].length <= 5 * 103
  • +
  • 1 <= wordsQuery[i].length <= 5 * 103
  • +
  • wordsContainer[i] consists only of lowercase English letters.
  • +
  • wordsQuery[i] consists only of lowercase English letters.
  • +
  • Sum of wordsContainer[i].length is at most 5 * 105.
  • +
  • Sum of wordsQuery[i].length is at most 5 * 105.
  • +
+
\ No newline at end of file From 7085ce84d89858326ac914cabf1247ae98c15e8f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 25 Mar 2024 18:19:59 +0530 Subject: [PATCH 0816/1399] Time: 447 ms (83.33%), Space: 824.3 MB (16.67%) - LeetHub --- .../3093-longest-common-suffix-queries.cpp | 131 ++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 3093-longest-common-suffix-queries/3093-longest-common-suffix-queries.cpp diff --git a/3093-longest-common-suffix-queries/3093-longest-common-suffix-queries.cpp b/3093-longest-common-suffix-queries/3093-longest-common-suffix-queries.cpp new file mode 100644 index 00000000..9e5a32b9 --- /dev/null +++ b/3093-longest-common-suffix-queries/3093-longest-common-suffix-queries.cpp @@ -0,0 +1,131 @@ +class Node{ + public: + Node* child[26] = {nullptr}; + pair p; + bool isWord; + + public: + + Node(){ + p.first = 1e9; + p.second = 1e9; + isWord = false; + } + bool containsKey(char ch) + { + return child[ch-'a'] != nullptr; + } + + void put(char ch, Node* node) + { + child[ch - 'a'] = node; + } + + Node* get(char ch) + { + return child[ch - 'a']; + } + + void setEnd() + { + isWord = true; + } + + void updateMin(int idx, int len) + { + if(len < p.second) + { + p.first = idx; + p.second = len; + } + else if(len == p.second) + p.first = min(p.first, idx); + } + + int getIdx() + { + return p.first; + } +}; + +class Trie{ + private: + Node* root; + + public: + Trie() + { + root = new Node(); + } + + void insert(string& word, int idx) + { + Node* temp = root; + + int n = word.size(); + + for(int i = 0; i < n; ++i) + { + char ch = word[i]; + temp->updateMin(idx, n); + if(!temp->containsKey(ch)) + { + temp->put(ch, new Node()); + + } + temp = temp->get(ch); + } + temp->updateMin(idx, n); + temp->setEnd(); + } + + int search(string& word) + { + Node* temp = root; + int n = word.size(); + int ansIdx = -1; + + for(int i = 0; i < n; ++i) + { + char ch = word[i]; + if(!temp->containsKey(ch)) + { + break; + } + ansIdx= temp->getIdx(); + temp = temp->get(ch); + } + + return temp->getIdx(); + } +}; + +class Solution { +public: + vector stringIndices(vector& wordsContainer, vector& wordsQuery) { + + Trie *trie = new Trie(); + + vector ans; + + int idx = 0, minLength = 1e9;; + for(auto& word : wordsContainer) + { + string rev = word; + reverse(rev.begin(), rev.end()); + minLength = min(minLength, (int)rev.size()); + trie->insert(rev, idx); + ++idx; + } + + for(auto& word : wordsQuery) + { + string rev = word; + reverse(rev.begin(), rev.end()); + int ansIdx = trie->search(rev); + ans.push_back(ansIdx); + } + + return ans; + } +}; \ No newline at end of file From 31c5d7fadb6d6f399fb27a498332605048eef34f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 25 Mar 2024 21:56:38 +0530 Subject: [PATCH 0817/1399] Attach NOTES - LeetHub --- 0442-find-all-duplicates-in-an-array/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0442-find-all-duplicates-in-an-array/NOTES.md diff --git a/0442-find-all-duplicates-in-an-array/NOTES.md b/0442-find-all-duplicates-in-an-array/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0442-find-all-duplicates-in-an-array/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 5dde6b98b695eab8340b11b2b8aeb797ca6d3972 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 26 Mar 2024 23:28:52 +0530 Subject: [PATCH 0818/1399] Create README - LeetHub --- 0041-first-missing-positive/README.md | 34 +++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 0041-first-missing-positive/README.md diff --git a/0041-first-missing-positive/README.md b/0041-first-missing-positive/README.md new file mode 100644 index 00000000..8c35883a --- /dev/null +++ b/0041-first-missing-positive/README.md @@ -0,0 +1,34 @@ +

41. First Missing Positive

Hard


Given an unsorted integer array nums. Return the smallest positive integer that is not present in nums.

+ +

You must implement an algorithm that runs in O(n) time and uses O(1) auxiliary space.

+ +

 

+

Example 1:

+ +
Input: nums = [1,2,0]
+Output: 3
+Explanation: The numbers in the range [1,2] are all in the array.
+
+ +

Example 2:

+ +
Input: nums = [3,4,-1,1]
+Output: 2
+Explanation: 1 is in the array but 2 is missing.
+
+ +

Example 3:

+ +
Input: nums = [7,8,9,11,12]
+Output: 1
+Explanation: The smallest positive integer 1 is missing.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 105
  • +
  • -231 <= nums[i] <= 231 - 1
  • +
+
\ No newline at end of file From 15451b7f96ae037b21b0505570fa5e4e2215e54d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 26 Mar 2024 23:28:55 +0530 Subject: [PATCH 0819/1399] Time: 34 ms (93.31%), Space: 43.5 MB (81.44%) - LeetHub --- .../0041-first-missing-positive.cpp | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 0041-first-missing-positive/0041-first-missing-positive.cpp diff --git a/0041-first-missing-positive/0041-first-missing-positive.cpp b/0041-first-missing-positive/0041-first-missing-positive.cpp new file mode 100644 index 00000000..285acdfb --- /dev/null +++ b/0041-first-missing-positive/0041-first-missing-positive.cpp @@ -0,0 +1,30 @@ +class Solution { +public: + int firstMissingPositive(vector& nums) { + + int n = nums.size(); + + for(int i = 0; i < n; ++i) + { + if(nums[i] <= 0) + nums[i] = n+1; + } + + for(int i = 0; i < n; ++i) + { + if(abs(nums[i])-1 >= 0 and abs(nums[i])-1 < n) + { + if(nums[abs(nums[i])-1] >= 0) + nums[abs(nums[i])-1] = -nums[abs(nums[i])-1]; + } + } + + for(int i = 0; i < n; ++i) + { + if(nums[i] >= 0) + return i+1; + } + + return n+1; + } +}; \ No newline at end of file From 16879b7200c4bf86702e0a5b66bed2b975e08da4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 28 Mar 2024 19:48:41 +0530 Subject: [PATCH 0820/1399] Create README - LeetHub --- .../README.md | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 2958-length-of-longest-subarray-with-at-most-k-frequency/README.md diff --git a/2958-length-of-longest-subarray-with-at-most-k-frequency/README.md b/2958-length-of-longest-subarray-with-at-most-k-frequency/README.md new file mode 100644 index 00000000..768756e8 --- /dev/null +++ b/2958-length-of-longest-subarray-with-at-most-k-frequency/README.md @@ -0,0 +1,44 @@ +

2958. Length of Longest Subarray With at Most K Frequency

Medium


You are given an integer array nums and an integer k.

+ +

The frequency of an element x is the number of times it occurs in an array.

+ +

An array is called good if the frequency of each element in this array is less than or equal to k.

+ +

Return the length of the longest good subarray of nums.

+ +

A subarray is a contiguous non-empty sequence of elements within an array.

+ +

 

+

Example 1:

+ +
Input: nums = [1,2,3,1,2,3,1,2], k = 2
+Output: 6
+Explanation: The longest possible good subarray is [1,2,3,1,2,3] since the values 1, 2, and 3 occur at most twice in this subarray. Note that the subarrays [2,3,1,2,3,1] and [3,1,2,3,1,2] are also good.
+It can be shown that there are no good subarrays with length more than 6.
+
+ +

Example 2:

+ +
Input: nums = [1,2,1,2,1,2,1,2], k = 1
+Output: 2
+Explanation: The longest possible good subarray is [1,2] since the values 1 and 2 occur at most once in this subarray. Note that the subarray [2,1] is also good.
+It can be shown that there are no good subarrays with length more than 2.
+
+ +

Example 3:

+ +
Input: nums = [5,5,5,5,5,5,5], k = 4
+Output: 4
+Explanation: The longest possible good subarray is [5,5,5,5] since the value 5 occurs 4 times in this subarray.
+It can be shown that there are no good subarrays with length more than 4.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 105
  • +
  • 1 <= nums[i] <= 109
  • +
  • 1 <= k <= nums.length
  • +
+
\ No newline at end of file From 5af470c49ee2f4e9351fde4d982fb9edadd4c695 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 28 Mar 2024 19:48:44 +0530 Subject: [PATCH 0821/1399] Time: 461 ms (5.02%), Space: 152 MB (5.02%) - LeetHub --- ...gest-subarray-with-at-most-k-frequency.cpp | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 2958-length-of-longest-subarray-with-at-most-k-frequency/2958-length-of-longest-subarray-with-at-most-k-frequency.cpp diff --git a/2958-length-of-longest-subarray-with-at-most-k-frequency/2958-length-of-longest-subarray-with-at-most-k-frequency.cpp b/2958-length-of-longest-subarray-with-at-most-k-frequency/2958-length-of-longest-subarray-with-at-most-k-frequency.cpp new file mode 100644 index 00000000..cd5a0c16 --- /dev/null +++ b/2958-length-of-longest-subarray-with-at-most-k-frequency/2958-length-of-longest-subarray-with-at-most-k-frequency.cpp @@ -0,0 +1,30 @@ +class Solution { +public: + int maxSubarrayLength(vector& nums, int k) { + + int i = 0, j = 0, n = nums.size(); + + int maxFreq = 0, ans = 0; + + map mp; + + while(j < n) + { + ++mp[nums[j]]; + maxFreq = max(maxFreq, mp[nums[j]]); + + while(mp[nums[j]] > k) + { + --mp[nums[i]]; + if(mp[nums[i]] == 0) + mp.erase(nums[i]); + ++i; + } + + ans = max(ans, j-i+1); + ++j; + } + + return ans; + } +}; \ No newline at end of file From 0f72be57afdaa50056927e035ed89c03b78b9470 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 28 Mar 2024 19:52:50 +0530 Subject: [PATCH 0822/1399] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2958-length-of-longest-subarray-with-at-most-k-frequency/NOTES.md diff --git a/2958-length-of-longest-subarray-with-at-most-k-frequency/NOTES.md b/2958-length-of-longest-subarray-with-at-most-k-frequency/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2958-length-of-longest-subarray-with-at-most-k-frequency/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 3a08f837c278139cca19a1bdd4fd6b90048e28d9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 28 Mar 2024 19:52:52 +0530 Subject: [PATCH 0823/1399] Time: 406 ms (6.33%), Space: 152.2 MB (5.02%) - LeetHub --- ...958-length-of-longest-subarray-with-at-most-k-frequency.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/2958-length-of-longest-subarray-with-at-most-k-frequency/2958-length-of-longest-subarray-with-at-most-k-frequency.cpp b/2958-length-of-longest-subarray-with-at-most-k-frequency/2958-length-of-longest-subarray-with-at-most-k-frequency.cpp index cd5a0c16..46200095 100644 --- a/2958-length-of-longest-subarray-with-at-most-k-frequency/2958-length-of-longest-subarray-with-at-most-k-frequency.cpp +++ b/2958-length-of-longest-subarray-with-at-most-k-frequency/2958-length-of-longest-subarray-with-at-most-k-frequency.cpp @@ -4,14 +4,13 @@ class Solution { int i = 0, j = 0, n = nums.size(); - int maxFreq = 0, ans = 0; + int ans = 0; map mp; while(j < n) { ++mp[nums[j]]; - maxFreq = max(maxFreq, mp[nums[j]]); while(mp[nums[j]] > k) { From 15c4697bd71c064d1ee69bc1accdd97c09eac7c9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 28 Mar 2024 19:53:16 +0530 Subject: [PATCH 0824/1399] Attach NOTES - LeetHub From 18c8c4c5cea0c5da57d5853aab69b559868e95d5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 28 Mar 2024 19:54:09 +0530 Subject: [PATCH 0825/1399] Attach NOTES - LeetHub From 85d71d33d7d9aa21a1b1d05d3f8ce3caf4f22422 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 28 Mar 2024 19:54:12 +0530 Subject: [PATCH 0826/1399] Time: 406 ms (6.33%), Space: 152.2 MB (5.02%) - LeetHub From 789fa391b8b8b175f3948a9ceda4a30dbc74fb6d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 29 Mar 2024 12:15:35 +0530 Subject: [PATCH 0827/1399] Create README - LeetHub --- .../README.md | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 2962-count-subarrays-where-max-element-appears-at-least-k-times/README.md diff --git a/2962-count-subarrays-where-max-element-appears-at-least-k-times/README.md b/2962-count-subarrays-where-max-element-appears-at-least-k-times/README.md new file mode 100644 index 00000000..499c8675 --- /dev/null +++ b/2962-count-subarrays-where-max-element-appears-at-least-k-times/README.md @@ -0,0 +1,30 @@ +

2962. Count Subarrays Where Max Element Appears at Least K Times

Medium


You are given an integer array nums and a positive integer k.

+ +

Return the number of subarrays where the maximum element of nums appears at least k times in that subarray.

+ +

A subarray is a contiguous sequence of elements within an array.

+ +

 

+

Example 1:

+ +
Input: nums = [1,3,2,3,3], k = 2
+Output: 6
+Explanation: The subarrays that contain the element 3 at least 2 times are: [1,3,2,3], [1,3,2,3,3], [3,2,3], [3,2,3,3], [2,3,3] and [3,3].
+
+ +

Example 2:

+ +
Input: nums = [1,4,2,1], k = 3
+Output: 0
+Explanation: No subarray contains the element 4 at least 3 times.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 105
  • +
  • 1 <= nums[i] <= 106
  • +
  • 1 <= k <= 105
  • +
+
\ No newline at end of file From d67e8acd2da7e38347437265273c3be0bbdc902e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 29 Mar 2024 12:15:36 +0530 Subject: [PATCH 0828/1399] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2962-count-subarrays-where-max-element-appears-at-least-k-times/NOTES.md diff --git a/2962-count-subarrays-where-max-element-appears-at-least-k-times/NOTES.md b/2962-count-subarrays-where-max-element-appears-at-least-k-times/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2962-count-subarrays-where-max-element-appears-at-least-k-times/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 0522897b6fd91f36d29e5659ab65754533b5bb83 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 29 Mar 2024 12:15:40 +0530 Subject: [PATCH 0829/1399] Time: 134 ms (55.43%), Space: 120 MB (84.30%) - LeetHub --- ...e-max-element-appears-at-least-k-times.cpp | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 2962-count-subarrays-where-max-element-appears-at-least-k-times/2962-count-subarrays-where-max-element-appears-at-least-k-times.cpp diff --git a/2962-count-subarrays-where-max-element-appears-at-least-k-times/2962-count-subarrays-where-max-element-appears-at-least-k-times.cpp b/2962-count-subarrays-where-max-element-appears-at-least-k-times/2962-count-subarrays-where-max-element-appears-at-least-k-times.cpp new file mode 100644 index 00000000..f4926b3b --- /dev/null +++ b/2962-count-subarrays-where-max-element-appears-at-least-k-times/2962-count-subarrays-where-max-element-appears-at-least-k-times.cpp @@ -0,0 +1,28 @@ +class Solution { +public: + long long countSubarrays(vector& nums, int k) { + + int maxElement = *max_element(nums.begin(), nums.end()); + + int i = 0, j = 0, n = nums.size(); + + long long cnt = 0, ans = 0; + + while(j < n) + { + if(nums[j] == maxElement) + ++cnt; + + while(cnt >= k) + { + ans += (n-j); + if(nums[i] == maxElement) + --cnt; + ++i; + } + ++j; + } + + return ans; + } +}; \ No newline at end of file From 6639902b63122663b42b19f8be1dd4f4527b982f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 30 Mar 2024 08:59:41 +0530 Subject: [PATCH 0830/1399] Create README - LeetHub --- .../README.md | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 0992-subarrays-with-k-different-integers/README.md diff --git a/0992-subarrays-with-k-different-integers/README.md b/0992-subarrays-with-k-different-integers/README.md new file mode 100644 index 00000000..cde878b8 --- /dev/null +++ b/0992-subarrays-with-k-different-integers/README.md @@ -0,0 +1,33 @@ +

992. Subarrays with K Different Integers

Hard


Given an integer array nums and an integer k, return the number of good subarrays of nums.

+ +

A good array is an array where the number of different integers in that array is exactly k.

+ +
    +
  • For example, [1,2,3,1,2] has 3 different integers: 1, 2, and 3.
  • +
+ +

A subarray is a contiguous part of an array.

+ +

 

+

Example 1:

+ +
Input: nums = [1,2,1,2,3], k = 2
+Output: 7
+Explanation: Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2]
+
+ +

Example 2:

+ +
Input: nums = [1,2,1,3,4], k = 3
+Output: 3
+Explanation: Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 2 * 104
  • +
  • 1 <= nums[i], k <= nums.length
  • +
+
\ No newline at end of file From 6a51a3fca00c59d113f7f771f14683c98b591baa Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 31 Mar 2024 22:31:18 +0530 Subject: [PATCH 0831/1399] Create README - LeetHub --- .../README.md | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 2444-count-subarrays-with-fixed-bounds/README.md diff --git a/2444-count-subarrays-with-fixed-bounds/README.md b/2444-count-subarrays-with-fixed-bounds/README.md new file mode 100644 index 00000000..ab1ef885 --- /dev/null +++ b/2444-count-subarrays-with-fixed-bounds/README.md @@ -0,0 +1,36 @@ +

2444. Count Subarrays With Fixed Bounds

Hard


You are given an integer array nums and two integers minK and maxK.

+ +

A fixed-bound subarray of nums is a subarray that satisfies the following conditions:

+ +
    +
  • The minimum value in the subarray is equal to minK.
  • +
  • The maximum value in the subarray is equal to maxK.
  • +
+ +

Return the number of fixed-bound subarrays.

+ +

A subarray is a contiguous part of an array.

+ +

 

+

Example 1:

+ +
Input: nums = [1,3,5,2,7,5], minK = 1, maxK = 5
+Output: 2
+Explanation: The fixed-bound subarrays are [1,3,5] and [1,3,5,2].
+
+ +

Example 2:

+ +
Input: nums = [1,1,1,1], minK = 1, maxK = 1
+Output: 10
+Explanation: Every subarray of nums is a fixed-bound subarray. There are 10 possible subarrays.
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= nums.length <= 105
  • +
  • 1 <= nums[i], minK, maxK <= 106
  • +
+
\ No newline at end of file From 874859d184e2b43088e827191c2597528fe880ab Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 1 Apr 2024 08:23:37 +0530 Subject: [PATCH 0832/1399] Create README - LeetHub --- 0058-length-of-last-word/README.md | 35 ++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 0058-length-of-last-word/README.md diff --git a/0058-length-of-last-word/README.md b/0058-length-of-last-word/README.md new file mode 100644 index 00000000..f5d2873a --- /dev/null +++ b/0058-length-of-last-word/README.md @@ -0,0 +1,35 @@ +

58. Length of Last Word

Easy


Given a string s consisting of words and spaces, return the length of the last word in the string.

+ +

A word is a maximal substring consisting of non-space characters only.

+ +

 

+

Example 1:

+ +
Input: s = "Hello World"
+Output: 5
+Explanation: The last word is "World" with length 5.
+
+ +

Example 2:

+ +
Input: s = "   fly me   to   the moon  "
+Output: 4
+Explanation: The last word is "moon" with length 4.
+
+ +

Example 3:

+ +
Input: s = "luffy is still joyboy"
+Output: 6
+Explanation: The last word is "joyboy" with length 6.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 104
  • +
  • s consists of only English letters and spaces ' '.
  • +
  • There will be at least one word in s.
  • +
+
\ No newline at end of file From 71c24cfe95dfd4208156e297676e634aa74fbd30 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 1 Apr 2024 08:23:40 +0530 Subject: [PATCH 0833/1399] Time: 0 ms (100.00%), Space: 7.6 MB (87.89%) - LeetHub --- .../0058-length-of-last-word.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 0058-length-of-last-word/0058-length-of-last-word.cpp diff --git a/0058-length-of-last-word/0058-length-of-last-word.cpp b/0058-length-of-last-word/0058-length-of-last-word.cpp new file mode 100644 index 00000000..16428111 --- /dev/null +++ b/0058-length-of-last-word/0058-length-of-last-word.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + int lengthOfLastWord(string s) { + + int i = s.size()-1; + int len = 0; + + while(i >= 0 and s[i] == ' ') + --i; + + while(i >= 0 and s[i] != ' ') + ++len, --i; + + return len; + + } +}; \ No newline at end of file From ba0168718b5bc9968ee54caafe342e2eb7de4f67 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 1 Apr 2024 08:24:54 +0530 Subject: [PATCH 0834/1399] Attach NOTES - LeetHub --- 0058-length-of-last-word/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0058-length-of-last-word/NOTES.md diff --git a/0058-length-of-last-word/NOTES.md b/0058-length-of-last-word/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0058-length-of-last-word/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 704c572f4a1d265fbffa45b3cb9f993103bdd663 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 1 Apr 2024 08:24:57 +0530 Subject: [PATCH 0835/1399] Time: 0 ms (100.00%), Space: 7.6 MB (87.89%) - LeetHub From 58441577c192bedcf302bace1466b396210c6211 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 2 Apr 2024 10:06:06 +0530 Subject: [PATCH 0836/1399] Create README - LeetHub --- 0205-isomorphic-strings/README.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 0205-isomorphic-strings/README.md diff --git a/0205-isomorphic-strings/README.md b/0205-isomorphic-strings/README.md new file mode 100644 index 00000000..c7d544ca --- /dev/null +++ b/0205-isomorphic-strings/README.md @@ -0,0 +1,26 @@ +

205. Isomorphic Strings

Easy


Given two strings s and t, determine if they are isomorphic.

+ +

Two strings s and t are isomorphic if the characters in s can be replaced to get t.

+ +

All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.

+ +

 

+

Example 1:

+
Input: s = "egg", t = "add"
+Output: true
+

Example 2:

+
Input: s = "foo", t = "bar"
+Output: false
+

Example 3:

+
Input: s = "paper", t = "title"
+Output: true
+
+

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 5 * 104
  • +
  • t.length == s.length
  • +
  • s and t consist of any valid ascii character.
  • +
+
\ No newline at end of file From a54d9aaf7a4ab49b9fc73d52986420bf7a1c50ab Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 3 Apr 2024 22:56:55 +0530 Subject: [PATCH 0837/1399] Create README - LeetHub --- 0079-word-search/README.md | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 0079-word-search/README.md diff --git a/0079-word-search/README.md b/0079-word-search/README.md new file mode 100644 index 00000000..1480ac27 --- /dev/null +++ b/0079-word-search/README.md @@ -0,0 +1,37 @@ +

79. Word Search

Medium


Given an m x n grid of characters board and a string word, return true if word exists in the grid.

+ +

The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically neighboring. The same letter cell may not be used more than once.

+ +

 

+

Example 1:

+ +
Input: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"
+Output: true
+
+ +

Example 2:

+ +
Input: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "SEE"
+Output: true
+
+ +

Example 3:

+ +
Input: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCB"
+Output: false
+
+ +

 

+

Constraints:

+ +
    +
  • m == board.length
  • +
  • n = board[i].length
  • +
  • 1 <= m, n <= 6
  • +
  • 1 <= word.length <= 15
  • +
  • board and word consists of only lowercase and uppercase English letters.
  • +
+ +

 

+

Follow up: Could you use search pruning to make your solution faster with a larger board?

+
\ No newline at end of file From 0115b002abccc1dcd51881b042a05d2901298097 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 4 Apr 2024 19:31:37 +0530 Subject: [PATCH 0838/1399] Create README - LeetHub --- .../README.md | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 1614-maximum-nesting-depth-of-the-parentheses/README.md diff --git a/1614-maximum-nesting-depth-of-the-parentheses/README.md b/1614-maximum-nesting-depth-of-the-parentheses/README.md new file mode 100644 index 00000000..b636dfcb --- /dev/null +++ b/1614-maximum-nesting-depth-of-the-parentheses/README.md @@ -0,0 +1,44 @@ +

1614. Maximum Nesting Depth of the Parentheses

Easy


A string is a valid parentheses string (denoted VPS) if it meets one of the following:

+ +
    +
  • It is an empty string "", or a single character not equal to "(" or ")",
  • +
  • It can be written as AB (A concatenated with B), where A and B are VPS's, or
  • +
  • It can be written as (A), where A is a VPS.
  • +
+ +

We can similarly define the nesting depth depth(S) of any VPS S as follows:

+ +
    +
  • depth("") = 0
  • +
  • depth(C) = 0, where C is a string with a single character not equal to "(" or ")".
  • +
  • depth(A + B) = max(depth(A), depth(B)), where A and B are VPS's.
  • +
  • depth("(" + A + ")") = 1 + depth(A), where A is a VPS.
  • +
+ +

For example, "", "()()", and "()(()())" are VPS's (with nesting depths 0, 1, and 2), and ")(" and "(()" are not VPS's.

+ +

Given a VPS represented as string s, return the nesting depth of s.

+ +

 

+

Example 1:

+ +
Input: s = "(1+(2*3)+((8)/4))+1"
+Output: 3
+Explanation: Digit 8 is inside of 3 nested parentheses in the string.
+
+ +

Example 2:

+ +
Input: s = "(1)+((2))+(((3)))"
+Output: 3
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 100
  • +
  • s consists of digits 0-9 and characters '+', '-', '*', '/', '(', and ')'.
  • +
  • It is guaranteed that parentheses expression s is a VPS.
  • +
+
\ No newline at end of file From d21645bdaa081717fd29d069de5f9badeb2771f1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 4 Apr 2024 19:31:41 +0530 Subject: [PATCH 0839/1399] Time: 0 ms (100.00%), Space: 7.5 MB (22.85%) - LeetHub --- ...aximum-nesting-depth-of-the-parentheses.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 1614-maximum-nesting-depth-of-the-parentheses/1614-maximum-nesting-depth-of-the-parentheses.cpp diff --git a/1614-maximum-nesting-depth-of-the-parentheses/1614-maximum-nesting-depth-of-the-parentheses.cpp b/1614-maximum-nesting-depth-of-the-parentheses/1614-maximum-nesting-depth-of-the-parentheses.cpp new file mode 100644 index 00000000..9cc248b4 --- /dev/null +++ b/1614-maximum-nesting-depth-of-the-parentheses/1614-maximum-nesting-depth-of-the-parentheses.cpp @@ -0,0 +1,18 @@ +class Solution { +public: + int maxDepth(string s) { + + int open = 0, ans = 0; + + for(auto& ch : s) + { + if(ch == '(') + ++open; + else if(ch == ')') + --open; + ans = max(ans, open); + } + + return ans; + } +}; \ No newline at end of file From 2610c60a13460d4153a8454adc7b92ed1513f20c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 4 Apr 2024 19:32:16 +0530 Subject: [PATCH 0840/1399] Attach NOTES - LeetHub --- 1614-maximum-nesting-depth-of-the-parentheses/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1614-maximum-nesting-depth-of-the-parentheses/NOTES.md diff --git a/1614-maximum-nesting-depth-of-the-parentheses/NOTES.md b/1614-maximum-nesting-depth-of-the-parentheses/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1614-maximum-nesting-depth-of-the-parentheses/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 9942a4081abf354e109e79caf2bcd6a01138d392 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 4 Apr 2024 19:32:19 +0530 Subject: [PATCH 0841/1399] Time: 0 ms (100.00%), Space: 7.5 MB (22.85%) - LeetHub From ea55707fc31040eab690556f714d3f69b915311f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 5 Apr 2024 22:18:38 +0530 Subject: [PATCH 0842/1399] Create README - LeetHub --- 1544-make-the-string-great/README.md | 46 ++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 1544-make-the-string-great/README.md diff --git a/1544-make-the-string-great/README.md b/1544-make-the-string-great/README.md new file mode 100644 index 00000000..4ce6592e --- /dev/null +++ b/1544-make-the-string-great/README.md @@ -0,0 +1,46 @@ +

1544. Make The String Great

Easy


Given a string s of lower and upper case English letters.

+ +

A good string is a string which doesn't have two adjacent characters s[i] and s[i + 1] where:

+ +
    +
  • 0 <= i <= s.length - 2
  • +
  • s[i] is a lower-case letter and s[i + 1] is the same letter but in upper-case or vice-versa.
  • +
+ +

To make the string good, you can choose two adjacent characters that make the string bad and remove them. You can keep doing this until the string becomes good.

+ +

Return the string after making it good. The answer is guaranteed to be unique under the given constraints.

+ +

Notice that an empty string is also good.

+ +

 

+

Example 1:

+ +
Input: s = "leEeetcode"
+Output: "leetcode"
+Explanation: In the first step, either you choose i = 1 or i = 2, both will result "leEeetcode" to be reduced to "leetcode".
+
+ +

Example 2:

+ +
Input: s = "abBAcC"
+Output: ""
+Explanation: We have many possible scenarios, and all lead to the same answer. For example:
+"abBAcC" --> "aAcC" --> "cC" --> ""
+"abBAcC" --> "abBA" --> "aA" --> ""
+
+ +

Example 3:

+ +
Input: s = "s"
+Output: "s"
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 100
  • +
  • s contains only lower and upper case English letters.
  • +
+
\ No newline at end of file From 2cca864da4df889fc3290748edd6c32979e7a25d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 5 Apr 2024 22:18:39 +0530 Subject: [PATCH 0843/1399] Attach NOTES - LeetHub --- 1544-make-the-string-great/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1544-make-the-string-great/NOTES.md diff --git a/1544-make-the-string-great/NOTES.md b/1544-make-the-string-great/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1544-make-the-string-great/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 655b78b9a8ca18a583ef312bc5c4feb543cd7ebc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 5 Apr 2024 22:18:42 +0530 Subject: [PATCH 0844/1399] Time: 4 ms (42.41%), Space: 7.4 MB (98.61%) - LeetHub --- .../1544-make-the-string-great.cpp | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 1544-make-the-string-great/1544-make-the-string-great.cpp diff --git a/1544-make-the-string-great/1544-make-the-string-great.cpp b/1544-make-the-string-great/1544-make-the-string-great.cpp new file mode 100644 index 00000000..4c9d7994 --- /dev/null +++ b/1544-make-the-string-great/1544-make-the-string-great.cpp @@ -0,0 +1,28 @@ +class Solution { +public: + + string makeGood(string s) { + + string ans; + + ans += s[0]; + + int n = s.size(); + + for(int i = 1; i < n; ++i) + { + if(!ans.empty() and tolower(ans.back()) == tolower(s[i])) + { + if(s[i] != ans.back()) + ans.pop_back(); + else + ans += s[i]; + } + else + ans += s[i]; + } + + return ans; + + } +}; \ No newline at end of file From d8937c7e23a747f855774a7239953a9f3851967b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 6 Apr 2024 20:13:38 +0530 Subject: [PATCH 0845/1399] Create README - LeetHub --- .../README.md | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 1249-minimum-remove-to-make-valid-parentheses/README.md diff --git a/1249-minimum-remove-to-make-valid-parentheses/README.md b/1249-minimum-remove-to-make-valid-parentheses/README.md new file mode 100644 index 00000000..d5ec3542 --- /dev/null +++ b/1249-minimum-remove-to-make-valid-parentheses/README.md @@ -0,0 +1,41 @@ +

1249. Minimum Remove to Make Valid Parentheses

Medium


Given a string s of '(' , ')' and lowercase English characters.

+ +

Your task is to remove the minimum number of parentheses ( '(' or ')', in any positions ) so that the resulting parentheses string is valid and return any valid string.

+ +

Formally, a parentheses string is valid if and only if:

+ +
    +
  • It is the empty string, contains only lowercase characters, or
  • +
  • It can be written as AB (A concatenated with B), where A and B are valid strings, or
  • +
  • It can be written as (A), where A is a valid string.
  • +
+ +

 

+

Example 1:

+ +
Input: s = "lee(t(c)o)de)"
+Output: "lee(t(c)o)de"
+Explanation: "lee(t(co)de)" , "lee(t(c)ode)" would also be accepted.
+
+ +

Example 2:

+ +
Input: s = "a)b(c)d"
+Output: "ab(c)d"
+
+ +

Example 3:

+ +
Input: s = "))(("
+Output: ""
+Explanation: An empty string is also valid.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 105
  • +
  • s[i] is either'(' , ')', or lowercase English letter.
  • +
+
\ No newline at end of file From 17d553e2919ea9976eef6304ea77b22936b4d211 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 6 Apr 2024 20:13:42 +0530 Subject: [PATCH 0846/1399] Time: 27 ms (22.35%), Space: 13.2 MB (44.22%) - LeetHub --- ...nimum-remove-to-make-valid-parentheses.cpp | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 1249-minimum-remove-to-make-valid-parentheses/1249-minimum-remove-to-make-valid-parentheses.cpp diff --git a/1249-minimum-remove-to-make-valid-parentheses/1249-minimum-remove-to-make-valid-parentheses.cpp b/1249-minimum-remove-to-make-valid-parentheses/1249-minimum-remove-to-make-valid-parentheses.cpp new file mode 100644 index 00000000..c1a6bc24 --- /dev/null +++ b/1249-minimum-remove-to-make-valid-parentheses/1249-minimum-remove-to-make-valid-parentheses.cpp @@ -0,0 +1,32 @@ +class Solution { +public: + string minRemoveToMakeValid(string s) { + + int n = s.size(); + + stack st; + + for(int i = 0; i < n; ++i) + { + if(!st.empty() and s[st.top()] == '(' and s[i] == ')') + st.pop(); + else if(s[i] == '(' or s[i] == ')') + st.push(i); + } + + while(!st.empty()) + { + s[st.top()] = '#'; + st.pop(); + } + + string ans; + for(int i = 0; i < n; ++i) + { + if(s[i] != '#') + ans += s[i]; + } + + return ans; + } +}; \ No newline at end of file From b4d292f7993cd58e7c41246f6349df6a09eff77e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 7 Apr 2024 23:49:45 +0530 Subject: [PATCH 0847/1399] Create README - LeetHub --- 0678-valid-parenthesis-string/README.md | 30 +++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 0678-valid-parenthesis-string/README.md diff --git a/0678-valid-parenthesis-string/README.md b/0678-valid-parenthesis-string/README.md new file mode 100644 index 00000000..26a61d26 --- /dev/null +++ b/0678-valid-parenthesis-string/README.md @@ -0,0 +1,30 @@ +

678. Valid Parenthesis String

Medium


Given a string s containing only three types of characters: '(', ')' and '*', return true if s is valid.

+ +

The following rules define a valid string:

+ +
    +
  • Any left parenthesis '(' must have a corresponding right parenthesis ')'.
  • +
  • Any right parenthesis ')' must have a corresponding left parenthesis '('.
  • +
  • Left parenthesis '(' must go before the corresponding right parenthesis ')'.
  • +
  • '*' could be treated as a single right parenthesis ')' or a single left parenthesis '(' or an empty string "".
  • +
+ +

 

+

Example 1:

+
Input: s = "()"
+Output: true
+

Example 2:

+
Input: s = "(*)"
+Output: true
+

Example 3:

+
Input: s = "(*))"
+Output: true
+
+

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 100
  • +
  • s[i] is '(', ')' or '*'.
  • +
+
\ No newline at end of file From 6709998d3cb8f0ce81a805b30c18b082a29650b7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 7 Apr 2024 23:49:45 +0530 Subject: [PATCH 0848/1399] Attach NOTES - LeetHub --- 0678-valid-parenthesis-string/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0678-valid-parenthesis-string/NOTES.md diff --git a/0678-valid-parenthesis-string/NOTES.md b/0678-valid-parenthesis-string/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0678-valid-parenthesis-string/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From d81e6f746d6b65ee542194e54516875125949f4f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 8 Apr 2024 22:18:43 +0530 Subject: [PATCH 0849/1399] Create README - LeetHub --- .../README.md | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 1700-number-of-students-unable-to-eat-lunch/README.md diff --git a/1700-number-of-students-unable-to-eat-lunch/README.md b/1700-number-of-students-unable-to-eat-lunch/README.md new file mode 100644 index 00000000..6d562160 --- /dev/null +++ b/1700-number-of-students-unable-to-eat-lunch/README.md @@ -0,0 +1,46 @@ +

1700. Number of Students Unable to Eat Lunch

Easy


The school cafeteria offers circular and square sandwiches at lunch break, referred to by numbers 0 and 1 respectively. All students stand in a queue. Each student either prefers square or circular sandwiches.

+ +

The number of sandwiches in the cafeteria is equal to the number of students. The sandwiches are placed in a stack. At each step:

+ +
    +
  • If the student at the front of the queue prefers the sandwich on the top of the stack, they will take it and leave the queue.
  • +
  • Otherwise, they will leave it and go to the queue's end.
  • +
+ +

This continues until none of the queue students want to take the top sandwich and are thus unable to eat.

+ +

You are given two integer arrays students and sandwiches where sandwiches[i] is the type of the i​​​​​​th sandwich in the stack (i = 0 is the top of the stack) and students[j] is the preference of the j​​​​​​th student in the initial queue (j = 0 is the front of the queue). Return the number of students that are unable to eat.

+ +

 

+

Example 1:

+ +
Input: students = [1,1,0,0], sandwiches = [0,1,0,1]
+Output: 0 
+Explanation:
+- Front student leaves the top sandwich and returns to the end of the line making students = [1,0,0,1].
+- Front student leaves the top sandwich and returns to the end of the line making students = [0,0,1,1].
+- Front student takes the top sandwich and leaves the line making students = [0,1,1] and sandwiches = [1,0,1].
+- Front student leaves the top sandwich and returns to the end of the line making students = [1,1,0].
+- Front student takes the top sandwich and leaves the line making students = [1,0] and sandwiches = [0,1].
+- Front student leaves the top sandwich and returns to the end of the line making students = [0,1].
+- Front student takes the top sandwich and leaves the line making students = [1] and sandwiches = [1].
+- Front student takes the top sandwich and leaves the line making students = [] and sandwiches = [].
+Hence all students are able to eat.
+
+ +

Example 2:

+ +
Input: students = [1,1,1,0,0,1], sandwiches = [1,0,0,0,1,1]
+Output: 3
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= students.length, sandwiches.length <= 100
  • +
  • students.length == sandwiches.length
  • +
  • sandwiches[i] is 0 or 1.
  • +
  • students[i] is 0 or 1.
  • +
+
\ No newline at end of file From fa5c75c8c5b80a7c85cf6a87066465e21af83562 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 8 Apr 2024 22:18:48 +0530 Subject: [PATCH 0850/1399] Time: 0 ms (100.00%), Space: 10.8 MB (79.46%) - LeetHub --- ...number-of-students-unable-to-eat-lunch.cpp | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 1700-number-of-students-unable-to-eat-lunch/1700-number-of-students-unable-to-eat-lunch.cpp diff --git a/1700-number-of-students-unable-to-eat-lunch/1700-number-of-students-unable-to-eat-lunch.cpp b/1700-number-of-students-unable-to-eat-lunch/1700-number-of-students-unable-to-eat-lunch.cpp new file mode 100644 index 00000000..07c848a7 --- /dev/null +++ b/1700-number-of-students-unable-to-eat-lunch/1700-number-of-students-unable-to-eat-lunch.cpp @@ -0,0 +1,28 @@ +class Solution { +public: + int countStudents(vector& students, vector& sandwiches) { + + int n = students.size(); + + vector freq(2, 0); + + for(auto& ele : students) + ++freq[ele]; + + int cnt = 0; + + for(auto& ele : sandwiches) + { + if(freq[ele] > 0) + { + ++cnt; + --freq[ele]; + } + else + break; + } + + return n - cnt; + + } +}; \ No newline at end of file From a5aacb79efefa6f452d89311d2abcd185779d921 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 8 Apr 2024 22:19:41 +0530 Subject: [PATCH 0851/1399] Attach NOTES - LeetHub --- 1700-number-of-students-unable-to-eat-lunch/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1700-number-of-students-unable-to-eat-lunch/NOTES.md diff --git a/1700-number-of-students-unable-to-eat-lunch/NOTES.md b/1700-number-of-students-unable-to-eat-lunch/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1700-number-of-students-unable-to-eat-lunch/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 32a6be6d2f58b2159abeed39f25f494566c79bc0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 8 Apr 2024 22:19:44 +0530 Subject: [PATCH 0852/1399] Time: 0 ms (100.00%), Space: 10.8 MB (79.46%) - LeetHub From 6002f743cb2d5af1ba7ad49f86e12575006b9ca7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 8 Apr 2024 23:52:47 +0530 Subject: [PATCH 0853/1399] Create README - LeetHub --- .../README.md | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 3108-minimum-cost-walk-in-weighted-graph/README.md diff --git a/3108-minimum-cost-walk-in-weighted-graph/README.md b/3108-minimum-cost-walk-in-weighted-graph/README.md new file mode 100644 index 00000000..e48a0a2f --- /dev/null +++ b/3108-minimum-cost-walk-in-weighted-graph/README.md @@ -0,0 +1,54 @@ +

3108. Minimum Cost Walk in Weighted Graph

Hard


There is an undirected weighted graph with n vertices labeled from 0 to n - 1.

+ +

You are given the integer n and an array edges, where edges[i] = [ui, vi, wi] indicates that there is an edge between vertices ui and vi with a weight of wi.

+ +

A walk on a graph is a sequence of vertices and edges. The walk starts and ends with a vertex, and each edge connects the vertex that comes before it and the vertex that comes after it. It's important to note that a walk may visit the same edge or vertex more than once.

+ +

The cost of a walk starting at node u and ending at node v is defined as the bitwise AND of the weights of the edges traversed during the walk. In other words, if the sequence of edge weights encountered during the walk is w0, w1, w2, ..., wk, then the cost is calculated as w0 & w1 & w2 & ... & wk, where & denotes the bitwise AND operator.

+ +

You are also given a 2D array query, where query[i] = [si, ti]. For each query, you need to find the minimum cost of the walk starting at vertex si and ending at vertex ti. If there exists no such walk, the answer is -1.

+ +

Return the array answer, where answer[i] denotes the minimum cost of a walk for query i.

+ +

 

+

Example 1:

+ +
+

Input: n = 5, edges = [[0,1,7],[1,3,7],[1,2,1]], query = [[0,3],[3,4]]

+ +

Output: [1,-1]

+ +

Explanation:

+ +

To achieve the cost of 1 in the first query, we need to move on the following edges: 0->1 (weight 7), 1->2 (weight 1), 2->1 (weight 1), 1->3 (weight 7).

+ +

In the second query, there is no walk between nodes 3 and 4, so the answer is -1.

+ +

Example 2:

+
+ +
+

Input: n = 3, edges = [[0,2,7],[0,1,15],[1,2,6],[1,2,1]], query = [[1,2]]

+ +

Output: [0]

+ +

Explanation:

+ +

To achieve the cost of 0 in the first query, we need to move on the following edges: 1->2 (weight 1), 2->1 (weight 6), 1->2 (weight 1).

+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= 105
  • +
  • 0 <= edges.length <= 105
  • +
  • edges[i].length == 3
  • +
  • 0 <= ui, vi <= n - 1
  • +
  • ui != vi
  • +
  • 0 <= wi <= 105
  • +
  • 1 <= query.length <= 105
  • +
  • query[i].length == 2
  • +
  • 0 <= si, ti <= n - 1
  • +
+
\ No newline at end of file From 64fdfcf9602e543a9a3165dc6f668764e66c6390 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 8 Apr 2024 23:52:50 +0530 Subject: [PATCH 0854/1399] Time: 322 ms (77.78%), Space: 151.8 MB (77.78%) - LeetHub --- ...08-minimum-cost-walk-in-weighted-graph.cpp | 112 ++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 3108-minimum-cost-walk-in-weighted-graph/3108-minimum-cost-walk-in-weighted-graph.cpp diff --git a/3108-minimum-cost-walk-in-weighted-graph/3108-minimum-cost-walk-in-weighted-graph.cpp b/3108-minimum-cost-walk-in-weighted-graph/3108-minimum-cost-walk-in-weighted-graph.cpp new file mode 100644 index 00000000..193b48fc --- /dev/null +++ b/3108-minimum-cost-walk-in-weighted-graph/3108-minimum-cost-walk-in-weighted-graph.cpp @@ -0,0 +1,112 @@ +class DSU{ + public: + int N; + vector parent, size; + + DSU(int n) + { + N = n; + parent.resize(N); + size.resize(N, 1); + + for(int i = 0; i < n; ++i) + parent[i] = i; + } + + int findParent(int u) + { + if(u == parent[u]) + { + return u; + } + return parent[u] = findParent(parent[u]); + } + + void unionBySize(int u, int v) + { + int parU = findParent(u); + int parV = findParent(v); + + if(parU == parV) + return; + + if(size[parU] < size[parV]) + { + parent[parU] = parV; + size[parV] += size[parU]; + } + else + { + parent[parV] = parU; + size[parU] += size[parV]; + } + } + + bool isSame(int u, int v) + { + return findParent(u) == findParent(v); + } +}; + +class Solution { +public: + + vector minimumCost(int n, vector>& edges, vector>& query) { + + vector ans; + + DSU dsu(n+1); + + vector bitAnd(n, -1); + + for(auto& edge : edges) + { + int u = edge[0]; + int v = edge[1]; + int wt = edge[2]; + + if(bitAnd[u] == -1) + bitAnd[u] = wt; + else + bitAnd[u] &= wt; + + if(bitAnd[v] == -1) + bitAnd[v] = wt; + else + bitAnd[v] &= wt; + + if(!dsu.isSame(u, v)) + { + dsu.unionBySize(u, v); + } + } + + map mp; + + for(int i = 0; i < n; ++i) + { + int ultPar = dsu.findParent(i); + if(bitAnd[ultPar] == -1) + continue; + if(mp.find(ultPar) == mp.end()) + mp[ultPar] = (bitAnd[i] & bitAnd[ultPar]); + else + mp[ultPar] &= bitAnd[i]; + } + + for(auto& q : query) + { + int u = q[0]; + int v = q[1]; + + if(u == v) + ans.push_back(0); + else if(dsu.isSame(u, v)) + ans.push_back(mp[dsu.findParent(u)]); + else + ans.push_back(-1); + } + + return ans; + } +}; \ No newline at end of file From 810f698f8dbde18de1de6318681a9c0ef8e9a249 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 9 Apr 2024 23:00:23 +0530 Subject: [PATCH 0855/1399] Attach NOTES - LeetHub --- 2073-time-needed-to-buy-tickets/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2073-time-needed-to-buy-tickets/NOTES.md diff --git a/2073-time-needed-to-buy-tickets/NOTES.md b/2073-time-needed-to-buy-tickets/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2073-time-needed-to-buy-tickets/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From c26a83aa006e53e6e2174ae6c6b5bbfca70f8a08 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 9 Apr 2024 23:00:25 +0530 Subject: [PATCH 0856/1399] Create README - LeetHub --- 2073-time-needed-to-buy-tickets/README.md | 39 +++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 2073-time-needed-to-buy-tickets/README.md diff --git a/2073-time-needed-to-buy-tickets/README.md b/2073-time-needed-to-buy-tickets/README.md new file mode 100644 index 00000000..5c998ffc --- /dev/null +++ b/2073-time-needed-to-buy-tickets/README.md @@ -0,0 +1,39 @@ +

2073. Time Needed to Buy Tickets

Easy


There are n people in a line queuing to buy tickets, where the 0th person is at the front of the line and the (n - 1)th person is at the back of the line.

+ +

You are given a 0-indexed integer array tickets of length n where the number of tickets that the ith person would like to buy is tickets[i].

+ +

Each person takes exactly 1 second to buy a ticket. A person can only buy 1 ticket at a time and has to go back to the end of the line (which happens instantaneously) in order to buy more tickets. If a person does not have any tickets left to buy, the person will leave the line.

+ +

Return the time taken for the person at position k (0-indexed) to finish buying tickets.

+ +

 

+

Example 1:

+ +
Input: tickets = [2,3,2], k = 2
+Output: 6
+Explanation: 
+- In the first pass, everyone in the line buys a ticket and the line becomes [1, 2, 1].
+- In the second pass, everyone in the line buys a ticket and the line becomes [0, 1, 0].
+The person at position 2 has successfully bought 2 tickets and it took 3 + 3 = 6 seconds.
+
+ +

Example 2:

+ +
Input: tickets = [5,1,1,1], k = 0
+Output: 8
+Explanation:
+- In the first pass, everyone in the line buys a ticket and the line becomes [4, 0, 0, 0].
+- In the next 4 passes, only the person in position 0 is buying tickets.
+The person at position 0 has successfully bought 5 tickets and it took 4 + 1 + 1 + 1 + 1 = 8 seconds.
+
+ +

 

+

Constraints:

+ +
    +
  • n == tickets.length
  • +
  • 1 <= n <= 100
  • +
  • 1 <= tickets[i] <= 100
  • +
  • 0 <= k < n
  • +
+
\ No newline at end of file From b34befda1d709b32b0a878c8adc75e300011610a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 9 Apr 2024 23:00:41 +0530 Subject: [PATCH 0857/1399] Time: 5 ms (22.66%), Space: 9.5 MB (46.11%) - LeetHub --- .../2073-time-needed-to-buy-tickets.cpp | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 2073-time-needed-to-buy-tickets/2073-time-needed-to-buy-tickets.cpp diff --git a/2073-time-needed-to-buy-tickets/2073-time-needed-to-buy-tickets.cpp b/2073-time-needed-to-buy-tickets/2073-time-needed-to-buy-tickets.cpp new file mode 100644 index 00000000..3f1fb25d --- /dev/null +++ b/2073-time-needed-to-buy-tickets/2073-time-needed-to-buy-tickets.cpp @@ -0,0 +1,22 @@ +class Solution { +public: + int timeRequiredToBuy(vector& tickets, int k) { + + int n = tickets.size(); + int time = 0; + + for(int i = 0; i < n; ++i) + { + if(i <= k) + { + time += (tickets[i] <= tickets[k] ? tickets[i] : tickets[k]); + } + else + { + time += (tickets[i] < tickets[k] ? tickets[i] : max(0, tickets[k] - 1)); + } + } + + return time; + } +}; \ No newline at end of file From feb610492990501e4ff01f85be5301e8db6073ac Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 9 Apr 2024 23:03:52 +0530 Subject: [PATCH 0858/1399] Attach NOTES - LeetHub From dcebcb190e39f987017bce1015361a288cf2410a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 10 Apr 2024 23:06:02 +0530 Subject: [PATCH 0859/1399] Create README - LeetHub --- .../README.md | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 0950-reveal-cards-in-increasing-order/README.md diff --git a/0950-reveal-cards-in-increasing-order/README.md b/0950-reveal-cards-in-increasing-order/README.md new file mode 100644 index 00000000..8fd4c765 --- /dev/null +++ b/0950-reveal-cards-in-increasing-order/README.md @@ -0,0 +1,49 @@ +

950. Reveal Cards In Increasing Order

Medium


You are given an integer array deck. There is a deck of cards where every card has a unique integer. The integer on the ith card is deck[i].

+ +

You can order the deck in any order you want. Initially, all the cards start face down (unrevealed) in one deck.

+ +

You will do the following steps repeatedly until all cards are revealed:

+ +
    +
  1. Take the top card of the deck, reveal it, and take it out of the deck.
  2. +
  3. If there are still cards in the deck then put the next top card of the deck at the bottom of the deck.
  4. +
  5. If there are still unrevealed cards, go back to step 1. Otherwise, stop.
  6. +
+ +

Return an ordering of the deck that would reveal the cards in increasing order.

+ +

Note that the first entry in the answer is considered to be the top of the deck.

+ +

 

+

Example 1:

+ +
Input: deck = [17,13,11,2,3,5,7]
+Output: [2,13,3,11,5,17,7]
+Explanation: 
+We get the deck in the order [17,13,11,2,3,5,7] (this order does not matter), and reorder it.
+After reordering, the deck starts as [2,13,3,11,5,17,7], where 2 is the top of the deck.
+We reveal 2, and move 13 to the bottom.  The deck is now [3,11,5,17,7,13].
+We reveal 3, and move 11 to the bottom.  The deck is now [5,17,7,13,11].
+We reveal 5, and move 17 to the bottom.  The deck is now [7,13,11,17].
+We reveal 7, and move 13 to the bottom.  The deck is now [11,17,13].
+We reveal 11, and move 17 to the bottom.  The deck is now [13,17].
+We reveal 13, and move 17 to the bottom.  The deck is now [17].
+We reveal 17.
+Since all the cards revealed are in increasing order, the answer is correct.
+
+ +

Example 2:

+ +
Input: deck = [1,1000]
+Output: [1,1000]
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= deck.length <= 1000
  • +
  • 1 <= deck[i] <= 106
  • +
  • All the values of deck are unique.
  • +
+
\ No newline at end of file From e435e759087f3debcdadc4b0861854ecb9a4fd30 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 10 Apr 2024 23:06:02 +0530 Subject: [PATCH 0860/1399] Attach NOTES - LeetHub --- 0950-reveal-cards-in-increasing-order/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0950-reveal-cards-in-increasing-order/NOTES.md diff --git a/0950-reveal-cards-in-increasing-order/NOTES.md b/0950-reveal-cards-in-increasing-order/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0950-reveal-cards-in-increasing-order/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 2457c122204481abdce0a3867a57c020b7dbf390 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 10 Apr 2024 23:06:05 +0530 Subject: [PATCH 0861/1399] Time: 8 ms (18.53%), Space: 12.9 MB (5.03%) - LeetHub --- .../0950-reveal-cards-in-increasing-order.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 0950-reveal-cards-in-increasing-order/0950-reveal-cards-in-increasing-order.cpp diff --git a/0950-reveal-cards-in-increasing-order/0950-reveal-cards-in-increasing-order.cpp b/0950-reveal-cards-in-increasing-order/0950-reveal-cards-in-increasing-order.cpp new file mode 100644 index 00000000..5c3092eb --- /dev/null +++ b/0950-reveal-cards-in-increasing-order/0950-reveal-cards-in-increasing-order.cpp @@ -0,0 +1,15 @@ +class Solution { +public: + vector deckRevealedIncreasing(vector& deck) { + sort(deck.rbegin(), deck.rend()); + deque d; + d.push_back(deck[0]); + for (int i = 1; i < deck.size(); i++) { + d.push_front(d.back()); + d.pop_back(); + d.push_front(deck[i]); + } + vector res(d.begin(), d.end()); + return res; + } +}; \ No newline at end of file From 78670902025f42bdc8e2d717e69d7cff1979eb70 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 11 Apr 2024 22:52:37 +0530 Subject: [PATCH 0862/1399] Attach NOTES - LeetHub From 1621827d163318a5ffc816c03c5563320db55366 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 11 Apr 2024 22:52:40 +0530 Subject: [PATCH 0863/1399] Time: 15 ms (19.57%), Space: 11.7 MB (9.05%) - LeetHub --- 0402-remove-k-digits/0402-remove-k-digits.cpp | 34 +++++++------------ 1 file changed, 13 insertions(+), 21 deletions(-) diff --git a/0402-remove-k-digits/0402-remove-k-digits.cpp b/0402-remove-k-digits/0402-remove-k-digits.cpp index a7e3602b..3a06c361 100644 --- a/0402-remove-k-digits/0402-remove-k-digits.cpp +++ b/0402-remove-k-digits/0402-remove-k-digits.cpp @@ -2,47 +2,39 @@ class Solution { public: string removeKdigits(string num, int k) { - if(k >= num.size()) - return "0"; + stack st; - stack st; + st.push(num[0] - '0'); - int n = num.size(); - - st.push(num[0]); - - for(int i = 1; i < n; ++i) + for(int i = 1; i < num.size(); ++i) { - while(k > 0 and !st.empty() and st.top() > num[i]) + while(!st.empty() and st.top() > (num[i] - '0') and k) { - --k; st.pop(); + k--; } - st.push(num[i]); - if(st.size() == 1 and num[i] == '0') + st.push(num[i] - '0'); + if(st.size() == 1 and st.top() == 0) st.pop(); } - while(k and !st.empty()) + while(!st.empty() and k--) { - --k; st.pop(); } - - string res; + + string ans; while(!st.empty()) { - res += st.top(); + ans += (st.top() + '0'); st.pop(); } - if(res.empty()) - return "0"; + reverse(ans.begin(), ans.end()); - reverse(res.begin(), res.end()); + return ans.empty() ? "0" : ans; - return res; } }; \ No newline at end of file From 6b654bda8fd07fe48211a8c355485981fbdd0399 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 12 Apr 2024 22:44:55 +0530 Subject: [PATCH 0864/1399] Create README - LeetHub --- 0042-trapping-rain-water/README.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 0042-trapping-rain-water/README.md diff --git a/0042-trapping-rain-water/README.md b/0042-trapping-rain-water/README.md new file mode 100644 index 00000000..9d727dd1 --- /dev/null +++ b/0042-trapping-rain-water/README.md @@ -0,0 +1,25 @@ +

42. Trapping Rain Water

Hard


Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it can trap after raining.

+ +

 

+

Example 1:

+ +
Input: height = [0,1,0,2,1,0,1,3,2,1,2,1]
+Output: 6
+Explanation: The above elevation map (black section) is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped.
+
+ +

Example 2:

+ +
Input: height = [4,2,0,3,2,5]
+Output: 9
+
+ +

 

+

Constraints:

+ +
    +
  • n == height.length
  • +
  • 1 <= n <= 2 * 104
  • +
  • 0 <= height[i] <= 105
  • +
+
\ No newline at end of file From e27b1ef5332f7940653dcbce48d97d018f171743 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 12 Apr 2024 22:44:58 +0530 Subject: [PATCH 0865/1399] Time: 4 ms (93.97%), Space: 22.3 MB (58.72%) - LeetHub --- .../0042-trapping-rain-water.cpp | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 0042-trapping-rain-water/0042-trapping-rain-water.cpp diff --git a/0042-trapping-rain-water/0042-trapping-rain-water.cpp b/0042-trapping-rain-water/0042-trapping-rain-water.cpp new file mode 100644 index 00000000..2f7a6ad3 --- /dev/null +++ b/0042-trapping-rain-water/0042-trapping-rain-water.cpp @@ -0,0 +1,23 @@ +class Solution { +public: + int trap(vector& height) { + + int start = 0,end = height.size()-1; + int ans = 0; + int maxleft = height[start], maxright = height[end]; + while(start <= end) + { + maxleft = max(maxleft,height[start]); + maxright = max(maxright,height[end]); + + ans += min(maxleft,maxright) - min(height[start],height[end]); + if(height[start] < height[end]) + ++start; + else + --end; + } + + return ans; + } +}; + From 9d802eb8f809dc46ead11b93b7b2f48749792b18 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 13 Apr 2024 23:02:47 +0530 Subject: [PATCH 0866/1399] Attach NOTES - LeetHub --- 0085-maximal-rectangle/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0085-maximal-rectangle/NOTES.md diff --git a/0085-maximal-rectangle/NOTES.md b/0085-maximal-rectangle/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0085-maximal-rectangle/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 12ff8015f70c1e774f5e1d08265211f45b9bd099 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 13 Apr 2024 23:08:43 +0530 Subject: [PATCH 0867/1399] Attach NOTES - LeetHub From eb82674d8a3fb7c099bd17a2e30c3ed6ae36b7d2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 14 Apr 2024 21:38:08 +0530 Subject: [PATCH 0868/1399] Create README - LeetHub --- 0404-sum-of-left-leaves/README.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 0404-sum-of-left-leaves/README.md diff --git a/0404-sum-of-left-leaves/README.md b/0404-sum-of-left-leaves/README.md new file mode 100644 index 00000000..34e2f167 --- /dev/null +++ b/0404-sum-of-left-leaves/README.md @@ -0,0 +1,26 @@ +

404. Sum of Left Leaves

Easy


Given the root of a binary tree, return the sum of all left leaves.

+ +

A leaf is a node with no children. A left leaf is a leaf that is the left child of another node.

+ +

 

+

Example 1:

+ +
Input: root = [3,9,20,null,null,15,7]
+Output: 24
+Explanation: There are two left leaves in the binary tree, with values 9 and 15 respectively.
+
+ +

Example 2:

+ +
Input: root = [1]
+Output: 0
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is in the range [1, 1000].
  • +
  • -1000 <= Node.val <= 1000
  • +
+
\ No newline at end of file From 9838089d584e916bf71a206832e7889637cc607b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 14 Apr 2024 21:38:13 +0530 Subject: [PATCH 0869/1399] Time: 3 ms (56.94%), Space: 14.6 MB (81.87%) - LeetHub --- .../0404-sum-of-left-leaves.cpp | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 0404-sum-of-left-leaves/0404-sum-of-left-leaves.cpp diff --git a/0404-sum-of-left-leaves/0404-sum-of-left-leaves.cpp b/0404-sum-of-left-leaves/0404-sum-of-left-leaves.cpp new file mode 100644 index 00000000..09675766 --- /dev/null +++ b/0404-sum-of-left-leaves/0404-sum-of-left-leaves.cpp @@ -0,0 +1,42 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + + void helper(TreeNode* root, int& sum) + { + if(!root) + return; + + if(root->left and !root->left->left and !root->left->right) + { + sum += root->left->val; + helper(root->right, sum); + } + else + { + helper(root->left, sum); + helper(root->right, sum); + } + + } + + int sumOfLeftLeaves(TreeNode* root) { + + int sum = 0; + + helper(root, sum); + + return sum; + + } +}; \ No newline at end of file From 886fc05eddf5055de3da3276f2fb012df3140047 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 15 Apr 2024 20:48:11 +0530 Subject: [PATCH 0870/1399] Attach NOTES - LeetHub --- 1004-max-consecutive-ones-iii/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1004-max-consecutive-ones-iii/NOTES.md diff --git a/1004-max-consecutive-ones-iii/NOTES.md b/1004-max-consecutive-ones-iii/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1004-max-consecutive-ones-iii/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 99b5c184ad83e02e97b56949f7f153c50afc1428 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 15 Apr 2024 20:48:15 +0530 Subject: [PATCH 0871/1399] Time: 40 ms (63.04%), Space: 57.8 MB (49.20%) - LeetHub --- .../1004-max-consecutive-ones-iii.cpp | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 1004-max-consecutive-ones-iii/1004-max-consecutive-ones-iii.cpp diff --git a/1004-max-consecutive-ones-iii/1004-max-consecutive-ones-iii.cpp b/1004-max-consecutive-ones-iii/1004-max-consecutive-ones-iii.cpp new file mode 100644 index 00000000..b2d6bfa9 --- /dev/null +++ b/1004-max-consecutive-ones-iii/1004-max-consecutive-ones-iii.cpp @@ -0,0 +1,26 @@ +class Solution { +public: + int longestOnes(vector& nums, int k) { + + int n = nums.size(); + + int i = 0, j = 0, zeroCnt = 0, ans = 0; + + while(j < n) + { + zeroCnt += (nums[j] == 0 ? 1 : 0); + + while(zeroCnt > k) + { + zeroCnt -= (nums[i] == 0 ? 1 : 0); + ++i; + } + + ans = max(ans, j - i + 1); + + ++j; + } + + return ans; + } +}; \ No newline at end of file From 533eb3932fbe0d0a5176983859d11f61ab2b47db Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 16 Apr 2024 21:21:00 +0530 Subject: [PATCH 0872/1399] Create README - LeetHub --- 0623-add-one-row-to-tree/README.md | 37 ++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 0623-add-one-row-to-tree/README.md diff --git a/0623-add-one-row-to-tree/README.md b/0623-add-one-row-to-tree/README.md new file mode 100644 index 00000000..c1f4b23e --- /dev/null +++ b/0623-add-one-row-to-tree/README.md @@ -0,0 +1,37 @@ +

623. Add One Row to Tree

Medium


Given the root of a binary tree and two integers val and depth, add a row of nodes with value val at the given depth depth.

+ +

Note that the root node is at depth 1.

+ +

The adding rule is:

+ +
    +
  • Given the integer depth, for each not null tree node cur at the depth depth - 1, create two tree nodes with value val as cur's left subtree root and right subtree root.
  • +
  • cur's original left subtree should be the left subtree of the new left subtree root.
  • +
  • cur's original right subtree should be the right subtree of the new right subtree root.
  • +
  • If depth == 1 that means there is no depth depth - 1 at all, then create a tree node with value val as the new root of the whole original tree, and the original tree is the new root's left subtree.
  • +
+ +

 

+

Example 1:

+ +
Input: root = [4,2,6,3,1,5], val = 1, depth = 2
+Output: [4,1,1,2,null,null,6,3,1,5]
+
+ +

Example 2:

+ +
Input: root = [4,2,null,3,1], val = 1, depth = 3
+Output: [4,2,null,1,1,3,null,null,1]
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is in the range [1, 104].
  • +
  • The depth of the tree is in the range [1, 104].
  • +
  • -100 <= Node.val <= 100
  • +
  • -105 <= val <= 105
  • +
  • 1 <= depth <= the depth of tree + 1
  • +
+
\ No newline at end of file From ede2ba4bb659b27fc1224a6fa2574481694e5bf0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 16 Apr 2024 21:21:05 +0530 Subject: [PATCH 0873/1399] Time: 17 ms (22.67%), Space: 24.6 MB (14.90%) - LeetHub --- .../0623-add-one-row-to-tree.cpp | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 0623-add-one-row-to-tree/0623-add-one-row-to-tree.cpp diff --git a/0623-add-one-row-to-tree/0623-add-one-row-to-tree.cpp b/0623-add-one-row-to-tree/0623-add-one-row-to-tree.cpp new file mode 100644 index 00000000..6b5b032c --- /dev/null +++ b/0623-add-one-row-to-tree/0623-add-one-row-to-tree.cpp @@ -0,0 +1,63 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + TreeNode* addOneRow(TreeNode* root, int val, int depth) { + + if(depth == 1) + { + TreeNode* node = new TreeNode(val); + + node->left = root; + + return node; + } + + queue q; + + --depth; + q.push(root); + + while(!q.empty()) + { + int sz = q.size(); + + for(int i = 0; i < sz; ++i) + { + TreeNode* curr = q.front(); + q.pop(); + + if(depth == 1) + { + TreeNode* nexLeft = curr->left ? curr->left : nullptr; + TreeNode* newNode1 = new TreeNode(val); + curr->left = newNode1; + newNode1->left = nexLeft; + + TreeNode* nexRight = curr->right ? curr->right : nullptr; + TreeNode* newNode2 = new TreeNode(val); + curr->right = newNode2; + newNode2->right = nexRight; + } + + if(curr->left) + q.push(curr->left); + if(curr->right) + q.push(curr->right); + } + + --depth; + } + + return root; + } +}; \ No newline at end of file From 19be9f646349c0d97531c81c25b0776808d5da14 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 17 Apr 2024 23:35:31 +0530 Subject: [PATCH 0874/1399] Create README - LeetHub --- .../README.md | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 0988-smallest-string-starting-from-leaf/README.md diff --git a/0988-smallest-string-starting-from-leaf/README.md b/0988-smallest-string-starting-from-leaf/README.md new file mode 100644 index 00000000..2ba3cfed --- /dev/null +++ b/0988-smallest-string-starting-from-leaf/README.md @@ -0,0 +1,39 @@ +

988. Smallest String Starting From Leaf

Medium


You are given the root of a binary tree where each node has a value in the range [0, 25] representing the letters 'a' to 'z'.

+ +

Return the lexicographically smallest string that starts at a leaf of this tree and ends at the root.

+ +

As a reminder, any shorter prefix of a string is lexicographically smaller.

+ +
    +
  • For example, "ab" is lexicographically smaller than "aba".
  • +
+ +

A leaf of a node is a node that has no children.

+ +

 

+

Example 1:

+ +
Input: root = [0,1,2,3,4,3,4]
+Output: "dba"
+
+ +

Example 2:

+ +
Input: root = [25,1,3,1,3,0,2]
+Output: "adz"
+
+ +

Example 3:

+ +
Input: root = [2,2,1,null,1,0,null,0]
+Output: "abc"
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is in the range [1, 8500].
  • +
  • 0 <= Node.val <= 25
  • +
+
\ No newline at end of file From bf33b4dc87398435402d9414c2aa4678a78ce5bd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 17 Apr 2024 23:35:34 +0530 Subject: [PATCH 0875/1399] Time: 7 ms (80.26%), Space: 20.4 MB (67.10%) - LeetHub --- ...988-smallest-string-starting-from-leaf.cpp | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 0988-smallest-string-starting-from-leaf/0988-smallest-string-starting-from-leaf.cpp diff --git a/0988-smallest-string-starting-from-leaf/0988-smallest-string-starting-from-leaf.cpp b/0988-smallest-string-starting-from-leaf/0988-smallest-string-starting-from-leaf.cpp new file mode 100644 index 00000000..c71963a8 --- /dev/null +++ b/0988-smallest-string-starting-from-leaf/0988-smallest-string-starting-from-leaf.cpp @@ -0,0 +1,35 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + + string ans = "~"; + + void helper(TreeNode* root, string curr) + { + if(!root) + return; + + if(!root->left and !root->right) + ans = min(ans, char(root->val + 'a') + curr); + + helper(root->left, char(root->val + 'a') + curr); + helper(root->right, char(root->val + 'a') + curr); + } + + string smallestFromLeaf(TreeNode* root) { + + helper(root, ""); + return ans; + + } +}; \ No newline at end of file From 1796dab2294f1dfe48af278844460aa75490ac69 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 19 Apr 2024 08:57:58 +0530 Subject: [PATCH 0876/1399] Attach NOTES - LeetHub From af2467b8ec553ca698205caeb19be5fcd217b07e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 19 Apr 2024 08:58:02 +0530 Subject: [PATCH 0877/1399] Time: 47 ms (14.06%), Space: 23.5 MB (21.40%) - LeetHub --- .../0200-number-of-islands.cpp | 101 +++++++++++++----- 1 file changed, 74 insertions(+), 27 deletions(-) diff --git a/0200-number-of-islands/0200-number-of-islands.cpp b/0200-number-of-islands/0200-number-of-islands.cpp index 7a793f47..29d1a177 100644 --- a/0200-number-of-islands/0200-number-of-islands.cpp +++ b/0200-number-of-islands/0200-number-of-islands.cpp @@ -1,52 +1,99 @@ -class Solution { -public: - - vector dy = {-1, +1, 0, 0}; - vector dx = {0, 0, -1, +1}; +class DSU{ + public: + int N; + vector parent, size; + + DSU(int n) + { + N = n; + parent.resize(N); + size.resize(N, 1); + for(int i = 0; i < N; ++i) + parent[i] = i; + } - bool isValid(int i, int j, int n, int m) - { - return (i >= 0 and j >= 0 and i < n and j < m); - } + int findParent(int u) + { + if(u == parent[u]) + return u; + return parent[u] = findParent(parent[u]); + } - void dfs(int i, int j, int n, int m, vector>& grid, vector>& visited) - { - visited[i][j] = true; - - for(int k = 0; k < 4; ++k) + void unionBySize(int u, int v) { - int newx = dx[k] + i; - int newy = dy[k] + j; + int parU = findParent(u); + int parV = findParent(v); - if(isValid(newx, newy, n, m) and !visited[newx][newy] and grid[newx][newy] == '1') + if(parU == parV) + return; + + if(size[parU] < size[parV]) { - dfs(newx, newy, n, m, grid, visited); + parent[parU] = parV; + size[parV] += size[parU]; + } + else + { + parent[parV] = parU; + size[parU] += size[parV]; } } - } + bool isSame(int u, int v) + { + return findParent(u) == findParent(v); + } +}; + +class Solution { +public: int numIslands(vector>& grid) { - int n = grid.size(); - int m = grid[0].size(); + int n = grid.size(), m = grid[0].size(); + + vector dx = {-1, 0, 0, +1}; + vector dy = {0, -1, +1 ,0}; + + DSU dsu(n*m + 1); - int numberOfIslands = 0; + set ls; - vector> visited(n, vector(m, false)); + int island = 0; for(int i = 0; i < n; ++i) { for(int j = 0; j < m; ++j) { - if(grid[i][j] == '1' and !visited[i][j]) + if(grid[i][j] == '1') { - ++numberOfIslands; - dfs(i, j, n, m, grid, visited); + ls.insert((i*m) + j); + for(int k = 0; k < 4; ++k) + { + int cx = dx[k] + i; + int cy = dy[k] + j; + + if(cx >= 0 and cy >= 0 and cx < n and cy < m and grid[cx][cy] == '1') + { + int node = (i * m) + j; + int adjNode = (cx * m) + cy; + + if(!dsu.isSame(node, adjNode)) + { + dsu.unionBySize(node, adjNode); + } + } + } } } } - return numberOfIslands; + for(auto& node : ls) + { + if(dsu.findParent(node) == node) + ++island; + } + + return island; } }; \ No newline at end of file From b4217dbc39c85a9824c099d2ad247f049162b731 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 20 Apr 2024 09:47:31 +0530 Subject: [PATCH 0878/1399] Create README - LeetHub --- 1992-find-all-groups-of-farmland/README.md | 45 ++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 1992-find-all-groups-of-farmland/README.md diff --git a/1992-find-all-groups-of-farmland/README.md b/1992-find-all-groups-of-farmland/README.md new file mode 100644 index 00000000..783ad402 --- /dev/null +++ b/1992-find-all-groups-of-farmland/README.md @@ -0,0 +1,45 @@ +

1992. Find All Groups of Farmland

Medium


You are given a 0-indexed m x n binary matrix land where a 0 represents a hectare of forested land and a 1 represents a hectare of farmland.

+ +

To keep the land organized, there are designated rectangular areas of hectares that consist entirely of farmland. These rectangular areas are called groups. No two groups are adjacent, meaning farmland in one group is not four-directionally adjacent to another farmland in a different group.

+ +

land can be represented by a coordinate system where the top left corner of land is (0, 0) and the bottom right corner of land is (m-1, n-1). Find the coordinates of the top left and bottom right corner of each group of farmland. A group of farmland with a top left corner at (r1, c1) and a bottom right corner at (r2, c2) is represented by the 4-length array [r1, c1, r2, c2].

+ +

Return a 2D array containing the 4-length arrays described above for each group of farmland in land. If there are no groups of farmland, return an empty array. You may return the answer in any order.

+ +

 

+

Example 1:

+ +
Input: land = [[1,0,0],[0,1,1],[0,1,1]]
+Output: [[0,0,0,0],[1,1,2,2]]
+Explanation:
+The first group has a top left corner at land[0][0] and a bottom right corner at land[0][0].
+The second group has a top left corner at land[1][1] and a bottom right corner at land[2][2].
+
+ +

Example 2:

+ +
Input: land = [[1,1],[1,1]]
+Output: [[0,0,1,1]]
+Explanation:
+The first group has a top left corner at land[0][0] and a bottom right corner at land[1][1].
+
+ +

Example 3:

+ +
Input: land = [[0]]
+Output: []
+Explanation:
+There are no groups of farmland.
+
+ +

 

+

Constraints:

+ +
    +
  • m == land.length
  • +
  • n == land[i].length
  • +
  • 1 <= m, n <= 300
  • +
  • land consists of only 0's and 1's.
  • +
  • Groups of farmland are rectangular in shape.
  • +
+
\ No newline at end of file From bc7e91f0348101755bc44b8f8fdf799d8c9af052 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 20 Apr 2024 09:47:34 +0530 Subject: [PATCH 0879/1399] Time: 167 ms (34.91%), Space: 83.8 MB (40.73%) - LeetHub --- .../1992-find-all-groups-of-farmland.cpp | 145 ++++++++++++++++++ 1 file changed, 145 insertions(+) create mode 100644 1992-find-all-groups-of-farmland/1992-find-all-groups-of-farmland.cpp diff --git a/1992-find-all-groups-of-farmland/1992-find-all-groups-of-farmland.cpp b/1992-find-all-groups-of-farmland/1992-find-all-groups-of-farmland.cpp new file mode 100644 index 00000000..add477bd --- /dev/null +++ b/1992-find-all-groups-of-farmland/1992-find-all-groups-of-farmland.cpp @@ -0,0 +1,145 @@ +class DSU{ + private: + vector size, rank, parent; + public: + DSU(int n) + { + int N = n; + size.resize(N, 1); + rank.resize(N, 0); + parent.resize(N); + for(int i = 0; i < N; ++i) + parent[i] = i; + } + + int findParent(int u) + { + if(u == parent[u]) + return u; + return parent[u] = findParent(parent[u]); + } + + void unionByRank(int u, int v) + { + int parU = findParent(u); + int parV = findParent(v); + + if(parU == parV) + return; + + if(rank[parU] < rank[parV]) + { + parent[parU] = parV; + } + else if(rank[parU] > rank[parV]) + { + parent[parV] = parU; + } + else + { + parent[parV] = parU; + ++rank[parU]; + } + } + + void unionBySize(int u, int v) + { + int parU = findParent(u); + int parV = findParent(v); + + if(parU == parV) + return; + + if(size[parU] < size[parV]) + { + parent[parU] = parV; + size[parV] += size[parU]; + } + else{ + parent[parV] = parU; + size[parU] += size[parV]; + } + } + + bool isSame(int u, int v) + { + return findParent(u) == findParent(v); + } +}; + +class Solution { +public: + vector> findFarmland(vector>& land) { + + int n = land.size(); + int m = land[0].size(); + + vector dx = {-1, 0, 0, +1}; + vector dy = {0, -1, +1, 0}; + + auto isValid = [&](int x, int y)->bool + { + return (x >= 0 and y >= 0 and x < n and y < m); + }; + + DSU dsu((n*m) + 1); + + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < m; ++j) + { + if(land[i][j] == 1) + { + int node = (i * m) + j; + + for(int k = 0; k < 4; ++k) + { + int nx = dx[k] + i; + int ny = dy[k] + j; + + if(isValid(nx, ny) and land[nx][ny] == 1) + { + int adjNode = (nx * m) + ny; + + if(!dsu.isSame(node, adjNode)) + dsu.unionBySize(node, adjNode); + } + } + } + } + } + + map> mp; + + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < m; ++j) + { + if(land[i][j] == 1) + { + int node = (i * m) + j; + int par = dsu.findParent(node); + + if(mp.find(par) == mp.end()) + { + mp[par] = {i, j, i, j}; + } + else + { + mp[par][2] = i; + mp[par][3] = j; + } + + } + } + } + + vector> res; + + for(auto&[_, vec] : mp) + res.push_back(vec); + + return res; + + } +}; \ No newline at end of file From 803105da53206b86b05a38186c1ed9b22d9f326c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 21 Apr 2024 22:27:37 +0530 Subject: [PATCH 0880/1399] Create README - LeetHub --- 1971-find-if-path-exists-in-graph/README.md | 37 +++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 1971-find-if-path-exists-in-graph/README.md diff --git a/1971-find-if-path-exists-in-graph/README.md b/1971-find-if-path-exists-in-graph/README.md new file mode 100644 index 00000000..d95edbe4 --- /dev/null +++ b/1971-find-if-path-exists-in-graph/README.md @@ -0,0 +1,37 @@ +

1971. Find if Path Exists in Graph

Easy


There is a bi-directional graph with n vertices, where each vertex is labeled from 0 to n - 1 (inclusive). The edges in the graph are represented as a 2D integer array edges, where each edges[i] = [ui, vi] denotes a bi-directional edge between vertex ui and vertex vi. Every vertex pair is connected by at most one edge, and no vertex has an edge to itself.

+ +

You want to determine if there is a valid path that exists from vertex source to vertex destination.

+ +

Given edges and the integers n, source, and destination, return true if there is a valid path from source to destination, or false otherwise.

+ +

 

+

Example 1:

+ +
Input: n = 3, edges = [[0,1],[1,2],[2,0]], source = 0, destination = 2
+Output: true
+Explanation: There are two paths from vertex 0 to vertex 2:
+- 0 → 1 → 2
+- 0 → 2
+
+ +

Example 2:

+ +
Input: n = 6, edges = [[0,1],[0,2],[3,5],[5,4],[4,3]], source = 0, destination = 5
+Output: false
+Explanation: There is no path from vertex 0 to vertex 5.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= 2 * 105
  • +
  • 0 <= edges.length <= 2 * 105
  • +
  • edges[i].length == 2
  • +
  • 0 <= ui, vi <= n - 1
  • +
  • ui != vi
  • +
  • 0 <= source, destination <= n - 1
  • +
  • There are no duplicate edges.
  • +
  • There are no self edges.
  • +
+
\ No newline at end of file From 36b463b084d73d43f247511a4c2b921eb2d979c1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 22 Apr 2024 22:47:09 +0530 Subject: [PATCH 0881/1399] Create README - LeetHub --- 0752-open-the-lock/README.md | 44 ++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 0752-open-the-lock/README.md diff --git a/0752-open-the-lock/README.md b/0752-open-the-lock/README.md new file mode 100644 index 00000000..36cb276f --- /dev/null +++ b/0752-open-the-lock/README.md @@ -0,0 +1,44 @@ +

752. Open the Lock

Medium


You have a lock in front of you with 4 circular wheels. Each wheel has 10 slots: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'. The wheels can rotate freely and wrap around: for example we can turn '9' to be '0', or '0' to be '9'. Each move consists of turning one wheel one slot.

+ +

The lock initially starts at '0000', a string representing the state of the 4 wheels.

+ +

You are given a list of deadends dead ends, meaning if the lock displays any of these codes, the wheels of the lock will stop turning and you will be unable to open it.

+ +

Given a target representing the value of the wheels that will unlock the lock, return the minimum total number of turns required to open the lock, or -1 if it is impossible.

+ +

 

+

Example 1:

+ +
Input: deadends = ["0201","0101","0102","1212","2002"], target = "0202"
+Output: 6
+Explanation: 
+A sequence of valid moves would be "0000" -> "1000" -> "1100" -> "1200" -> "1201" -> "1202" -> "0202".
+Note that a sequence like "0000" -> "0001" -> "0002" -> "0102" -> "0202" would be invalid,
+because the wheels of the lock become stuck after the display becomes the dead end "0102".
+
+ +

Example 2:

+ +
Input: deadends = ["8888"], target = "0009"
+Output: 1
+Explanation: We can turn the last wheel in reverse to move from "0000" -> "0009".
+
+ +

Example 3:

+ +
Input: deadends = ["8887","8889","8878","8898","8788","8988","7888","9888"], target = "8888"
+Output: -1
+Explanation: We cannot reach the target without getting stuck.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= deadends.length <= 500
  • +
  • deadends[i].length == 4
  • +
  • target.length == 4
  • +
  • target will not be in the list deadends.
  • +
  • target and deadends[i] consist of digits only.
  • +
+
\ No newline at end of file From ba7c11fed6037328d1dd57c31eb92bf0cb5f0aad Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 22 Apr 2024 22:47:13 +0530 Subject: [PATCH 0882/1399] Time: 490 ms (11.35%), Space: 40.2 MB (53.65%) - LeetHub --- 0752-open-the-lock/0752-open-the-lock.cpp | 72 +++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 0752-open-the-lock/0752-open-the-lock.cpp diff --git a/0752-open-the-lock/0752-open-the-lock.cpp b/0752-open-the-lock/0752-open-the-lock.cpp new file mode 100644 index 00000000..b7539355 --- /dev/null +++ b/0752-open-the-lock/0752-open-the-lock.cpp @@ -0,0 +1,72 @@ +class Solution { +public: + + string forward(string curr, int idx) + { + if(curr[idx] == '9') + curr[idx] = '0'; + else + ++curr[idx]; + return curr; + } + + string backward(string curr, int idx) + { + if(curr[idx] == '0') + curr[idx] = '9'; + else + --curr[idx]; + return curr; + } + + int openLock(vector& deadends, string target) { + + set st(deadends.begin(), deadends.end()); + + set visited; + + queue q; + + if(!st.count("0000")) + { + q.push("0000"); + visited.insert("0000"); + } + + int steps = 0; + + while(!q.empty()) + { + int size = q.size(); + + for(int i = 0; i < size; ++i) + { + string curr = q.front(); + q.pop(); + + if(curr == target) + return steps; + + for(int i = 0; i < 4; ++i) + { + string f = forward(curr, i); + string s = backward(curr, i); + + if(!st.count(f) and !visited.count(f)) + { + q.push(f); + visited.insert(f); + } + if(!st.count(s) and !visited.count(s)) + { + q.push(s); + visited.insert(s); + } + } + } + ++steps; + } + + return -1; + } +}; \ No newline at end of file From 1d3efba18dc59079d1f9066c07ab1ed3a31d80e4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 23 Apr 2024 23:38:10 +0530 Subject: [PATCH 0883/1399] Create README - LeetHub --- 0310-minimum-height-trees/README.md | 34 +++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 0310-minimum-height-trees/README.md diff --git a/0310-minimum-height-trees/README.md b/0310-minimum-height-trees/README.md new file mode 100644 index 00000000..de5a228a --- /dev/null +++ b/0310-minimum-height-trees/README.md @@ -0,0 +1,34 @@ +

310. Minimum Height Trees

Medium


A tree is an undirected graph in which any two vertices are connected by exactly one path. In other words, any connected graph without simple cycles is a tree.

+ +

Given a tree of n nodes labelled from 0 to n - 1, and an array of n - 1 edges where edges[i] = [ai, bi] indicates that there is an undirected edge between the two nodes ai and bi in the tree, you can choose any node of the tree as the root. When you select a node x as the root, the result tree has height h. Among all possible rooted trees, those with minimum height (i.e. min(h))  are called minimum height trees (MHTs).

+ +

Return a list of all MHTs' root labels. You can return the answer in any order.

+ +

The height of a rooted tree is the number of edges on the longest downward path between the root and a leaf.

+ +

 

+

Example 1:

+ +
Input: n = 4, edges = [[1,0],[1,2],[1,3]]
+Output: [1]
+Explanation: As shown, the height of the tree is 1 when the root is the node with label 1 which is the only MHT.
+
+ +

Example 2:

+ +
Input: n = 6, edges = [[3,0],[3,1],[3,2],[3,4],[5,4]]
+Output: [3,4]
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= 2 * 104
  • +
  • edges.length == n - 1
  • +
  • 0 <= ai, bi < n
  • +
  • ai != bi
  • +
  • All the pairs (ai, bi) are distinct.
  • +
  • The given input is guaranteed to be a tree and there will be no repeated edges.
  • +
+
\ No newline at end of file From 86c6de911f2527cbee11386d474079e32bfce9e5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 23 Apr 2024 23:38:14 +0530 Subject: [PATCH 0884/1399] Time: 99 ms (86.83%), Space: 55.9 MB (72.52%) - LeetHub --- .../0310-minimum-height-trees.cpp | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 0310-minimum-height-trees/0310-minimum-height-trees.cpp diff --git a/0310-minimum-height-trees/0310-minimum-height-trees.cpp b/0310-minimum-height-trees/0310-minimum-height-trees.cpp new file mode 100644 index 00000000..075848c2 --- /dev/null +++ b/0310-minimum-height-trees/0310-minimum-height-trees.cpp @@ -0,0 +1,34 @@ +class Solution { +public: + vector findMinHeightTrees(int n, vector>& edges) { + vector> graph(n); + vector indegree(n, 0), ans; + + for(auto &e : edges){ + graph[e[0]].push_back(e[1]); + graph[e[1]].push_back(e[0]); + indegree[e[0]]++; + indegree[e[1]]++; + } + + queue q; + for(int i=0; i Date: Thu, 25 Apr 2024 22:51:07 +0530 Subject: [PATCH 0885/1399] Create README - LeetHub --- 2370-longest-ideal-subsequence/README.md | 37 ++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 2370-longest-ideal-subsequence/README.md diff --git a/2370-longest-ideal-subsequence/README.md b/2370-longest-ideal-subsequence/README.md new file mode 100644 index 00000000..17c7257e --- /dev/null +++ b/2370-longest-ideal-subsequence/README.md @@ -0,0 +1,37 @@ +

2370. Longest Ideal Subsequence

Medium


You are given a string s consisting of lowercase letters and an integer k. We call a string t ideal if the following conditions are satisfied:

+ +
    +
  • t is a subsequence of the string s.
  • +
  • The absolute difference in the alphabet order of every two adjacent letters in t is less than or equal to k.
  • +
+ +

Return the length of the longest ideal string.

+ +

A subsequence is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.

+ +

Note that the alphabet order is not cyclic. For example, the absolute difference in the alphabet order of 'a' and 'z' is 25, not 1.

+ +

 

+

Example 1:

+ +
Input: s = "acfgbd", k = 2
+Output: 4
+Explanation: The longest ideal string is "acbd". The length of this string is 4, so 4 is returned.
+Note that "acfgbd" is not ideal because 'c' and 'f' have a difference of 3 in alphabet order.
+ +

Example 2:

+ +
Input: s = "abcd", k = 3
+Output: 4
+Explanation: The longest ideal string is "abcd". The length of this string is 4, so 4 is returned.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 105
  • +
  • 0 <= k <= 25
  • +
  • s consists of lowercase English letters.
  • +
+
\ No newline at end of file From 880c22761bb6a3a96714a63328e02ae09829d3cc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 25 Apr 2024 22:51:10 +0530 Subject: [PATCH 0886/1399] Time: 624 ms (6.72%), Space: 480.1 MB (6.30%) - LeetHub --- .../2370-longest-ideal-subsequence.cpp | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 2370-longest-ideal-subsequence/2370-longest-ideal-subsequence.cpp diff --git a/2370-longest-ideal-subsequence/2370-longest-ideal-subsequence.cpp b/2370-longest-ideal-subsequence/2370-longest-ideal-subsequence.cpp new file mode 100644 index 00000000..d54a17f5 --- /dev/null +++ b/2370-longest-ideal-subsequence/2370-longest-ideal-subsequence.cpp @@ -0,0 +1,31 @@ +class Solution { +public: + + int helper(int idx, int prev, int k, int n, string& s, vector>& dp) + { + if(idx == n) + return 0; + + if(dp[idx][prev+1] != -1) + return dp[idx][prev+1]; + + int notTake = helper(idx+1, prev, k, n, s, dp); + + int take = 0; + int curr = s[idx]; + if(prev == -1 or abs(prev - curr) <= k) + take = max(1 + helper(idx+1, curr, k, n, s, dp), notTake); + + return dp[idx][prev+1] = max(take, notTake); + } + + int longestIdealString(string s, int k) { + + int n = s.size(); + + vector> dp(n+1, vector(150, -1)); + + return helper(0, -1, k, n, s, dp); + + } +}; \ No newline at end of file From f139a943b4ce004418601ef7d7d5f14e11d2fa09 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 26 Apr 2024 21:55:38 +0530 Subject: [PATCH 0887/1399] Create README - LeetHub --- 1289-minimum-falling-path-sum-ii/README.md | 32 ++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 1289-minimum-falling-path-sum-ii/README.md diff --git a/1289-minimum-falling-path-sum-ii/README.md b/1289-minimum-falling-path-sum-ii/README.md new file mode 100644 index 00000000..90e5bbb6 --- /dev/null +++ b/1289-minimum-falling-path-sum-ii/README.md @@ -0,0 +1,32 @@ +

1289. Minimum Falling Path Sum II

Hard


Given an n x n integer matrix grid, return the minimum sum of a falling path with non-zero shifts.

+ +

A falling path with non-zero shifts is a choice of exactly one element from each row of grid such that no two elements chosen in adjacent rows are in the same column.

+ +

 

+

Example 1:

+ +
Input: grid = [[1,2,3],[4,5,6],[7,8,9]]
+Output: 13
+Explanation: 
+The possible falling paths are:
+[1,5,9], [1,5,7], [1,6,7], [1,6,8],
+[2,4,8], [2,4,9], [2,6,7], [2,6,8],
+[3,4,8], [3,4,9], [3,5,7], [3,5,9]
+The falling path with the smallest sum is [1,5,7], so the answer is 13.
+
+ +

Example 2:

+ +
Input: grid = [[7]]
+Output: 7
+
+ +

 

+

Constraints:

+ +
    +
  • n == grid.length == grid[i].length
  • +
  • 1 <= n <= 200
  • +
  • -99 <= grid[i][j] <= 99
  • +
+
\ No newline at end of file From d806fb97def4b8a46e90e8945c5e0c0f367f2f13 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 26 Apr 2024 21:55:42 +0530 Subject: [PATCH 0888/1399] Time: 355 ms (23.00%), Space: 18.3 MB (16.18%) - LeetHub --- .../1289-minimum-falling-path-sum-ii.cpp | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 1289-minimum-falling-path-sum-ii/1289-minimum-falling-path-sum-ii.cpp diff --git a/1289-minimum-falling-path-sum-ii/1289-minimum-falling-path-sum-ii.cpp b/1289-minimum-falling-path-sum-ii/1289-minimum-falling-path-sum-ii.cpp new file mode 100644 index 00000000..b667b066 --- /dev/null +++ b/1289-minimum-falling-path-sum-ii/1289-minimum-falling-path-sum-ii.cpp @@ -0,0 +1,32 @@ +class Solution { +public: + + int helper(int idx, int prev, int n, vector>& grid, vector>& dp) + { + if(idx == n) + return 0; + + if(dp[idx][prev+1] != -1) + return dp[idx][prev+1]; + + int ans = INT_MAX; + for(int j = 0; j < n; ++j) + { + if(j != prev) + { + int curr = grid[idx][j] + helper(idx+1, j, n, grid, dp); + ans = min(ans, curr); + } + } + + return dp[idx][prev+1] = ans; + } + + int minFallingPathSum(vector>& grid) { + + int n = grid.size(); + vector> dp(n+1, vector(n+1, -1)); + + return helper(0, -1, n, grid, dp); + } +}; \ No newline at end of file From 885fd3384839dfc4dd73c37f906a8521c4f71410 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 27 Apr 2024 22:27:14 +0530 Subject: [PATCH 0889/1399] Attach NOTES - LeetHub --- 0514-freedom-trail/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0514-freedom-trail/NOTES.md diff --git a/0514-freedom-trail/NOTES.md b/0514-freedom-trail/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0514-freedom-trail/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 16ac2d0c5e57304fbb928aff1d2e8c685c861eab Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 27 Apr 2024 22:27:17 +0530 Subject: [PATCH 0890/1399] Time: 22 ms (55.19%), Space: 14.8 MB (58.02%) - LeetHub From b0e9ac7492c8a28d5d71f25571c03f64d4acab37 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 28 Apr 2024 23:46:23 +0530 Subject: [PATCH 0891/1399] Attach NOTES - LeetHub --- 0834-sum-of-distances-in-tree/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0834-sum-of-distances-in-tree/NOTES.md diff --git a/0834-sum-of-distances-in-tree/NOTES.md b/0834-sum-of-distances-in-tree/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0834-sum-of-distances-in-tree/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From ae9b0d8ccceb3aacbeececeb48607a0e22112fb1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 29 Apr 2024 07:52:56 +0530 Subject: [PATCH 0892/1399] Create README - LeetHub --- .../README.md | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 2997-minimum-number-of-operations-to-make-array-xor-equal-to-k/README.md diff --git a/2997-minimum-number-of-operations-to-make-array-xor-equal-to-k/README.md b/2997-minimum-number-of-operations-to-make-array-xor-equal-to-k/README.md new file mode 100644 index 00000000..8da44784 --- /dev/null +++ b/2997-minimum-number-of-operations-to-make-array-xor-equal-to-k/README.md @@ -0,0 +1,40 @@ +

2997. Minimum Number of Operations to Make Array XOR Equal to K

Medium


You are given a 0-indexed integer array nums and a positive integer k.

+ +

You can apply the following operation on the array any number of times:

+ +
    +
  • Choose any element of the array and flip a bit in its binary representation. Flipping a bit means changing a 0 to 1 or vice versa.
  • +
+ +

Return the minimum number of operations required to make the bitwise XOR of all elements of the final array equal to k.

+ +

Note that you can flip leading zero bits in the binary representation of elements. For example, for the number (101)2 you can flip the fourth bit and obtain (1101)2.

+ +

 

+

Example 1:

+ +
Input: nums = [2,1,3,4], k = 1
+Output: 2
+Explanation: We can do the following operations:
+- Choose element 2 which is 3 == (011)2, we flip the first bit and we obtain (010)2 == 2. nums becomes [2,1,2,4].
+- Choose element 0 which is 2 == (010)2, we flip the third bit and we obtain (110)2 = 6. nums becomes [6,1,2,4].
+The XOR of elements of the final array is (6 XOR 1 XOR 2 XOR 4) == 1 == k.
+It can be shown that we cannot make the XOR equal to k in less than 2 operations.
+
+ +

Example 2:

+ +
Input: nums = [2,0,2,0], k = 0
+Output: 0
+Explanation: The XOR of elements of the array is (2 XOR 0 XOR 2 XOR 0) == 0 == k. So no operation is needed.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 105
  • +
  • 0 <= nums[i] <= 106
  • +
  • 0 <= k <= 106
  • +
+
\ No newline at end of file From a21d5cfba36aa598f23dfde477e622eff2d223f9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 29 Apr 2024 07:52:57 +0530 Subject: [PATCH 0893/1399] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2997-minimum-number-of-operations-to-make-array-xor-equal-to-k/NOTES.md diff --git a/2997-minimum-number-of-operations-to-make-array-xor-equal-to-k/NOTES.md b/2997-minimum-number-of-operations-to-make-array-xor-equal-to-k/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2997-minimum-number-of-operations-to-make-array-xor-equal-to-k/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 807504cb27bc690ca7433cea670df9fc2523c9bc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 29 Apr 2024 07:52:59 +0530 Subject: [PATCH 0894/1399] Time: 181 ms (9.95%), Space: 91.5 MB (12.57%) - LeetHub --- ...perations-to-make-array-xor-equal-to-k.cpp | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 2997-minimum-number-of-operations-to-make-array-xor-equal-to-k/2997-minimum-number-of-operations-to-make-array-xor-equal-to-k.cpp diff --git a/2997-minimum-number-of-operations-to-make-array-xor-equal-to-k/2997-minimum-number-of-operations-to-make-array-xor-equal-to-k.cpp b/2997-minimum-number-of-operations-to-make-array-xor-equal-to-k/2997-minimum-number-of-operations-to-make-array-xor-equal-to-k.cpp new file mode 100644 index 00000000..2c31aa57 --- /dev/null +++ b/2997-minimum-number-of-operations-to-make-array-xor-equal-to-k/2997-minimum-number-of-operations-to-make-array-xor-equal-to-k.cpp @@ -0,0 +1,37 @@ +class Solution { +public: + int minOperations(vector& nums, int k) { + + int n = nums.size(); + + vector bits(32, 0); + + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < 32; ++j) + { + if(nums[i] & (1 << j)) + ++bits[j]; + } + } + + int op = 0; + + for(int i = 0; i < 32; ++i) + { + if(k & (1 << i)) + { + if(bits[i] % 2 == 0) + ++op; + } + else + { + if(bits[i] & 1) + ++op; + } + } + + return op; + + } +}; \ No newline at end of file From e9cf04023cbe1826983569babcf8e2c76973ebda Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 29 Apr 2024 07:54:10 +0530 Subject: [PATCH 0895/1399] Attach NOTES - LeetHub From 409aa24c3672e05d563a8bfa29655a32b1cb5a78 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 29 Apr 2024 07:54:14 +0530 Subject: [PATCH 0896/1399] Time: 181 ms (9.95%), Space: 91.5 MB (12.57%) - LeetHub From 9373e7bd4d25d2a22ffc8a638fbbd04770fe9485 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 29 Apr 2024 07:54:18 +0530 Subject: [PATCH 0897/1399] Attach NOTES - LeetHub From bc1f602952f6d4b3c7de4d81bcaeccb9909b4b58 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 29 Apr 2024 07:54:21 +0530 Subject: [PATCH 0898/1399] Time: 181 ms (9.95%), Space: 91.5 MB (12.57%) - LeetHub From 848eb53c7a8f2a43553669c5aab60b81c0706dbc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 29 Apr 2024 08:37:57 +0530 Subject: [PATCH 0899/1399] Create README - LeetHub --- 0148-sort-list/README.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 0148-sort-list/README.md diff --git a/0148-sort-list/README.md b/0148-sort-list/README.md new file mode 100644 index 00000000..e56bc9c9 --- /dev/null +++ b/0148-sort-list/README.md @@ -0,0 +1,32 @@ +

148. Sort List

Medium


Given the head of a linked list, return the list after sorting it in ascending order.

+ +

 

+

Example 1:

+ +
Input: head = [4,2,1,3]
+Output: [1,2,3,4]
+
+ +

Example 2:

+ +
Input: head = [-1,5,3,4,0]
+Output: [-1,0,3,4,5]
+
+ +

Example 3:

+ +
Input: head = []
+Output: []
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the list is in the range [0, 5 * 104].
  • +
  • -105 <= Node.val <= 105
  • +
+ +

 

+

Follow up: Can you sort the linked list in O(n logn) time and O(1) memory (i.e. constant space)?

+
\ No newline at end of file From 1186f6b16297f63a0146f03d03bc0709f276317e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 29 Apr 2024 08:38:01 +0530 Subject: [PATCH 0900/1399] Time: 118 ms (66.56%), Space: 56.8 MB (63.53%) - LeetHub --- 0148-sort-list/0148-sort-list.cpp | 55 +++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 0148-sort-list/0148-sort-list.cpp diff --git a/0148-sort-list/0148-sort-list.cpp b/0148-sort-list/0148-sort-list.cpp new file mode 100644 index 00000000..65a81cfc --- /dev/null +++ b/0148-sort-list/0148-sort-list.cpp @@ -0,0 +1,55 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + + ListNode* merge(ListNode* l1, ListNode* l2) + { + if(!l1) + return l2; + + if(!l2) + return l1; + + if(l1->val < l2->val) + { + l1->next = merge(l1->next, l2); + return l1; + } + else + { + l2->next = merge(l1, l2->next); + return l2; + } + } + + ListNode* sortList(ListNode* head) { + + if(!head or !head->next) + return head; + + ListNode* slow = head, *fast = head, *prev = nullptr; + + while(fast and fast->next) + { + prev = slow; + slow = slow->next; + fast = fast->next->next; + } + + prev->next = nullptr; + + ListNode* l1 = sortList(head); + ListNode* l2 = sortList(slow); + + return merge(l1, l2); + } +}; \ No newline at end of file From ae216a4d64a88e30c4c33473f250a05d8de5f878 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 29 Apr 2024 09:04:26 +0530 Subject: [PATCH 0901/1399] Attach NOTES - LeetHub From b38698d27ab03e5b48874f670130523e0ec7f862 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 29 Apr 2024 09:04:29 +0530 Subject: [PATCH 0902/1399] Time: 11 ms (65.39%), Space: 22.1 MB (40.22%) - LeetHub --- .../0543-diameter-of-binary-tree.cpp | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/0543-diameter-of-binary-tree/0543-diameter-of-binary-tree.cpp b/0543-diameter-of-binary-tree/0543-diameter-of-binary-tree.cpp index c381414a..e64aec71 100644 --- a/0543-diameter-of-binary-tree/0543-diameter-of-binary-tree.cpp +++ b/0543-diameter-of-binary-tree/0543-diameter-of-binary-tree.cpp @@ -12,22 +12,23 @@ class Solution { public: - int helper(TreeNode* root, int& maxi) + int helper(TreeNode* root, int& ans) { if(!root) return 0; + + int left = helper(root->left, ans); + int right = helper(root->right, ans); + + ans = max(ans, left + right); - int left = helper(root->left, maxi); - int right = helper(root->right, maxi); - - maxi = max(maxi, left + right); - return 1 + max(left, right); + return 1 + max(left, right); } int diameterOfBinaryTree(TreeNode* root) { - int maxi = 0; - helper(root, maxi); - return maxi; + int ans = 0; + helper(root, ans); + return ans; } }; \ No newline at end of file From c7242b8735765c936418139103b7692f87e4cb30 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 29 Apr 2024 09:04:52 +0530 Subject: [PATCH 0903/1399] Attach NOTES - LeetHub From 7f04e1a4faa345218a386a00cf5d4614bb1c76f6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 29 Apr 2024 09:04:55 +0530 Subject: [PATCH 0904/1399] Time: 11 ms (65.39%), Space: 22.1 MB (40.22%) - LeetHub From 3072a32c9631610d721eaf66d7260c5a01404d19 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 29 Apr 2024 09:50:01 +0530 Subject: [PATCH 0905/1399] Create README - LeetHub --- 0912-sort-an-array/README.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 0912-sort-an-array/README.md diff --git a/0912-sort-an-array/README.md b/0912-sort-an-array/README.md new file mode 100644 index 00000000..9b50c617 --- /dev/null +++ b/0912-sort-an-array/README.md @@ -0,0 +1,27 @@ +

912. Sort an Array

Medium


Given an array of integers nums, sort the array in ascending order and return it.

+ +

You must solve the problem without using any built-in functions in O(nlog(n)) time complexity and with the smallest space complexity possible.

+ +

 

+

Example 1:

+ +
Input: nums = [5,2,3,1]
+Output: [1,2,3,5]
+Explanation: After sorting the array, the positions of some numbers are not changed (for example, 2 and 3), while the positions of other numbers are changed (for example, 1 and 5).
+
+ +

Example 2:

+ +
Input: nums = [5,1,1,2,0,0]
+Output: [0,0,1,1,2,5]
+Explanation: Note that the values of nums are not necessairly unique.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 5 * 104
  • +
  • -5 * 104 <= nums[i] <= 5 * 104
  • +
+
\ No newline at end of file From 4cf0a37005c73932affaeb120e698012cc6ab8c4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 29 Apr 2024 10:15:09 +0530 Subject: [PATCH 0906/1399] Create README - LeetHub --- 0009-palindrome-number/README.md | 33 ++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 0009-palindrome-number/README.md diff --git a/0009-palindrome-number/README.md b/0009-palindrome-number/README.md new file mode 100644 index 00000000..135519db --- /dev/null +++ b/0009-palindrome-number/README.md @@ -0,0 +1,33 @@ +

9. Palindrome Number

Easy


Given an integer x, return true if x is a palindrome, and false otherwise.

+ +

 

+

Example 1:

+ +
Input: x = 121
+Output: true
+Explanation: 121 reads as 121 from left to right and from right to left.
+
+ +

Example 2:

+ +
Input: x = -121
+Output: false
+Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
+
+ +

Example 3:

+ +
Input: x = 10
+Output: false
+Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
+
+ +

 

+

Constraints:

+ +
    +
  • -231 <= x <= 231 - 1
  • +
+ +

 

+Follow up: Could you solve it without converting the integer to a string?
\ No newline at end of file From d60c320f226997d2b525ec903bbe772cb94f0992 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 29 Apr 2024 10:15:12 +0530 Subject: [PATCH 0907/1399] Time: 4 ms (84.53%), Space: 8.1 MB (96.47%) - LeetHub --- .../0009-palindrome-number.cpp | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 0009-palindrome-number/0009-palindrome-number.cpp diff --git a/0009-palindrome-number/0009-palindrome-number.cpp b/0009-palindrome-number/0009-palindrome-number.cpp new file mode 100644 index 00000000..f6243bdb --- /dev/null +++ b/0009-palindrome-number/0009-palindrome-number.cpp @@ -0,0 +1,24 @@ +class Solution { +public: + bool isPalindrome(int x) { + + if(x < 0) + return false; + + int curr = 0, num = x; + + while(num > 0) + { + int rem = num % 10; + num /= 10; + + if(curr > INT_MAX/10 or curr == INT_MAX/10 and rem > INT_MAX%10) + return false; + + curr = (curr * 10) + rem; + } + cout< Date: Mon, 29 Apr 2024 10:15:19 +0530 Subject: [PATCH 0908/1399] Attach NOTES - LeetHub --- 0009-palindrome-number/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0009-palindrome-number/NOTES.md diff --git a/0009-palindrome-number/NOTES.md b/0009-palindrome-number/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0009-palindrome-number/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From ad2da2847292567f8703f6eae4af8246d93344d2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 29 Apr 2024 10:15:23 +0530 Subject: [PATCH 0909/1399] Time: 12 ms (34.42%), Space: 8.1 MB (79.20%) - LeetHub --- 0009-palindrome-number/0009-palindrome-number.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/0009-palindrome-number/0009-palindrome-number.cpp b/0009-palindrome-number/0009-palindrome-number.cpp index f6243bdb..a34a0ab8 100644 --- a/0009-palindrome-number/0009-palindrome-number.cpp +++ b/0009-palindrome-number/0009-palindrome-number.cpp @@ -17,7 +17,7 @@ class Solution { curr = (curr * 10) + rem; } - cout< Date: Mon, 29 Apr 2024 10:17:48 +0530 Subject: [PATCH 0910/1399] Attach NOTES - LeetHub From c8b502b47b867b4283384ad6498d61bf02956f7b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 29 Apr 2024 10:17:52 +0530 Subject: [PATCH 0911/1399] Time: 12 ms (34.42%), Space: 8.1 MB (79.20%) - LeetHub From 8ac70c338061b8658309ef4825525144d2a7a959 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 29 Apr 2024 10:51:10 +0530 Subject: [PATCH 0912/1399] Attach NOTES - LeetHub From d534925840b0f83bdefcb9b1d20ac8854d0aef9a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 29 Apr 2024 10:51:13 +0530 Subject: [PATCH 0913/1399] Time: 11 ms (42.84%), Space: 8.1 MB (79.20%) - LeetHub --- .../0009-palindrome-number.cpp | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/0009-palindrome-number/0009-palindrome-number.cpp b/0009-palindrome-number/0009-palindrome-number.cpp index a34a0ab8..0d8a867c 100644 --- a/0009-palindrome-number/0009-palindrome-number.cpp +++ b/0009-palindrome-number/0009-palindrome-number.cpp @@ -2,23 +2,17 @@ class Solution { public: bool isPalindrome(int x) { - if(x < 0) + if(x < 0 or (x % 10 == 0 and x != 0)) return false; - int curr = 0, num = x; + int rev = 0; - while(num > 0) + while(rev < x) { - int rem = num % 10; - num /= 10; - - if(curr > INT_MAX/10 or curr == INT_MAX/10 and rem > INT_MAX%10) - return false; - - curr = (curr * 10) + rem; + rev = (rev * 10) + (x%10); + x /= 10; } - - return curr == x; + return (rev == x or rev/10 == x); } }; \ No newline at end of file From 50df3e309a16b531a1c59347c70314b6b57b2480 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 30 Apr 2024 22:55:28 +0530 Subject: [PATCH 0914/1399] Create README - LeetHub --- 1915-number-of-wonderful-substrings/README.md | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 1915-number-of-wonderful-substrings/README.md diff --git a/1915-number-of-wonderful-substrings/README.md b/1915-number-of-wonderful-substrings/README.md new file mode 100644 index 00000000..c19d9e1f --- /dev/null +++ b/1915-number-of-wonderful-substrings/README.md @@ -0,0 +1,54 @@ +

1915. Number of Wonderful Substrings

Medium


A wonderful string is a string where at most one letter appears an odd number of times.

+ +
    +
  • For example, "ccjjc" and "abab" are wonderful, but "ab" is not.
  • +
+ +

Given a string word that consists of the first ten lowercase English letters ('a' through 'j'), return the number of wonderful non-empty substrings in word. If the same substring appears multiple times in word, then count each occurrence separately.

+ +

A substring is a contiguous sequence of characters in a string.

+ +

 

+

Example 1:

+ +
Input: word = "aba"
+Output: 4
+Explanation: The four wonderful substrings are underlined below:
+- "aba" -> "a"
+- "aba" -> "b"
+- "aba" -> "a"
+- "aba" -> "aba"
+
+ +

Example 2:

+ +
Input: word = "aabb"
+Output: 9
+Explanation: The nine wonderful substrings are underlined below:
+- "aabb" -> "a"
+- "aabb" -> "aa"
+- "aabb" -> "aab"
+- "aabb" -> "aabb"
+- "aabb" -> "a"
+- "aabb" -> "abb"
+- "aabb" -> "b"
+- "aabb" -> "bb"
+- "aabb" -> "b"
+
+ +

Example 3:

+ +
Input: word = "he"
+Output: 2
+Explanation: The two wonderful substrings are underlined below:
+- "he" -> "h"
+- "he" -> "e"
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= word.length <= 105
  • +
  • word consists of lowercase English letters from 'a' to 'j'.
  • +
\ No newline at end of file From 81e2413ca6d98f07b0924895cfbb37a6f3109a06 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 30 Apr 2024 22:55:31 +0530 Subject: [PATCH 0915/1399] Time: 53 ms (68.86%), Space: 16.6 MB (59.88%) - LeetHub --- .../1915-number-of-wonderful-substrings.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 1915-number-of-wonderful-substrings/1915-number-of-wonderful-substrings.cpp diff --git a/1915-number-of-wonderful-substrings/1915-number-of-wonderful-substrings.cpp b/1915-number-of-wonderful-substrings/1915-number-of-wonderful-substrings.cpp new file mode 100644 index 00000000..f89c9d77 --- /dev/null +++ b/1915-number-of-wonderful-substrings/1915-number-of-wonderful-substrings.cpp @@ -0,0 +1,15 @@ +class Solution { +public: + long long wonderfulSubstrings(string word) { + + long cnt[1024] = { 1 }, mask = 0, res = 0; + for (auto ch : word) { + mask ^= 1 << (ch - 'a'); + res += cnt[mask]; + for (auto n = 0; n < 10; ++n) + res += cnt[mask ^ (1 << n)]; + ++cnt[mask]; + } + return res; + } +}; \ No newline at end of file From b2b9c731a0b1dd2213e6cd74199a4d5e760c4195 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 1 May 2024 19:52:40 +0530 Subject: [PATCH 0916/1399] Attach NOTES - LeetHub --- 2000-reverse-prefix-of-word/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2000-reverse-prefix-of-word/NOTES.md diff --git a/2000-reverse-prefix-of-word/NOTES.md b/2000-reverse-prefix-of-word/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2000-reverse-prefix-of-word/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 7d0813cf38959fa30ae7cd88a64a9564adb22883 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 1 May 2024 19:52:43 +0530 Subject: [PATCH 0917/1399] Time: 3 ms (42.21%), Space: 7.6 MB (26.16%) - LeetHub --- .../2000-reverse-prefix-of-word.cpp | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 2000-reverse-prefix-of-word/2000-reverse-prefix-of-word.cpp diff --git a/2000-reverse-prefix-of-word/2000-reverse-prefix-of-word.cpp b/2000-reverse-prefix-of-word/2000-reverse-prefix-of-word.cpp new file mode 100644 index 00000000..b6cb4401 --- /dev/null +++ b/2000-reverse-prefix-of-word/2000-reverse-prefix-of-word.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + string reversePrefix(string word, char ch) { + + int n = word.size(); + string pref, ans; + bool isfound = false; + + for(int i = 0; i < n; ++i) + { + if(!isfound) + pref += word[i]; + else + ans += word[i]; + if(word[i] == ch) + isfound = true; + } + + if(!isfound) + return pref; + + reverse(pref.begin(), pref.end()); + + return pref + ans; + + } +}; \ No newline at end of file From c6559e8e4e3c126dc165e4b3ef943040da997db5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 1 May 2024 19:54:49 +0530 Subject: [PATCH 0918/1399] Attach NOTES - LeetHub From 88bd18d6ae2b061bb8ef1aca56dde5b3639f8233 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 2 May 2024 08:11:40 +0530 Subject: [PATCH 0919/1399] Create README - LeetHub --- .../README.md | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 2441-largest-positive-integer-that-exists-with-its-negative/README.md diff --git a/2441-largest-positive-integer-that-exists-with-its-negative/README.md b/2441-largest-positive-integer-that-exists-with-its-negative/README.md new file mode 100644 index 00000000..6a824672 --- /dev/null +++ b/2441-largest-positive-integer-that-exists-with-its-negative/README.md @@ -0,0 +1,35 @@ +

2441. Largest Positive Integer That Exists With Its Negative

Easy


Given an integer array nums that does not contain any zeros, find the largest positive integer k such that -k also exists in the array.

+ +

Return the positive integer k. If there is no such integer, return -1.

+ +

 

+

Example 1:

+ +
Input: nums = [-1,2,-3,3]
+Output: 3
+Explanation: 3 is the only valid k we can find in the array.
+
+ +

Example 2:

+ +
Input: nums = [-1,10,6,7,-7,1]
+Output: 7
+Explanation: Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
+
+ +

Example 3:

+ +
Input: nums = [-10,8,6,7,-2,-3]
+Output: -1
+Explanation: There is no a single valid k, we return -1.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 1000
  • +
  • -1000 <= nums[i] <= 1000
  • +
  • nums[i] != 0
  • +
+
\ No newline at end of file From 4885dcac83bae027693648e7d9fae5c2029db295 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 2 May 2024 08:11:44 +0530 Subject: [PATCH 0920/1399] Time: 25 ms (29.95%), Space: 25.6 MB (39.47%) - LeetHub --- ...-integer-that-exists-with-its-negative.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 2441-largest-positive-integer-that-exists-with-its-negative/2441-largest-positive-integer-that-exists-with-its-negative.cpp diff --git a/2441-largest-positive-integer-that-exists-with-its-negative/2441-largest-positive-integer-that-exists-with-its-negative.cpp b/2441-largest-positive-integer-that-exists-with-its-negative/2441-largest-positive-integer-that-exists-with-its-negative.cpp new file mode 100644 index 00000000..03f004a3 --- /dev/null +++ b/2441-largest-positive-integer-that-exists-with-its-negative/2441-largest-positive-integer-that-exists-with-its-negative.cpp @@ -0,0 +1,19 @@ +class Solution { +public: + int findMaxK(vector& nums) { + + set st(nums.begin(), nums.end()); + int ans = -1; + + for(auto& ele : nums) + { + if(ele > 0) + { + if(st.count(-ele)) + ans = max(ans, ele); + } + } + + return ans; + } +}; \ No newline at end of file From 638881edf0911d72c7865c9692c5f7a6c897bb97 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 2 May 2024 08:12:43 +0530 Subject: [PATCH 0921/1399] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2441-largest-positive-integer-that-exists-with-its-negative/NOTES.md diff --git a/2441-largest-positive-integer-that-exists-with-its-negative/NOTES.md b/2441-largest-positive-integer-that-exists-with-its-negative/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2441-largest-positive-integer-that-exists-with-its-negative/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 136c80e7a7f742ac8e3e9d2769373e7cde149e6e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 3 May 2024 23:22:59 +0530 Subject: [PATCH 0922/1399] Create README - LeetHub --- 0165-compare-version-numbers/README.md | 49 ++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 0165-compare-version-numbers/README.md diff --git a/0165-compare-version-numbers/README.md b/0165-compare-version-numbers/README.md new file mode 100644 index 00000000..2e07f72d --- /dev/null +++ b/0165-compare-version-numbers/README.md @@ -0,0 +1,49 @@ +

165. Compare Version Numbers

Medium


Given two version numbers, version1 and version2, compare them.

+ +
    +
+ +

Version numbers consist of one or more revisions joined by a dot '.'. Each revision consists of digits and may contain leading zeros. Every revision contains at least one character. Revisions are 0-indexed from left to right, with the leftmost revision being revision 0, the next revision being revision 1, and so on. For example 2.5.33 and 0.1 are valid version numbers.

+ +

To compare version numbers, compare their revisions in left-to-right order. Revisions are compared using their integer value ignoring any leading zeros. This means that revisions 1 and 001 are considered equal. If a version number does not specify a revision at an index, then treat the revision as 0. For example, version 1.0 is less than version 1.1 because their revision 0s are the same, but their revision 1s are 0 and 1 respectively, and 0 < 1.

+ +

Return the following:

+ +
    +
  • If version1 < version2, return -1.
  • +
  • If version1 > version2, return 1.
  • +
  • Otherwise, return 0.
  • +
+ +

 

+

Example 1:

+ +
Input: version1 = "1.01", version2 = "1.001"
+Output: 0
+Explanation: Ignoring leading zeroes, both "01" and "001" represent the same integer "1".
+
+ +

Example 2:

+ +
Input: version1 = "1.0", version2 = "1.0.0"
+Output: 0
+Explanation: version1 does not specify revision 2, which means it is treated as "0".
+
+ +

Example 3:

+ +
Input: version1 = "0.1", version2 = "1.1"
+Output: -1
+Explanation: version1's revision 0 is "0", while version2's revision 0 is "1". 0 < 1, so version1 < version2.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= version1.length, version2.length <= 500
  • +
  • version1 and version2 only contain digits and '.'.
  • +
  • version1 and version2 are valid version numbers.
  • +
  • All the given revisions in version1 and version2 can be stored in a 32-bit integer.
  • +
+
\ No newline at end of file From 74226ab51f406c9edf52b1cc72ae6d85abacd266 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 3 May 2024 23:23:03 +0530 Subject: [PATCH 0923/1399] Time: 0 ms (100.00%), Space: 7.3 MB (77.28%) - LeetHub --- .../0165-compare-version-numbers.cpp | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 0165-compare-version-numbers/0165-compare-version-numbers.cpp diff --git a/0165-compare-version-numbers/0165-compare-version-numbers.cpp b/0165-compare-version-numbers/0165-compare-version-numbers.cpp new file mode 100644 index 00000000..14ab26a7 --- /dev/null +++ b/0165-compare-version-numbers/0165-compare-version-numbers.cpp @@ -0,0 +1,35 @@ +class Solution { +public: + int compareVersion(string version1, string version2) { + + int i = 0, j= 0, n1 = version1.length(), n2 = version2.length(), num1 = 0, num2 = 0; + + while(i < n1 || j < n2) + { + while(i < n1 && version1[i] != '.') + { + num1 = num1*10 + (version1[i] - '0'); + ++i; + } + while(j < n2 && version2[j] != '.') + { + num2 = num2 * 10 + (version2[j] - '0'); + ++j; + } + + + if(num1 > num2) + return 1; + + if(num1 < num2) + return -1; + + ++i, ++j; + + num1 = 0, num2 = 0; + + } + return 0; + + } +}; \ No newline at end of file From c91a44690c91a44ad139a5a3fcae0046e34a4912 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 4 May 2024 23:19:38 +0530 Subject: [PATCH 0924/1399] Attach NOTES - LeetHub --- 0881-boats-to-save-people/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0881-boats-to-save-people/NOTES.md diff --git a/0881-boats-to-save-people/NOTES.md b/0881-boats-to-save-people/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0881-boats-to-save-people/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From eb222f038221e6e00d51cad8e08678f3a2be58f2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 4 May 2024 23:19:41 +0530 Subject: [PATCH 0925/1399] Time: 52 ms (82.35%), Space: 45.5 MB (46.86%) - LeetHub --- .../0881-boats-to-save-people.cpp | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 0881-boats-to-save-people/0881-boats-to-save-people.cpp diff --git a/0881-boats-to-save-people/0881-boats-to-save-people.cpp b/0881-boats-to-save-people/0881-boats-to-save-people.cpp new file mode 100644 index 00000000..9bdda773 --- /dev/null +++ b/0881-boats-to-save-people/0881-boats-to-save-people.cpp @@ -0,0 +1,25 @@ +class Solution { +public: + int numRescueBoats(vector& people, int limit) { + + int cnt = 0; + + sort(people.begin(),people.end()); + + int n = people.size(); + int start = 0, end = n-1; + + while(start <= end) + { + if(people[start] + people[end] <= limit) + { + ++start, --end; + } + else + --end; + ++cnt; + } + + return cnt; + } +}; \ No newline at end of file From 220fa3f95c378a1abf85321c4ec2b61d1a15800a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 5 May 2024 10:32:38 +0530 Subject: [PATCH 0926/1399] Create README - LeetHub --- .../README.md | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 3138-minimum-length-of-anagram-concatenation/README.md diff --git a/3138-minimum-length-of-anagram-concatenation/README.md b/3138-minimum-length-of-anagram-concatenation/README.md new file mode 100644 index 00000000..5d4c31a9 --- /dev/null +++ b/3138-minimum-length-of-anagram-concatenation/README.md @@ -0,0 +1,39 @@ +

3138. Minimum Length of Anagram Concatenation

Medium


You are given a string s, which is known to be a concatenation of anagrams of some string t.

+ +

Return the minimum possible length of the string t.

+ +

An anagram is a word or phrase formed by rearranging the letters of a word or phrase, typically using all the original letters exactly once.

+ +

 

+

Example 1:

+ +
+

Input: s = "abba"

+ +

Output: 2

+ +

Explanation:

+ +

One possible string t could be "ba".

+
+ +

Example 2:

+ +
+

Input: s = "cdef"

+ +

Output: 4

+ +

Explanation:

+ +

One possible string t could be "cdef", notice that t can be equal to s.

+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 105
  • +
  • s consist only of lowercase English letters.
  • +
+
\ No newline at end of file From b9005d86e4ccb10e447c30830d5522dd9e7f5878 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 5 May 2024 10:32:41 +0530 Subject: [PATCH 0927/1399] Time: 35 ms (33.33%), Space: 13.1 MB (33.33%) - LeetHub --- ...inimum-length-of-anagram-concatenation.cpp | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 3138-minimum-length-of-anagram-concatenation/3138-minimum-length-of-anagram-concatenation.cpp diff --git a/3138-minimum-length-of-anagram-concatenation/3138-minimum-length-of-anagram-concatenation.cpp b/3138-minimum-length-of-anagram-concatenation/3138-minimum-length-of-anagram-concatenation.cpp new file mode 100644 index 00000000..083fb850 --- /dev/null +++ b/3138-minimum-length-of-anagram-concatenation/3138-minimum-length-of-anagram-concatenation.cpp @@ -0,0 +1,38 @@ +class Solution { +public: + int minAnagramLength(string s) { + + int n = s.size(); + + map mp; + + for(auto&ch : s) + ++mp[ch]; + + int minFreq = INT_MAX; + + for(auto&[_, e] : mp) + minFreq = min(minFreq, e); + + int ans = n; + + for(int i = 1; i <= minFreq; ++i) + { + bool ok = true; + int len = 0; + + for(auto&[_, e] : mp) + { + ok &= (e % i == 0); + len += (e / i); + } + + if(ok) + { + ans = len; + } + } + + return ans; + } +}; \ No newline at end of file From 5259a030bb6a138e6cb7bb746a244642be271565 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 5 May 2024 10:51:30 +0530 Subject: [PATCH 0928/1399] Attach NOTES - LeetHub --- 3138-minimum-length-of-anagram-concatenation/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 3138-minimum-length-of-anagram-concatenation/NOTES.md diff --git a/3138-minimum-length-of-anagram-concatenation/NOTES.md b/3138-minimum-length-of-anagram-concatenation/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/3138-minimum-length-of-anagram-concatenation/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From c7838ffee239c464282a45c4f50f5e13082349f8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 5 May 2024 10:51:33 +0530 Subject: [PATCH 0929/1399] Time: 35 ms (33.33%), Space: 13.1 MB (33.33%) - LeetHub From 866bb6d8e1b0733e1199fa9eb23c22ed52a26661 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 5 May 2024 10:53:47 +0530 Subject: [PATCH 0930/1399] Attach NOTES - LeetHub From 8472cf020e2471b506f75cff50c40fecd4f021f8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 5 May 2024 10:53:50 +0530 Subject: [PATCH 0931/1399] Time: 35 ms (33.33%), Space: 13.1 MB (33.33%) - LeetHub From 86aa8ebd7c8273515e6fb1ca4f6a7033d5b69778 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 5 May 2024 18:30:47 +0530 Subject: [PATCH 0932/1399] Create README - LeetHub --- .../README.md | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 3137-minimum-number-of-operations-to-make-word-k-periodic/README.md diff --git a/3137-minimum-number-of-operations-to-make-word-k-periodic/README.md b/3137-minimum-number-of-operations-to-make-word-k-periodic/README.md new file mode 100644 index 00000000..cc5c44d8 --- /dev/null +++ b/3137-minimum-number-of-operations-to-make-word-k-periodic/README.md @@ -0,0 +1,84 @@ +

3137. Minimum Number of Operations to Make Word K-Periodic

Medium


You are given a string word of size n, and an integer k such that k divides n.

+ +

In one operation, you can pick any two indices i and j, that are divisible by k, then replace the substring of length k starting at i with the substring of length k starting at j. That is, replace the substring word[i..i + k - 1] with the substring word[j..j + k - 1].

+ +

Return the minimum number of operations required to make word k-periodic.

+ +

We say that word is k-periodic if there is some string s of length k such that word can be obtained by concatenating s an arbitrary number of times. For example, if word == “ababab”, then word is 2-periodic for s = "ab".

+ +

 

+

Example 1:

+ +
+

Input: word = "leetcodeleet", k = 4

+ +

Output: 1

+ +

Explanation:

+ +

We can obtain a 4-periodic string by picking i = 4 and j = 0. After this operation, word becomes equal to "leetleetleet".

+
+ +

Example 2:

+ +
+

Input: word = "leetcoleet", k = 2

+ +

Output: 3

+ +

Explanation:

+ +

We can obtain a 2-periodic string by applying the operations in the table below.

+ + + + + + + + + + + + + + + + + + + + + + + + +
ijword
02etetcoleet
40etetetleet
60etetetetet
+
+ +
+
 
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n == word.length <= 105
  • +
  • 1 <= k <= word.length
  • +
  • k divides word.length.
  • +
  • word consists only of lowercase English letters.
  • +
+
\ No newline at end of file From 5b726352d46025dd9390ef8ee45f4740dc8397a1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 5 May 2024 18:30:50 +0530 Subject: [PATCH 0933/1399] Time: 191 ms (25.00%), Space: 34.8 MB (12.50%) - LeetHub --- ...-of-operations-to-make-word-k-periodic.cpp | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 3137-minimum-number-of-operations-to-make-word-k-periodic/3137-minimum-number-of-operations-to-make-word-k-periodic.cpp diff --git a/3137-minimum-number-of-operations-to-make-word-k-periodic/3137-minimum-number-of-operations-to-make-word-k-periodic.cpp b/3137-minimum-number-of-operations-to-make-word-k-periodic/3137-minimum-number-of-operations-to-make-word-k-periodic.cpp new file mode 100644 index 00000000..dbb896c6 --- /dev/null +++ b/3137-minimum-number-of-operations-to-make-word-k-periodic/3137-minimum-number-of-operations-to-make-word-k-periodic.cpp @@ -0,0 +1,28 @@ +class Solution { +public: + int minimumOperationsToMakeKPeriodic(string word, int k) { + + int n = word.size(); + + vector factors; + + for(int i = 0; i < n; ++i) + { + if(i == 0 or i % k == 0) + factors.push_back(i); + } + + map mp; + + int maxi = 0; + + for(auto& idx : factors) + { + string st = word.substr(idx, k); + ++mp[st]; + maxi = max(maxi, mp[st]); + } + + return (int)factors.size() - maxi; + } +}; \ No newline at end of file From 3d314f650892ea2aec26b26ab951f126db6c3a0b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 5 May 2024 18:31:46 +0530 Subject: [PATCH 0934/1399] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 3137-minimum-number-of-operations-to-make-word-k-periodic/NOTES.md diff --git a/3137-minimum-number-of-operations-to-make-word-k-periodic/NOTES.md b/3137-minimum-number-of-operations-to-make-word-k-periodic/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/3137-minimum-number-of-operations-to-make-word-k-periodic/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From e7aea0418ceb1d408b33e7f5384c7ccc4429ecb7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 5 May 2024 18:31:49 +0530 Subject: [PATCH 0935/1399] Time: 191 ms (25.00%), Space: 34.8 MB (12.50%) - LeetHub From cda1484bfe31c6d358ee9eabe26c6e00fcb5c3ed Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 5 May 2024 18:33:27 +0530 Subject: [PATCH 0936/1399] Attach NOTES - LeetHub From 1c2c594486fc31ebfe381482a3eee32d999d059b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 5 May 2024 21:08:08 +0530 Subject: [PATCH 0937/1399] Attach NOTES - LeetHub --- 3139-minimum-cost-to-equalize-array/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 3139-minimum-cost-to-equalize-array/NOTES.md diff --git a/3139-minimum-cost-to-equalize-array/NOTES.md b/3139-minimum-cost-to-equalize-array/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/3139-minimum-cost-to-equalize-array/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From e84c6f8bfb24b7f88f380a800fd632b4142111f3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 5 May 2024 21:08:12 +0530 Subject: [PATCH 0938/1399] Time: 201 ms (75.00%), Space: 92.6 MB (75.00%) - LeetHub --- .../3139-minimum-cost-to-equalize-array.cpp | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 3139-minimum-cost-to-equalize-array/3139-minimum-cost-to-equalize-array.cpp diff --git a/3139-minimum-cost-to-equalize-array/3139-minimum-cost-to-equalize-array.cpp b/3139-minimum-cost-to-equalize-array/3139-minimum-cost-to-equalize-array.cpp new file mode 100644 index 00000000..a3da90c1 --- /dev/null +++ b/3139-minimum-cost-to-equalize-array/3139-minimum-cost-to-equalize-array.cpp @@ -0,0 +1,52 @@ +using lli = long long; + +class Solution { +public: + int minCostToEqualizeArray(vector& nums, int cost1, int cost2) { + + int n = nums.size(); + + lli maxi = 0; + lli mini = 1e9; + lli ans = 1e18; + lli sum = 0; + const int mod = 1e9+7; + + for(auto& ele : nums) + { + maxi = max(maxi, 1LL * ele); + mini = min(mini, 1LL * ele); + sum += ele; + } + + for(int curMax = maxi; curMax <= 2 * maxi; ++curMax) + { + lli total = (n * 1LL* curMax) - sum; + lli curr = 0; + if(2 * cost1 <= cost2) + { + curr = (total * cost1); + ans = min(ans, curr); + } + else + { + lli maxDiff = curMax - mini; + lli pair = total - maxDiff; + + if(pair < maxDiff) + { + lli nonPair = maxDiff - pair; + curr = (nonPair * cost1) + (pair * cost2); + ans = min(ans, curr); + continue; + } + + curr = ((total/2) * cost2) + ((total%2) * cost1); + + ans = min(ans, curr); + } + } + + return ans%mod; + } +}; \ No newline at end of file From dcef3460a4129ea8151ef686221490d5027bf127 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 5 May 2024 21:16:26 +0530 Subject: [PATCH 0939/1399] Create README - LeetHub --- 0237-delete-node-in-a-linked-list/README.md | 48 +++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 0237-delete-node-in-a-linked-list/README.md diff --git a/0237-delete-node-in-a-linked-list/README.md b/0237-delete-node-in-a-linked-list/README.md new file mode 100644 index 00000000..3fc46f11 --- /dev/null +++ b/0237-delete-node-in-a-linked-list/README.md @@ -0,0 +1,48 @@ +

237. Delete Node in a Linked List

Medium


There is a singly-linked list head and we want to delete a node node in it.

+ +

You are given the node to be deleted node. You will not be given access to the first node of head.

+ +

All the values of the linked list are unique, and it is guaranteed that the given node node is not the last node in the linked list.

+ +

Delete the given node. Note that by deleting the node, we do not mean removing it from memory. We mean:

+ +
    +
  • The value of the given node should not exist in the linked list.
  • +
  • The number of nodes in the linked list should decrease by one.
  • +
  • All the values before node should be in the same order.
  • +
  • All the values after node should be in the same order.
  • +
+ +

Custom testing:

+ +
    +
  • For the input, you should provide the entire linked list head and the node to be given node. node should not be the last node of the list and should be an actual node in the list.
  • +
  • We will build the linked list and pass the node to your function.
  • +
  • The output will be the entire list after calling your function.
  • +
+ +

 

+

Example 1:

+ +
Input: head = [4,5,1,9], node = 5
+Output: [4,1,9]
+Explanation: You are given the second node with value 5, the linked list should become 4 -> 1 -> 9 after calling your function.
+
+ +

Example 2:

+ +
Input: head = [4,5,1,9], node = 1
+Output: [4,5,9]
+Explanation: You are given the third node with value 1, the linked list should become 4 -> 5 -> 9 after calling your function.
+
+ +

 

+

Constraints:

+ +
    +
  • The number of the nodes in the given list is in the range [2, 1000].
  • +
  • -1000 <= Node.val <= 1000
  • +
  • The value of each node in the list is unique.
  • +
  • The node to be deleted is in the list and is not a tail node.
  • +
+
\ No newline at end of file From 35a6a19b5ea43a77c58caaa5929d07d483e4d0e5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 5 May 2024 21:16:30 +0530 Subject: [PATCH 0940/1399] Time: 7 ms (37.88%), Space: 11.4 MB (43.54%) - LeetHub --- .../0237-delete-node-in-a-linked-list.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 0237-delete-node-in-a-linked-list/0237-delete-node-in-a-linked-list.cpp diff --git a/0237-delete-node-in-a-linked-list/0237-delete-node-in-a-linked-list.cpp b/0237-delete-node-in-a-linked-list/0237-delete-node-in-a-linked-list.cpp new file mode 100644 index 00000000..9a3c0cf0 --- /dev/null +++ b/0237-delete-node-in-a-linked-list/0237-delete-node-in-a-linked-list.cpp @@ -0,0 +1,16 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode(int x) : val(x), next(NULL) {} + * }; + */ +class Solution { +public: + void deleteNode(ListNode* node) { + + node->val = node->next->val; + node->next = node->next->next; + } +}; \ No newline at end of file From f1f336daa8cfa95ef6266382d639cb2f2e5702b8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 5 May 2024 21:17:24 +0530 Subject: [PATCH 0941/1399] Attach NOTES - LeetHub --- 0237-delete-node-in-a-linked-list/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0237-delete-node-in-a-linked-list/NOTES.md diff --git a/0237-delete-node-in-a-linked-list/NOTES.md b/0237-delete-node-in-a-linked-list/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0237-delete-node-in-a-linked-list/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From c0ef1f8951574ea39d0cbebdc96cf883f8f152c1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 5 May 2024 21:17:25 +0530 Subject: [PATCH 0942/1399] Attach NOTES - LeetHub From e8386485e3f169bd1929fef7096c9c3f71da0fdb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 5 May 2024 21:17:27 +0530 Subject: [PATCH 0943/1399] Time: 7 ms (37.88%), Space: 11.4 MB (43.54%) - LeetHub From 7f48d7f361f9f6316e0e2e5ad3517c4d300c4fe8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 7 May 2024 07:47:32 +0530 Subject: [PATCH 0944/1399] Create README - LeetHub --- .../README.md | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 2816-double-a-number-represented-as-a-linked-list/README.md diff --git a/2816-double-a-number-represented-as-a-linked-list/README.md b/2816-double-a-number-represented-as-a-linked-list/README.md new file mode 100644 index 00000000..57ca97b0 --- /dev/null +++ b/2816-double-a-number-represented-as-a-linked-list/README.md @@ -0,0 +1,28 @@ +

2816. Double a Number Represented as a Linked List

Medium


You are given the head of a non-empty linked list representing a non-negative integer without leading zeroes.

+ +

Return the head of the linked list after doubling it.

+ +

 

+

Example 1:

+ +
Input: head = [1,8,9]
+Output: [3,7,8]
+Explanation: The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378.
+
+ +

Example 2:

+ +
Input: head = [9,9,9]
+Output: [1,9,9,8]
+Explanation: The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998. 
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the list is in the range [1, 104]
  • +
  • 0 <= Node.val <= 9
  • +
  • The input is generated such that the list represents a number that does not have leading zeros, except the number 0 itself.
  • +
+
\ No newline at end of file From e056c4f35b997855765b2df2a0ec85d6eefe53e9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 7 May 2024 07:47:36 +0530 Subject: [PATCH 0945/1399] Time: 186 ms (40.89%), Space: 120.6 MB (87.76%) - LeetHub --- ...-a-number-represented-as-a-linked-list.cpp | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 2816-double-a-number-represented-as-a-linked-list/2816-double-a-number-represented-as-a-linked-list.cpp diff --git a/2816-double-a-number-represented-as-a-linked-list/2816-double-a-number-represented-as-a-linked-list.cpp b/2816-double-a-number-represented-as-a-linked-list/2816-double-a-number-represented-as-a-linked-list.cpp new file mode 100644 index 00000000..a10cd124 --- /dev/null +++ b/2816-double-a-number-represented-as-a-linked-list/2816-double-a-number-represented-as-a-linked-list.cpp @@ -0,0 +1,61 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + + ListNode* reverse(ListNode* head) + { + ListNode* prev = nullptr; + + while(head) + { + ListNode* nex = head->next; + head->next = prev; + prev = head; + head = nex; + } + return prev; + } + + ListNode* add(ListNode* head) + { + ListNode* temp = head, *prev = nullptr; + + int carry = 0; + + while(temp) + { + prev = temp; + int sum = 0; + sum += (2 * temp->val) + carry; + temp->val = (sum%10); + carry = sum / 10; + temp = temp->next; + } + + if(carry) + { + prev->next = new ListNode (carry); + } + + return head; + } + + ListNode* doubleIt(ListNode* head) { + + head = reverse(head); + + head = add(head); + + return reverse(head); + + } +}; \ No newline at end of file From 2e118d2757b90eb0e47af43a9cae2fb8f468057a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 7 May 2024 07:47:44 +0530 Subject: [PATCH 0946/1399] Attach NOTES - LeetHub --- 2816-double-a-number-represented-as-a-linked-list/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2816-double-a-number-represented-as-a-linked-list/NOTES.md diff --git a/2816-double-a-number-represented-as-a-linked-list/NOTES.md b/2816-double-a-number-represented-as-a-linked-list/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2816-double-a-number-represented-as-a-linked-list/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From b06dd75bd6b4b87db75b13afa5bf218f58b0db14 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 7 May 2024 07:47:47 +0530 Subject: [PATCH 0947/1399] Time: 186 ms (40.89%), Space: 120.6 MB (87.76%) - LeetHub From d6c144ff41b88f9f377e567dc87c3bc663b59045 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 7 May 2024 07:49:32 +0530 Subject: [PATCH 0948/1399] Attach NOTES - LeetHub From a5962b2b3f5d6ff35223845619433bd3768cc5db Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 7 May 2024 07:49:33 +0530 Subject: [PATCH 0949/1399] Attach NOTES - LeetHub From 1350e3243ab56078deacf247c5a59460fe880dc5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 7 May 2024 07:49:35 +0530 Subject: [PATCH 0950/1399] Time: 186 ms (40.89%), Space: 120.6 MB (87.76%) - LeetHub From 853e0f67e0407486e49d8d3804649c1a45950a0b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 7 May 2024 07:49:37 +0530 Subject: [PATCH 0951/1399] Time: 186 ms (40.89%), Space: 120.6 MB (87.76%) - LeetHub From bf8cbc18adbe307b39a47138e62cc3f28e71f5c4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 7 May 2024 07:49:38 +0530 Subject: [PATCH 0952/1399] Attach NOTES - LeetHub From c3ee43fc7660c4a9e4e2b9f52941c270bac7c51f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 7 May 2024 07:49:41 +0530 Subject: [PATCH 0953/1399] Time: 186 ms (40.89%), Space: 120.6 MB (87.76%) - LeetHub From 8548c1a12192c81eb06dd1ded84336edb76d7736 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 7 May 2024 07:51:55 +0530 Subject: [PATCH 0954/1399] Attach NOTES - LeetHub From a3af5132e7e85b9b83f4373e4b7d1aef213aca81 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 7 May 2024 08:07:02 +0530 Subject: [PATCH 0955/1399] Attach NOTES - LeetHub From 7cae6b4af6347a36743fe0d1dcaf9957bfea20a8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 7 May 2024 08:07:05 +0530 Subject: [PATCH 0956/1399] Time: 167 ms (77.26%), Space: 121.3 MB (41.17%) - LeetHub --- ...-a-number-represented-as-a-linked-list.cpp | 45 +++++-------------- 1 file changed, 10 insertions(+), 35 deletions(-) diff --git a/2816-double-a-number-represented-as-a-linked-list/2816-double-a-number-represented-as-a-linked-list.cpp b/2816-double-a-number-represented-as-a-linked-list/2816-double-a-number-represented-as-a-linked-list.cpp index a10cd124..d5ac5ea5 100644 --- a/2816-double-a-number-represented-as-a-linked-list/2816-double-a-number-represented-as-a-linked-list.cpp +++ b/2816-double-a-number-represented-as-a-linked-list/2816-double-a-number-represented-as-a-linked-list.cpp @@ -11,51 +11,26 @@ class Solution { public: - ListNode* reverse(ListNode* head) + int add(ListNode* head) { - ListNode* prev = nullptr; + if(!head) + return 0; - while(head) - { - ListNode* nex = head->next; - head->next = prev; - prev = head; - head = nex; - } - return prev; - } - - ListNode* add(ListNode* head) - { - ListNode* temp = head, *prev = nullptr; + int sum = (head->val * 2) + add(head->next); - int carry = 0; + head->val = sum % 10; - while(temp) - { - prev = temp; - int sum = 0; - sum += (2 * temp->val) + carry; - temp->val = (sum%10); - carry = sum / 10; - temp = temp->next; - } - - if(carry) - { - prev->next = new ListNode (carry); - } - - return head; + return sum / 10; } ListNode* doubleIt(ListNode* head) { - head = reverse(head); + int carry = add(head); - head = add(head); + if(carry) + head = new ListNode(carry, head); - return reverse(head); + return head; } }; \ No newline at end of file From 36ef78692f4b2d6fb524d7fdf46739abb3638016 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 9 May 2024 21:26:48 +0530 Subject: [PATCH 0957/1399] Create README - LeetHub --- .../README.md | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 3075-maximize-happiness-of-selected-children/README.md diff --git a/3075-maximize-happiness-of-selected-children/README.md b/3075-maximize-happiness-of-selected-children/README.md new file mode 100644 index 00000000..16567a73 --- /dev/null +++ b/3075-maximize-happiness-of-selected-children/README.md @@ -0,0 +1,47 @@ +

3075. Maximize Happiness of Selected Children

Medium


You are given an array happiness of length n, and a positive integer k.

+ +

There are n children standing in a queue, where the ith child has happiness value happiness[i]. You want to select k children from these n children in k turns.

+ +

In each turn, when you select a child, the happiness value of all the children that have not been selected till now decreases by 1. Note that the happiness value cannot become negative and gets decremented only if it is positive.

+ +

Return the maximum sum of the happiness values of the selected children you can achieve by selecting k children.

+ +

 

+

Example 1:

+ +
Input: happiness = [1,2,3], k = 2
+Output: 4
+Explanation: We can pick 2 children in the following way:
+- Pick the child with the happiness value == 3. The happiness value of the remaining children becomes [0,1].
+- Pick the child with the happiness value == 1. The happiness value of the remaining child becomes [0]. Note that the happiness value cannot become less than 0.
+The sum of the happiness values of the selected children is 3 + 1 = 4.
+
+ +

Example 2:

+ +
Input: happiness = [1,1,1,1], k = 2
+Output: 1
+Explanation: We can pick 2 children in the following way:
+- Pick any child with the happiness value == 1. The happiness value of the remaining children becomes [0,0,0].
+- Pick the child with the happiness value == 0. The happiness value of the remaining child becomes [0,0].
+The sum of the happiness values of the selected children is 1 + 0 = 1.
+
+ +

Example 3:

+ +
Input: happiness = [2,3,4,5], k = 1
+Output: 5
+Explanation: We can pick 1 child in the following way:
+- Pick the child with the happiness value == 5. The happiness value of the remaining children becomes [1,2,3].
+The sum of the happiness values of the selected children is 5.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n == happiness.length <= 2 * 105
  • +
  • 1 <= happiness[i] <= 108
  • +
  • 1 <= k <= n
  • +
+
\ No newline at end of file From d9e84f689545bea2cf4ecd38914d8e07ceebe177 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 9 May 2024 21:26:51 +0530 Subject: [PATCH 0958/1399] Time: 231 ms (10.07%), Space: 108.8 MB (23.42%) - LeetHub --- ...aximize-happiness-of-selected-children.cpp | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 3075-maximize-happiness-of-selected-children/3075-maximize-happiness-of-selected-children.cpp diff --git a/3075-maximize-happiness-of-selected-children/3075-maximize-happiness-of-selected-children.cpp b/3075-maximize-happiness-of-selected-children/3075-maximize-happiness-of-selected-children.cpp new file mode 100644 index 00000000..23cbcd93 --- /dev/null +++ b/3075-maximize-happiness-of-selected-children/3075-maximize-happiness-of-selected-children.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + long long maximumHappinessSum(vector& happiness, int k) { + + int n = happiness.size(); + + sort(happiness.rbegin(), happiness.rend()); + + int cap = 0; + long long ans = 0; + + for(int i = 0; i < n, i < k; ++i, ++cap) + { + if(happiness[i] - cap > 0) + ans += (happiness[i] - cap); + } + + return ans; + } +}; \ No newline at end of file From b51a62167892ec8a2d55972eb05bb4faf689a7c9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 10 May 2024 23:02:50 +0530 Subject: [PATCH 0959/1399] Attach NOTES - LeetHub --- 0786-k-th-smallest-prime-fraction/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0786-k-th-smallest-prime-fraction/NOTES.md diff --git a/0786-k-th-smallest-prime-fraction/NOTES.md b/0786-k-th-smallest-prime-fraction/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0786-k-th-smallest-prime-fraction/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 3fdbf7faf4e802b4b7ed220002a7af7b60e2ac3c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 10 May 2024 23:02:53 +0530 Subject: [PATCH 0960/1399] Time: 618 ms (18.96%), Space: 228.2 MB (11.99%) - LeetHub --- .../0786-k-th-smallest-prime-fraction.cpp | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 0786-k-th-smallest-prime-fraction/0786-k-th-smallest-prime-fraction.cpp diff --git a/0786-k-th-smallest-prime-fraction/0786-k-th-smallest-prime-fraction.cpp b/0786-k-th-smallest-prime-fraction/0786-k-th-smallest-prime-fraction.cpp new file mode 100644 index 00000000..7c02db31 --- /dev/null +++ b/0786-k-th-smallest-prime-fraction/0786-k-th-smallest-prime-fraction.cpp @@ -0,0 +1,24 @@ +class Solution { +public: + vector kthSmallestPrimeFraction(vector& arr, int k) { + + vector>> res; + + int n = arr.size(); + + for(int i = 0; i < n; ++i) + { + for(int j = n-1; j >= 0; --j) + { + if(i != j) + { + res.push_back({((double)arr[i]/(double)arr[j]),{arr[i], arr[j]}}); + } + } + } + + sort(res.begin(), res.end()); + + return {res[k-1].second.first, res[k-1].second.second}; + } +}; \ No newline at end of file From ba312f377a057c7348d32ef341eb749daa8464fd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 10 May 2024 23:05:08 +0530 Subject: [PATCH 0961/1399] Attach NOTES - LeetHub From dbe6f38ca784bf4b2f9d5fb96c268e04eb5affcb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 10 May 2024 23:05:12 +0530 Subject: [PATCH 0962/1399] Time: 618 ms (18.96%), Space: 228.2 MB (11.99%) - LeetHub From 2d5e274387c4c27185ab3a2ed4f0e46933dbe046 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 11 May 2024 22:58:06 +0530 Subject: [PATCH 0963/1399] Create README - LeetHub --- 0857-minimum-cost-to-hire-k-workers/README.md | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 0857-minimum-cost-to-hire-k-workers/README.md diff --git a/0857-minimum-cost-to-hire-k-workers/README.md b/0857-minimum-cost-to-hire-k-workers/README.md new file mode 100644 index 00000000..d251e14b --- /dev/null +++ b/0857-minimum-cost-to-hire-k-workers/README.md @@ -0,0 +1,35 @@ +

857. Minimum Cost to Hire K Workers

Hard


There are n workers. You are given two integer arrays quality and wage where quality[i] is the quality of the ith worker and wage[i] is the minimum wage expectation for the ith worker.

+ +

We want to hire exactly k workers to form a paid group. To hire a group of k workers, we must pay them according to the following rules:

+ +
    +
  1. Every worker in the paid group must be paid at least their minimum wage expectation.
  2. +
  3. In the group, each worker's pay must be directly proportional to their quality. This means if a worker’s quality is double that of another worker in the group, then they must be paid twice as much as the other worker.
  4. +
+ +

Given the integer k, return the least amount of money needed to form a paid group satisfying the above conditions. Answers within 10-5 of the actual answer will be accepted.

+ +

 

+

Example 1:

+ +
Input: quality = [10,20,5], wage = [70,50,30], k = 2
+Output: 105.00000
+Explanation: We pay 70 to 0th worker and 35 to 2nd worker.
+
+ +

Example 2:

+ +
Input: quality = [3,1,10,10,1], wage = [4,8,2,2,7], k = 3
+Output: 30.66667
+Explanation: We pay 4 to 0th worker, 13.33333 to 2nd and 3rd workers separately.
+
+ +

 

+

Constraints:

+ +
    +
  • n == quality.length == wage.length
  • +
  • 1 <= k <= n <= 104
  • +
  • 1 <= quality[i], wage[i] <= 104
  • +
+
\ No newline at end of file From 41597f76c8cee47aa46d70afd447bd076ce9aae5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 11 May 2024 22:58:06 +0530 Subject: [PATCH 0964/1399] Attach NOTES - LeetHub --- 0857-minimum-cost-to-hire-k-workers/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0857-minimum-cost-to-hire-k-workers/NOTES.md diff --git a/0857-minimum-cost-to-hire-k-workers/NOTES.md b/0857-minimum-cost-to-hire-k-workers/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0857-minimum-cost-to-hire-k-workers/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 883ef23e6fe4cc1abc516d5d95a832e3430ab017 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 11 May 2024 22:58:09 +0530 Subject: [PATCH 0965/1399] Time: 24 ms (74.02%), Space: 26.5 MB (38.83%) - LeetHub --- .../0857-minimum-cost-to-hire-k-workers.cpp | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 0857-minimum-cost-to-hire-k-workers/0857-minimum-cost-to-hire-k-workers.cpp diff --git a/0857-minimum-cost-to-hire-k-workers/0857-minimum-cost-to-hire-k-workers.cpp b/0857-minimum-cost-to-hire-k-workers/0857-minimum-cost-to-hire-k-workers.cpp new file mode 100644 index 00000000..38566929 --- /dev/null +++ b/0857-minimum-cost-to-hire-k-workers/0857-minimum-cost-to-hire-k-workers.cpp @@ -0,0 +1,37 @@ +class Solution { +public: + double mincostToHireWorkers(vector& quality, vector& wage, int k) { + vector> v; + int n=quality.size(); + for(int i=0;i pq; + + double ans=DBL_MAX; + int q=0; + for(int i=0;iv[i].second){ + q -= pq.top(); + pq.pop(); + + pq.push(v[i].second); + q += v[i].second; + } + } + if(pq.size()==k){ + double cost = q*v[i].first; + if(ans > cost) ans=cost; + } + } + return ans; + } +}; \ No newline at end of file From 2361be412c06dd0a3820563f54b4d2eac9c2f131 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 12 May 2024 21:32:15 +0530 Subject: [PATCH 0966/1399] Create README - LeetHub --- .../README.md | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 2373-largest-local-values-in-a-matrix/README.md diff --git a/2373-largest-local-values-in-a-matrix/README.md b/2373-largest-local-values-in-a-matrix/README.md new file mode 100644 index 00000000..b70175ae --- /dev/null +++ b/2373-largest-local-values-in-a-matrix/README.md @@ -0,0 +1,36 @@ +

2373. Largest Local Values in a Matrix

Easy


You are given an n x n integer matrix grid.

+ +

Generate an integer matrix maxLocal of size (n - 2) x (n - 2) such that:

+ +
    +
  • maxLocal[i][j] is equal to the largest value of the 3 x 3 matrix in grid centered around row i + 1 and column j + 1.
  • +
+ +

In other words, we want to find the largest value in every contiguous 3 x 3 matrix in grid.

+ +

Return the generated matrix.

+ +

 

+

Example 1:

+ +
Input: grid = [[9,9,8,1],[5,6,2,6],[8,2,6,4],[6,2,2,2]]
+Output: [[9,9],[8,6]]
+Explanation: The diagram above shows the original matrix and the generated matrix.
+Notice that each value in the generated matrix corresponds to the largest value of a contiguous 3 x 3 matrix in grid.
+ +

Example 2:

+ +
Input: grid = [[1,1,1,1,1],[1,1,1,1,1],[1,1,2,1,1],[1,1,1,1,1],[1,1,1,1,1]]
+Output: [[2,2,2],[2,2,2],[2,2,2]]
+Explanation: Notice that the 2 is contained within every contiguous 3 x 3 matrix in grid.
+
+ +

 

+

Constraints:

+ +
    +
  • n == grid.length == grid[i].length
  • +
  • 3 <= n <= 100
  • +
  • 1 <= grid[i][j] <= 100
  • +
+
\ No newline at end of file From baef01b19bde571a472171185a6a6d4a40722e0b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 12 May 2024 21:32:19 +0530 Subject: [PATCH 0967/1399] Time: 12 ms (37.90%), Space: 14.5 MB (15.10%) - LeetHub --- .../2373-largest-local-values-in-a-matrix.cpp | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 2373-largest-local-values-in-a-matrix/2373-largest-local-values-in-a-matrix.cpp diff --git a/2373-largest-local-values-in-a-matrix/2373-largest-local-values-in-a-matrix.cpp b/2373-largest-local-values-in-a-matrix/2373-largest-local-values-in-a-matrix.cpp new file mode 100644 index 00000000..1620dba7 --- /dev/null +++ b/2373-largest-local-values-in-a-matrix/2373-largest-local-values-in-a-matrix.cpp @@ -0,0 +1,29 @@ +class Solution { +public: + vector> largestLocal(vector>& grid) { + + int n = grid.size(), m = grid[0].size(); + + vector> ans; + + for(int i = 0; i < n-2; ++i) + { + vector curr; + for(int j = 0; j < n-2; ++j) + { + int maxi = -1; + for(int row = i; row < i+3; ++row) + { + for(int col = j; col < j+3; ++col) + { + maxi = max(maxi, grid[row][col]); + } + } + curr.push_back(maxi); + } + ans.push_back(curr); + } + + return ans; + } +}; \ No newline at end of file From 44a74a33decd37934bf938742c761cfa8b4ea66b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 14 May 2024 23:30:25 +0530 Subject: [PATCH 0968/1399] Create README - LeetHub --- 1219-path-with-maximum-gold/README.md | 48 +++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 1219-path-with-maximum-gold/README.md diff --git a/1219-path-with-maximum-gold/README.md b/1219-path-with-maximum-gold/README.md new file mode 100644 index 00000000..399cf0bd --- /dev/null +++ b/1219-path-with-maximum-gold/README.md @@ -0,0 +1,48 @@ +

1219. Path with Maximum Gold

Medium


In a gold mine grid of size m x n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty.

+ +

Return the maximum amount of gold you can collect under the conditions:

+ +
    +
  • Every time you are located in a cell you will collect all the gold in that cell.
  • +
  • From your position, you can walk one step to the left, right, up, or down.
  • +
  • You can't visit the same cell more than once.
  • +
  • Never visit a cell with 0 gold.
  • +
  • You can start and stop collecting gold from any position in the grid that has some gold.
  • +
+ +

 

+

Example 1:

+ +
Input: grid = [[0,6,0],[5,8,7],[0,9,0]]
+Output: 24
+Explanation:
+[[0,6,0],
+ [5,8,7],
+ [0,9,0]]
+Path to get the maximum gold, 9 -> 8 -> 7.
+
+ +

Example 2:

+ +
Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
+Output: 28
+Explanation:
+[[1,0,7],
+ [2,0,6],
+ [3,4,5],
+ [0,3,0],
+ [9,0,20]]
+Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.
+
+ +

 

+

Constraints:

+ +
    +
  • m == grid.length
  • +
  • n == grid[i].length
  • +
  • 1 <= m, n <= 15
  • +
  • 0 <= grid[i][j] <= 100
  • +
  • There are at most 25 cells containing gold.
  • +
+
\ No newline at end of file From e976664449e2765741fa4899983a829cc43fb7da Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 14 May 2024 23:30:30 +0530 Subject: [PATCH 0969/1399] Time: 288 ms (24.60%), Space: 9.9 MB (44.61%) - LeetHub --- .../1219-path-with-maximum-gold.cpp | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 1219-path-with-maximum-gold/1219-path-with-maximum-gold.cpp diff --git a/1219-path-with-maximum-gold/1219-path-with-maximum-gold.cpp b/1219-path-with-maximum-gold/1219-path-with-maximum-gold.cpp new file mode 100644 index 00000000..d38be720 --- /dev/null +++ b/1219-path-with-maximum-gold/1219-path-with-maximum-gold.cpp @@ -0,0 +1,57 @@ +class Solution { +public: + + vector dx = {-1, +1, 0, 0}; + vector dy = {0, 0, +1, -1}; + + bool isValid(int x, int y, int n, int m) + { + return (x >= 0 and y >= 0 and x < n and y < m); + } + + int dfs(int i, int j, int n, int m, vector>& grid, vector>& visited){ + + int maxAns = 0; + + visited[i][j] = true; + + for(int k = 0; k < 4; ++k) + { + int newx = dx[k] + i; + int newy = dy[k] + j; + + if(isValid(newx, newy, n, m) and grid[newx][newy] != 0 and !visited[newx][newy]) + { + int ans = grid[newx][newy] + dfs(newx, newy, n, m, grid, visited); + maxAns = max(maxAns, ans); + } + } + + visited[i][j] = false; + + return maxAns; + } + + int getMaximumGold(vector>& grid) { + + int n = grid.size(), m = grid[0].size(); + + int ans = 0; + + vector> visited(n, vector(m, false)); + + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < m; ++j) + { + if(grid[i][j] != 0) + { + int currVal = grid[i][j] + dfs(i, j, n, m, grid, visited); + ans = max(ans, currVal); + } + } + } + + return ans; + } +}; \ No newline at end of file From e885198921def8ffc8e2b47183a5c379cfc12897 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 15 May 2024 23:02:15 +0530 Subject: [PATCH 0970/1399] Attach NOTES - LeetHub --- 2812-find-the-safest-path-in-a-grid/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2812-find-the-safest-path-in-a-grid/NOTES.md diff --git a/2812-find-the-safest-path-in-a-grid/NOTES.md b/2812-find-the-safest-path-in-a-grid/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2812-find-the-safest-path-in-a-grid/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 7070ace99d6f408fad72164d78d6b8240b4688e1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 May 2024 21:43:27 +0530 Subject: [PATCH 0971/1399] Create README - LeetHub --- 2331-evaluate-boolean-binary-tree/README.md | 48 +++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 2331-evaluate-boolean-binary-tree/README.md diff --git a/2331-evaluate-boolean-binary-tree/README.md b/2331-evaluate-boolean-binary-tree/README.md new file mode 100644 index 00000000..d563a4b9 --- /dev/null +++ b/2331-evaluate-boolean-binary-tree/README.md @@ -0,0 +1,48 @@ +

2331. Evaluate Boolean Binary Tree

Easy


You are given the root of a full binary tree with the following properties:

+ +
    +
  • Leaf nodes have either the value 0 or 1, where 0 represents False and 1 represents True.
  • +
  • Non-leaf nodes have either the value 2 or 3, where 2 represents the boolean OR and 3 represents the boolean AND.
  • +
+ +

The evaluation of a node is as follows:

+ +
    +
  • If the node is a leaf node, the evaluation is the value of the node, i.e. True or False.
  • +
  • Otherwise, evaluate the node's two children and apply the boolean operation of its value with the children's evaluations.
  • +
+ +

Return the boolean result of evaluating the root node.

+ +

A full binary tree is a binary tree where each node has either 0 or 2 children.

+ +

A leaf node is a node that has zero children.

+ +

 

+

Example 1:

+ +
Input: root = [2,1,3,null,null,0,1]
+Output: true
+Explanation: The above diagram illustrates the evaluation process.
+The AND node evaluates to False AND True = False.
+The OR node evaluates to True OR False = True.
+The root node evaluates to True, so we return true.
+ +

Example 2:

+ +
Input: root = [0]
+Output: false
+Explanation: The root node is a leaf node and it evaluates to false, so we return false.
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is in the range [1, 1000].
  • +
  • 0 <= Node.val <= 3
  • +
  • Every node has either 0 or 2 children.
  • +
  • Leaf nodes have a value of 0 or 1.
  • +
  • Non-leaf nodes have a value of 2 or 3.
  • +
+
\ No newline at end of file From 7e26773e6bc30dd247ccb45cbda6369d45373f05 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 May 2024 21:43:30 +0530 Subject: [PATCH 0972/1399] Time: 7 ms (87.42%), Space: 17.3 MB (64.84%) - LeetHub --- .../2331-evaluate-boolean-binary-tree.cpp | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 2331-evaluate-boolean-binary-tree/2331-evaluate-boolean-binary-tree.cpp diff --git a/2331-evaluate-boolean-binary-tree/2331-evaluate-boolean-binary-tree.cpp b/2331-evaluate-boolean-binary-tree/2331-evaluate-boolean-binary-tree.cpp new file mode 100644 index 00000000..4b1f5932 --- /dev/null +++ b/2331-evaluate-boolean-binary-tree/2331-evaluate-boolean-binary-tree.cpp @@ -0,0 +1,32 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + + bool helper(TreeNode* root) + { + if(!root->left and !root->right) + { + return root->val; + } + + bool left = helper(root->left); + bool right = helper(root->right); + + return (root->val == 2 ? left | right : left & right); + } + + bool evaluateTree(TreeNode* root) { + + return helper(root); + } +}; \ No newline at end of file From 0651e9d4b21216d11251540a37e84ef266280b74 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 May 2024 21:44:16 +0530 Subject: [PATCH 0973/1399] Attach NOTES - LeetHub --- 2331-evaluate-boolean-binary-tree/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2331-evaluate-boolean-binary-tree/NOTES.md diff --git a/2331-evaluate-boolean-binary-tree/NOTES.md b/2331-evaluate-boolean-binary-tree/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2331-evaluate-boolean-binary-tree/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From ee22e4b66015a06124cb263d1cc457ae746db36f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 May 2024 21:44:19 +0530 Subject: [PATCH 0974/1399] Time: 7 ms (87.42%), Space: 17.3 MB (64.84%) - LeetHub From 178a4ba28cb554e201fd2e6c7fba7d5abace7cf0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 May 2024 21:44:35 +0530 Subject: [PATCH 0975/1399] Attach NOTES - LeetHub From cb62db803f96f71985c61f6ea41fee0d032a45b2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 May 2024 21:44:38 +0530 Subject: [PATCH 0976/1399] Time: 7 ms (87.42%), Space: 17.3 MB (64.84%) - LeetHub From 9db98f30e461883bcb0265b544a1bbb4a5a57f93 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 May 2024 23:50:38 +0530 Subject: [PATCH 0977/1399] Attach NOTES - LeetHub --- 0721-accounts-merge/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0721-accounts-merge/NOTES.md diff --git a/0721-accounts-merge/NOTES.md b/0721-accounts-merge/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0721-accounts-merge/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From e28c89f662354e53e0ff2d0220b778a472e09105 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 16 May 2024 23:51:57 +0530 Subject: [PATCH 0978/1399] Attach NOTES - LeetHub From 7dff6fab0f7a1f01bf672b9e8595ca6db947ab44 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 17 May 2024 08:58:00 +0530 Subject: [PATCH 0979/1399] Create README - LeetHub --- .../README.md | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 1325-delete-leaves-with-a-given-value/README.md diff --git a/1325-delete-leaves-with-a-given-value/README.md b/1325-delete-leaves-with-a-given-value/README.md new file mode 100644 index 00000000..23f321db --- /dev/null +++ b/1325-delete-leaves-with-a-given-value/README.md @@ -0,0 +1,40 @@ +

1325. Delete Leaves With a Given Value

Medium


Given a binary tree root and an integer target, delete all the leaf nodes with value target.

+ +

Note that once you delete a leaf node with value target, if its parent node becomes a leaf node and has the value target, it should also be deleted (you need to continue doing that until you cannot).

+ +

 

+

Example 1:

+ +

+ +
Input: root = [1,2,3,2,null,2,4], target = 2
+Output: [1,null,3,null,4]
+Explanation: Leaf nodes in green with value (target = 2) are removed (Picture in left). 
+After removing, new nodes become leaf nodes with value (target = 2) (Picture in center).
+
+ +

Example 2:

+ +

+ +
Input: root = [1,3,3,3,2], target = 3
+Output: [1,3,null,null,2]
+
+ +

Example 3:

+ +

+ +
Input: root = [1,2,null,2,null,2], target = 2
+Output: [1]
+Explanation: Leaf nodes in green with value (target = 2) are removed at each step.
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is in the range [1, 3000].
  • +
  • 1 <= Node.val, target <= 1000
  • +
+
\ No newline at end of file From b7b682b7a23ac5ad87daceb364509af0bc026f10 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 17 May 2024 08:58:03 +0530 Subject: [PATCH 0980/1399] Time: 16 ms (15.19%), Space: 21.4 MB (9.62%) - LeetHub --- .../1325-delete-leaves-with-a-given-value.cpp | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 1325-delete-leaves-with-a-given-value/1325-delete-leaves-with-a-given-value.cpp diff --git a/1325-delete-leaves-with-a-given-value/1325-delete-leaves-with-a-given-value.cpp b/1325-delete-leaves-with-a-given-value/1325-delete-leaves-with-a-given-value.cpp new file mode 100644 index 00000000..16914d74 --- /dev/null +++ b/1325-delete-leaves-with-a-given-value/1325-delete-leaves-with-a-given-value.cpp @@ -0,0 +1,42 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + + TreeNode* helper(TreeNode* root, int target) + { + if(!root) + return nullptr; + + TreeNode* left = helper(root->left, target); + TreeNode* right = helper(root->right, target); + + if(!left) + root->left = nullptr; + + if(!right) + root->right = nullptr; + + if(!left and !right and root->val == target) + { + return nullptr; + } + return root; + } + + + TreeNode* removeLeafNodes(TreeNode* root, int target) { + + return helper(root, target); + + } +}; \ No newline at end of file From f79b48e6afbfc86d3c30985044c690bc6f8033de Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 May 2024 09:22:12 +0530 Subject: [PATCH 0981/1399] Create README - LeetHub --- .../README.md | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 0979-distribute-coins-in-binary-tree/README.md diff --git a/0979-distribute-coins-in-binary-tree/README.md b/0979-distribute-coins-in-binary-tree/README.md new file mode 100644 index 00000000..e7b0415a --- /dev/null +++ b/0979-distribute-coins-in-binary-tree/README.md @@ -0,0 +1,31 @@ +

979. Distribute Coins in Binary Tree

Medium


You are given the root of a binary tree with n nodes where each node in the tree has node.val coins. There are n coins in total throughout the whole tree.

+ +

In one move, we may choose two adjacent nodes and move one coin from one node to another. A move may be from parent to child, or from child to parent.

+ +

Return the minimum number of moves required to make every node have exactly one coin.

+ +

 

+

Example 1:

+ +
Input: root = [3,0,0]
+Output: 2
+Explanation: From the root of the tree, we move one coin to its left child, and one coin to its right child.
+
+ +

Example 2:

+ +
Input: root = [0,3,0]
+Output: 3
+Explanation: From the left child of the root, we move two coins to the root [taking two moves]. Then, we move one coin from the root of the tree to the right child.
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is n.
  • +
  • 1 <= n <= 100
  • +
  • 0 <= Node.val <= n
  • +
  • The sum of all Node.val is n.
  • +
+
\ No newline at end of file From 4be46f6d9062b293aec0a4876d8596cefeefbc63 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 May 2024 09:22:16 +0530 Subject: [PATCH 0982/1399] Time: 4 ms (63.21%), Space: 15.6 MB (58.63%) - LeetHub --- .../0979-distribute-coins-in-binary-tree.cpp | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 0979-distribute-coins-in-binary-tree/0979-distribute-coins-in-binary-tree.cpp diff --git a/0979-distribute-coins-in-binary-tree/0979-distribute-coins-in-binary-tree.cpp b/0979-distribute-coins-in-binary-tree/0979-distribute-coins-in-binary-tree.cpp new file mode 100644 index 00000000..a8764161 --- /dev/null +++ b/0979-distribute-coins-in-binary-tree/0979-distribute-coins-in-binary-tree.cpp @@ -0,0 +1,37 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + + int helper(TreeNode* root, int& moves) + { + if(!root) + return 0; + + int left = helper(root->left, moves); + int right = helper(root->right, moves); + + moves += (abs(left) + abs(right)); + + return (left + right + root->val - 1); + } + + int distributeCoins(TreeNode* root) { + + int moves = 0; + + helper(root, moves); + + return moves; + + } +}; \ No newline at end of file From 5df6b155da3a23d3b4b911c065071f91f50290c6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 May 2024 21:56:46 +0530 Subject: [PATCH 0983/1399] Attach NOTES - LeetHub --- 1757-recyclable-and-low-fat-products/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1757-recyclable-and-low-fat-products/NOTES.md diff --git a/1757-recyclable-and-low-fat-products/NOTES.md b/1757-recyclable-and-low-fat-products/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1757-recyclable-and-low-fat-products/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From f68986ea87b4eb488db50035bb2a92402dc0399e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 May 2024 21:56:48 +0530 Subject: [PATCH 0984/1399] Time: 1210 ms (29.77%), Space: 0B (100.00%) - LeetHub --- .../1757-recyclable-and-low-fat-products.sql | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 1757-recyclable-and-low-fat-products/1757-recyclable-and-low-fat-products.sql diff --git a/1757-recyclable-and-low-fat-products/1757-recyclable-and-low-fat-products.sql b/1757-recyclable-and-low-fat-products/1757-recyclable-and-low-fat-products.sql new file mode 100644 index 00000000..68d567d4 --- /dev/null +++ b/1757-recyclable-and-low-fat-products/1757-recyclable-and-low-fat-products.sql @@ -0,0 +1,3 @@ +# Write your MySQL query statement below + +select product_id from Products where low_fats = 'Y' and recyclable = 'Y'; From 48ae66effcf7e6fae759943f89868e7c3d8b1379 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 May 2024 21:58:53 +0530 Subject: [PATCH 0985/1399] Attach NOTES - LeetHub From 10d412500e0e5f50e6f8d277b3abaf4ffc5c7762 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 May 2024 21:58:56 +0530 Subject: [PATCH 0986/1399] Time: 1210 ms (29.77%), Space: 0B (100.00%) - LeetHub From f60e98b9e797093973e753bea4ec00d5ff469643 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 May 2024 22:17:06 +0530 Subject: [PATCH 0987/1399] Create README - LeetHub --- 0584-find-customer-referee/README.md | 47 ++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 0584-find-customer-referee/README.md diff --git a/0584-find-customer-referee/README.md b/0584-find-customer-referee/README.md new file mode 100644 index 00000000..372be3cf --- /dev/null +++ b/0584-find-customer-referee/README.md @@ -0,0 +1,47 @@ +

584. Find Customer Referee

Easy


Table: Customer

+ +
+-------------+---------+
+| Column Name | Type    |
++-------------+---------+
+| id          | int     |
+| name        | varchar |
+| referee_id  | int     |
++-------------+---------+
+In SQL, id is the primary key column for this table.
+Each row of this table indicates the id of a customer, their name, and the id of the customer who referred them.
+
+ +

 

+ +

Find the names of the customer that are not referred by the customer with id = 2.

+ +

Return the result table in any order.

+ +

The result format is in the following example.

+ +

 

+

Example 1:

+ +
Input: 
+Customer table:
++----+------+------------+
+| id | name | referee_id |
++----+------+------------+
+| 1  | Will | null       |
+| 2  | Jane | null       |
+| 3  | Alex | 2          |
+| 4  | Bill | null       |
+| 5  | Zack | 1          |
+| 6  | Mark | 2          |
++----+------+------------+
+Output: 
++------+
+| name |
++------+
+| Will |
+| Jane |
+| Bill |
+| Zack |
++------+
+
+
\ No newline at end of file From fadf872e7e7c632859de8db7bee500a25e35e2e8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 May 2024 22:17:07 +0530 Subject: [PATCH 0988/1399] Attach NOTES - LeetHub --- 0584-find-customer-referee/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0584-find-customer-referee/NOTES.md diff --git a/0584-find-customer-referee/NOTES.md b/0584-find-customer-referee/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0584-find-customer-referee/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 647c7d5d318ceb247c50b4204678b44723a41bfa Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 May 2024 22:17:10 +0530 Subject: [PATCH 0989/1399] Time: 1239 ms (17.89%), Space: 0B (100.00%) - LeetHub --- 0584-find-customer-referee/0584-find-customer-referee.sql | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 0584-find-customer-referee/0584-find-customer-referee.sql diff --git a/0584-find-customer-referee/0584-find-customer-referee.sql b/0584-find-customer-referee/0584-find-customer-referee.sql new file mode 100644 index 00000000..7f77caac --- /dev/null +++ b/0584-find-customer-referee/0584-find-customer-referee.sql @@ -0,0 +1,3 @@ +# Write your MySQL query statement below + +select name from Customer where referee_id is null or referee_id != 2; \ No newline at end of file From 7fd2131b17c7c8f80727330f2682aa9597b18829 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 May 2024 22:17:22 +0530 Subject: [PATCH 0990/1399] Attach NOTES - LeetHub From 626c3600b7441e25abbe5bd85f278d1c0bfbe702 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 May 2024 22:17:26 +0530 Subject: [PATCH 0991/1399] Time: 1239 ms (17.89%), Space: 0B (100.00%) - LeetHub From 43fa845ec5cfeb8f696de398904bbf9947cec243 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 May 2024 22:20:04 +0530 Subject: [PATCH 0992/1399] Attach NOTES - LeetHub From a99a3a4537f47021df5b6784a214bd6f6af38a0a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 May 2024 22:20:06 +0530 Subject: [PATCH 0993/1399] Time: 1239 ms (17.89%), Space: 0B (100.00%) - LeetHub From 002f2e49487f85c795b7b6a68b2d9c96a5f277c5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 May 2024 22:31:42 +0530 Subject: [PATCH 0994/1399] Attach NOTES - LeetHub --- 0595-big-countries/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0595-big-countries/NOTES.md diff --git a/0595-big-countries/NOTES.md b/0595-big-countries/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0595-big-countries/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 1d1b7a47cbea600cef0147f0e9102dc3f5f2a1ef Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 May 2024 22:31:45 +0530 Subject: [PATCH 0995/1399] Time: 472 ms (69.65%), Space: 0B (100.00%) - LeetHub --- 0595-big-countries/0595-big-countries.sql | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 0595-big-countries/0595-big-countries.sql diff --git a/0595-big-countries/0595-big-countries.sql b/0595-big-countries/0595-big-countries.sql new file mode 100644 index 00000000..4ade2dde --- /dev/null +++ b/0595-big-countries/0595-big-countries.sql @@ -0,0 +1,3 @@ +# Write your MySQL query statement below + +select name, population, area from World where area >= 3000000 or population >= 25000000 order by name asc; \ No newline at end of file From dab30d1755c35e97d489658190de1c9af29cfe08 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 May 2024 22:32:19 +0530 Subject: [PATCH 0996/1399] Attach NOTES - LeetHub From 7844fe208902f225ecbf6895b747b47793115aac Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 May 2024 22:35:53 +0530 Subject: [PATCH 0997/1399] Create README - LeetHub --- 1148-article-views-i/README.md | 48 ++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 1148-article-views-i/README.md diff --git a/1148-article-views-i/README.md b/1148-article-views-i/README.md new file mode 100644 index 00000000..fa2d7609 --- /dev/null +++ b/1148-article-views-i/README.md @@ -0,0 +1,48 @@ +

1148. Article Views I

Easy


Table: Views

+ +
+---------------+---------+
+| Column Name   | Type    |
++---------------+---------+
+| article_id    | int     |
+| author_id     | int     |
+| viewer_id     | int     |
+| view_date     | date    |
++---------------+---------+
+There is no primary key (column with unique values) for this table, the table may have duplicate rows.
+Each row of this table indicates that some viewer viewed an article (written by some author) on some date. 
+Note that equal author_id and viewer_id indicate the same person.
+
+ +

 

+ +

Write a solution to find all the authors that viewed at least one of their own articles.

+ +

Return the result table sorted by id in ascending order.

+ +

The result format is in the following example.

+ +

 

+

Example 1:

+ +
Input: 
+Views table:
++------------+-----------+-----------+------------+
+| article_id | author_id | viewer_id | view_date  |
++------------+-----------+-----------+------------+
+| 1          | 3         | 5         | 2019-08-01 |
+| 1          | 3         | 6         | 2019-08-02 |
+| 2          | 7         | 7         | 2019-08-01 |
+| 2          | 7         | 6         | 2019-08-02 |
+| 4          | 7         | 1         | 2019-07-22 |
+| 3          | 4         | 4         | 2019-07-21 |
+| 3          | 4         | 4         | 2019-07-21 |
++------------+-----------+-----------+------------+
+Output: 
++------+
+| id   |
++------+
+| 4    |
+| 7    |
++------+
+
+
\ No newline at end of file From 5309c31f13b1038574fac38f69c62056a121b660 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 May 2024 22:35:57 +0530 Subject: [PATCH 0998/1399] Time: 808 ms (52.73%), Space: 0B (100.00%) - LeetHub --- 1148-article-views-i/1148-article-views-i.sql | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 1148-article-views-i/1148-article-views-i.sql diff --git a/1148-article-views-i/1148-article-views-i.sql b/1148-article-views-i/1148-article-views-i.sql new file mode 100644 index 00000000..f6d15e95 --- /dev/null +++ b/1148-article-views-i/1148-article-views-i.sql @@ -0,0 +1,3 @@ +# Write your MySQL query statement below + +select distinct(author_id) as id from Views where author_id = viewer_id order by author_id asc; \ No newline at end of file From f8ff36bcaa25e45cf0c6ac55e58f1e9da3210d96 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 May 2024 22:36:11 +0530 Subject: [PATCH 0999/1399] Attach NOTES - LeetHub --- 1148-article-views-i/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1148-article-views-i/NOTES.md diff --git a/1148-article-views-i/NOTES.md b/1148-article-views-i/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1148-article-views-i/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 4c5d44270ee54bef3dbbdbad44c118b16ad3ea52 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 May 2024 22:36:15 +0530 Subject: [PATCH 1000/1399] Time: 808 ms (52.73%), Space: 0B (100.00%) - LeetHub From d05423539dcf1d56693a3a60047c6e5943aad1d7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 May 2024 22:40:13 +0530 Subject: [PATCH 1001/1399] Create README - LeetHub --- 1683-invalid-tweets/README.md | 42 +++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 1683-invalid-tweets/README.md diff --git a/1683-invalid-tweets/README.md b/1683-invalid-tweets/README.md new file mode 100644 index 00000000..ffab3320 --- /dev/null +++ b/1683-invalid-tweets/README.md @@ -0,0 +1,42 @@ +

1683. Invalid Tweets

Easy


Table: Tweets

+ +
+----------------+---------+
+| Column Name    | Type    |
++----------------+---------+
+| tweet_id       | int     |
+| content        | varchar |
++----------------+---------+
+tweet_id is the primary key (column with unique values) for this table.
+This table contains all the tweets in a social media app.
+
+ +

 

+ +

Write a solution to find the IDs of the invalid tweets. The tweet is invalid if the number of characters used in the content of the tweet is strictly greater than 15.

+ +

Return the result table in any order.

+ +

The result format is in the following example.

+ +

 

+

Example 1:

+ +
Input: 
+Tweets table:
++----------+----------------------------------+
+| tweet_id | content                          |
++----------+----------------------------------+
+| 1        | Vote for Biden                   |
+| 2        | Let us make America great again! |
++----------+----------------------------------+
+Output: 
++----------+
+| tweet_id |
++----------+
+| 2        |
++----------+
+Explanation: 
+Tweet 1 has length = 14. It is a valid tweet.
+Tweet 2 has length = 32. It is an invalid tweet.
+
+
\ No newline at end of file From d1317f83e6989396a5c040923cd275e217b62a2a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 May 2024 22:40:13 +0530 Subject: [PATCH 1002/1399] Attach NOTES - LeetHub --- 1683-invalid-tweets/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1683-invalid-tweets/NOTES.md diff --git a/1683-invalid-tweets/NOTES.md b/1683-invalid-tweets/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1683-invalid-tweets/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 7712cd189277db815220f57f518f8c43b1e91683 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 May 2024 22:40:17 +0530 Subject: [PATCH 1003/1399] Time: 974 ms (89.05%), Space: 0B (100.00%) - LeetHub --- 1683-invalid-tweets/1683-invalid-tweets.sql | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 1683-invalid-tweets/1683-invalid-tweets.sql diff --git a/1683-invalid-tweets/1683-invalid-tweets.sql b/1683-invalid-tweets/1683-invalid-tweets.sql new file mode 100644 index 00000000..e8274c99 --- /dev/null +++ b/1683-invalid-tweets/1683-invalid-tweets.sql @@ -0,0 +1,5 @@ +# Write your MySQL query statement below + +select tweet_id from Tweets +where length(content) > 15; + From a0e0ec0a3a54e485b91d9b0a243217e2168c25cd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 May 2024 22:41:27 +0530 Subject: [PATCH 1004/1399] Attach NOTES - LeetHub From 3d72bcb5565c1a014ebd3ec2ce0a57a0706cff28 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 May 2024 22:41:45 +0530 Subject: [PATCH 1005/1399] Attach NOTES - LeetHub From 3c0531c4e0f077710b4c1e5aa86b21cd1ff4a3d4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 May 2024 22:41:48 +0530 Subject: [PATCH 1006/1399] Time: 1068 ms (71.40%), Space: 0B (100.00%) - LeetHub --- 1683-invalid-tweets/1683-invalid-tweets.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1683-invalid-tweets/1683-invalid-tweets.sql b/1683-invalid-tweets/1683-invalid-tweets.sql index e8274c99..122290c0 100644 --- a/1683-invalid-tweets/1683-invalid-tweets.sql +++ b/1683-invalid-tweets/1683-invalid-tweets.sql @@ -1,5 +1,5 @@ # Write your MySQL query statement below select tweet_id from Tweets -where length(content) > 15; +where char_length(content) > 15; From d0720a36ea2788c3264b58f7c1616e652b0f80f5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 May 2024 22:42:15 +0530 Subject: [PATCH 1007/1399] Attach NOTES - LeetHub From 6c436cd3c12d603090711c1743d265905a3547c1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 18 May 2024 22:42:19 +0530 Subject: [PATCH 1008/1399] Time: 1068 ms (71.40%), Space: 0B (100.00%) - LeetHub From aba50adffbddf6c7dc3d5e18a30184cbb7ae24b6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 19 May 2024 21:37:35 +0530 Subject: [PATCH 1009/1399] Create README - LeetHub --- .../README.md | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 3154-find-number-of-ways-to-reach-the-k-th-stair/README.md diff --git a/3154-find-number-of-ways-to-reach-the-k-th-stair/README.md b/3154-find-number-of-ways-to-reach-the-k-th-stair/README.md new file mode 100644 index 00000000..d257801d --- /dev/null +++ b/3154-find-number-of-ways-to-reach-the-k-th-stair/README.md @@ -0,0 +1,85 @@ +

3154. Find Number of Ways to Reach the K-th Stair

Hard


You are given a non-negative integer k. There exists a staircase with an infinite number of stairs, with the lowest stair numbered 0.

+ +

Alice has an integer jump, with an initial value of 0. She starts on stair 1 and wants to reach stair k using any number of operations. If she is on stair i, in one operation she can:

+ +
    +
  • Go down to stair i - 1. This operation cannot be used consecutively or on stair 0.
  • +
  • Go up to stair i + 2jump. And then, jump becomes jump + 1.
  • +
+ +

Return the total number of ways Alice can reach stair k.

+ +

Note that it is possible that Alice reaches the stair k, and performs some operations to reach the stair k again.

+ +

 

+

Example 1:

+ +
+

Input: k = 0

+ +

Output: 2

+ +

Explanation:

+ +

The 2 possible ways of reaching stair 0 are:

+ +
    +
  • Alice starts at stair 1. +
      +
    • Using an operation of the first type, she goes down 1 stair to reach stair 0.
    • +
    +
  • +
  • Alice starts at stair 1. +
      +
    • Using an operation of the first type, she goes down 1 stair to reach stair 0.
    • +
    • Using an operation of the second type, she goes up 20 stairs to reach stair 1.
    • +
    • Using an operation of the first type, she goes down 1 stair to reach stair 0.
    • +
    +
  • +
+
+ +

Example 2:

+ +
+

Input: k = 1

+ +

Output: 4

+ +

Explanation:

+ +

The 4 possible ways of reaching stair 1 are:

+ +
    +
  • Alice starts at stair 1. Alice is at stair 1.
  • +
  • Alice starts at stair 1. +
      +
    • Using an operation of the first type, she goes down 1 stair to reach stair 0.
    • +
    • Using an operation of the second type, she goes up 20 stairs to reach stair 1.
    • +
    +
  • +
  • Alice starts at stair 1. +
      +
    • Using an operation of the second type, she goes up 20 stairs to reach stair 2.
    • +
    • Using an operation of the first type, she goes down 1 stair to reach stair 1.
    • +
    +
  • +
  • Alice starts at stair 1. +
      +
    • Using an operation of the first type, she goes down 1 stair to reach stair 0.
    • +
    • Using an operation of the second type, she goes up 20 stairs to reach stair 1.
    • +
    • Using an operation of the first type, she goes down 1 stair to reach stair 0.
    • +
    • Using an operation of the second type, she goes up 21 stairs to reach stair 2.
    • +
    • Using an operation of the first type, she goes down 1 stair to reach stair 1.
    • +
    +
  • +
+
+ +

 

+

Constraints:

+ +
    +
  • 0 <= k <= 109
  • +
+
\ No newline at end of file From ce241145d1125fd05df6753ff6ed3a376d18c4c8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 19 May 2024 21:37:38 +0530 Subject: [PATCH 1010/1399] Time: 247 ms (20.00%), Space: 86.2 MB (20.00%) - LeetHub --- ...number-of-ways-to-reach-the-k-th-stair.cpp | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 3154-find-number-of-ways-to-reach-the-k-th-stair/3154-find-number-of-ways-to-reach-the-k-th-stair.cpp diff --git a/3154-find-number-of-ways-to-reach-the-k-th-stair/3154-find-number-of-ways-to-reach-the-k-th-stair.cpp b/3154-find-number-of-ways-to-reach-the-k-th-stair/3154-find-number-of-ways-to-reach-the-k-th-stair.cpp new file mode 100644 index 00000000..9049bed3 --- /dev/null +++ b/3154-find-number-of-ways-to-reach-the-k-th-stair/3154-find-number-of-ways-to-reach-the-k-th-stair.cpp @@ -0,0 +1,38 @@ +class Solution { +public: + + map>> dp; + + int helper(int i, int backStep, int jump, int target) + { + int ans = 0; + + if(dp[i][backStep].find(jump) != dp[i][backStep].end()) + { + return dp[i][backStep][jump]; + } + + if(i == target) + { + ++ans; + } + + if(backStep and i > 0) + { + ans += helper(i-1, 0, jump, target); + } + + if(i + (1LL< Date: Sun, 19 May 2024 22:59:04 +0530 Subject: [PATCH 1011/1399] Create README - LeetHub --- .../README.md | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 3068-find-the-maximum-sum-of-node-values/README.md diff --git a/3068-find-the-maximum-sum-of-node-values/README.md b/3068-find-the-maximum-sum-of-node-values/README.md new file mode 100644 index 00000000..f988eb9a --- /dev/null +++ b/3068-find-the-maximum-sum-of-node-values/README.md @@ -0,0 +1,57 @@ +

3068. Find the Maximum Sum of Node Values

Hard


There exists an undirected tree with n nodes numbered 0 to n - 1. You are given a 0-indexed 2D integer array edges of length n - 1, where edges[i] = [ui, vi] indicates that there is an edge between nodes ui and vi in the tree. You are also given a positive integer k, and a 0-indexed array of non-negative integers nums of length n, where nums[i] represents the value of the node numbered i.

+ +

Alice wants the sum of values of tree nodes to be maximum, for which Alice can perform the following operation any number of times (including zero) on the tree:

+ +
    +
  • Choose any edge [u, v] connecting the nodes u and v, and update their values as follows: + +
      +
    • nums[u] = nums[u] XOR k
    • +
    • nums[v] = nums[v] XOR k
    • +
    +
  • +
+ +

Return the maximum possible sum of the values Alice can achieve by performing the operation any number of times.

+ +

 

+

Example 1:

+ +
Input: nums = [1,2,1], k = 3, edges = [[0,1],[0,2]]
+Output: 6
+Explanation: Alice can achieve the maximum sum of 6 using a single operation:
+- Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -> [2,2,2].
+The total sum of values is 2 + 2 + 2 = 6.
+It can be shown that 6 is the maximum achievable sum of values.
+
+ +

Example 2:

+ +
Input: nums = [2,3], k = 7, edges = [[0,1]]
+Output: 9
+Explanation: Alice can achieve the maximum sum of 9 using a single operation:
+- Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -> [5,4].
+The total sum of values is 5 + 4 = 9.
+It can be shown that 9 is the maximum achievable sum of values.
+
+ +

Example 3:

+ +
Input: nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]]
+Output: 42
+Explanation: The maximum achievable sum is 42 which can be achieved by Alice performing no operations.
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= n == nums.length <= 2 * 104
  • +
  • 1 <= k <= 109
  • +
  • 0 <= nums[i] <= 109
  • +
  • edges.length == n - 1
  • +
  • edges[i].length == 2
  • +
  • 0 <= edges[i][0], edges[i][1] <= n - 1
  • +
  • The input is generated such that edges represent a valid tree.
  • +
+
\ No newline at end of file From 67f7bda3b3a0ad1dbcdb29f44e9259dcf6c411af Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 19 May 2024 22:59:08 +0530 Subject: [PATCH 1012/1399] Time: 178 ms (86.44%), Space: 125.5 MB (88.89%) - LeetHub --- ...68-find-the-maximum-sum-of-node-values.cpp | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 3068-find-the-maximum-sum-of-node-values/3068-find-the-maximum-sum-of-node-values.cpp diff --git a/3068-find-the-maximum-sum-of-node-values/3068-find-the-maximum-sum-of-node-values.cpp b/3068-find-the-maximum-sum-of-node-values/3068-find-the-maximum-sum-of-node-values.cpp new file mode 100644 index 00000000..71123a5d --- /dev/null +++ b/3068-find-the-maximum-sum-of-node-values/3068-find-the-maximum-sum-of-node-values.cpp @@ -0,0 +1,26 @@ +class Solution { +public: + long long maximumValueSum(vector& nums, int k, vector>& edges) { + + long long sum = 0; + int smallest_gain = 2 * k + 1; + int smallest_lost = 2 * k + 1; + int gain_count = 0; + for (int num : nums) { + int gain = (num ^ k) - num; + if (gain > 0) { + smallest_gain = min(smallest_gain, gain); + sum += num + gain; + ++gain_count; + } else { + smallest_lost = min(smallest_lost, -gain); + sum += num; + } + } + if (gain_count % 2 == 1) { + sum -= min(smallest_gain, smallest_lost); + } + return sum; + + } +}; \ No newline at end of file From 182b2ea8a7360d3aee3fb1643c2ac38643509854 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 20 May 2024 09:38:34 +0530 Subject: [PATCH 1013/1399] Attach NOTES - LeetHub --- 3068-find-the-maximum-sum-of-node-values/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 3068-find-the-maximum-sum-of-node-values/NOTES.md diff --git a/3068-find-the-maximum-sum-of-node-values/NOTES.md b/3068-find-the-maximum-sum-of-node-values/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/3068-find-the-maximum-sum-of-node-values/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 026fcf567787c76aed4238088327e57770a11411 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 20 May 2024 09:38:37 +0530 Subject: [PATCH 1014/1399] Time: 189 ms (64.41%), Space: 125.5 MB (89.41%) - LeetHub --- ...68-find-the-maximum-sum-of-node-values.cpp | 43 +++++++++++-------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/3068-find-the-maximum-sum-of-node-values/3068-find-the-maximum-sum-of-node-values.cpp b/3068-find-the-maximum-sum-of-node-values/3068-find-the-maximum-sum-of-node-values.cpp index 71123a5d..704e11a4 100644 --- a/3068-find-the-maximum-sum-of-node-values/3068-find-the-maximum-sum-of-node-values.cpp +++ b/3068-find-the-maximum-sum-of-node-values/3068-find-the-maximum-sum-of-node-values.cpp @@ -2,25 +2,30 @@ class Solution { public: long long maximumValueSum(vector& nums, int k, vector>& edges) { - long long sum = 0; - int smallest_gain = 2 * k + 1; - int smallest_lost = 2 * k + 1; - int gain_count = 0; - for (int num : nums) { - int gain = (num ^ k) - num; - if (gain > 0) { - smallest_gain = min(smallest_gain, gain); - sum += num + gain; - ++gain_count; - } else { - smallest_lost = min(smallest_lost, -gain); - sum += num; - } - } - if (gain_count % 2 == 1) { - sum -= min(smallest_gain, smallest_lost); - } - return sum; + int mini = 1e9; + long long cnt = 0, ans = 0; + + for(auto& ele : nums) + { + int xorVal = ele ^ k; + + if(xorVal > ele) + { + ++cnt; + ans += xorVal; + } + else + { + ans += ele; + } + + mini = min(mini, abs(xorVal - ele)); + } + if(cnt & 1) + { + return ans - mini; + } + return ans; } }; \ No newline at end of file From ca82af92cfc02b6a5f67451436d91ca06a3947cb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 20 May 2024 10:16:28 +0530 Subject: [PATCH 1015/1399] Attach NOTES - LeetHub From 20e546c9fb67c42938bcb6194a5685e0d49c1a5d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 20 May 2024 10:16:31 +0530 Subject: [PATCH 1016/1399] Time: 189 ms (64.41%), Space: 125.5 MB (89.41%) - LeetHub From 0be13dd859d4bc4152e4597248b3befaeb92d618 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 21 May 2024 01:09:53 +0530 Subject: [PATCH 1017/1399] Create README - LeetHub --- 1863-sum-of-all-subset-xor-totals/README.md | 56 +++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 1863-sum-of-all-subset-xor-totals/README.md diff --git a/1863-sum-of-all-subset-xor-totals/README.md b/1863-sum-of-all-subset-xor-totals/README.md new file mode 100644 index 00000000..4468942a --- /dev/null +++ b/1863-sum-of-all-subset-xor-totals/README.md @@ -0,0 +1,56 @@ +

1863. Sum of All Subset XOR Totals

Easy


The XOR total of an array is defined as the bitwise XOR of all its elements, or 0 if the array is empty.

+ +
    +
  • For example, the XOR total of the array [2,5,6] is 2 XOR 5 XOR 6 = 1.
  • +
+ +

Given an array nums, return the sum of all XOR totals for every subset of nums

+ +

Note: Subsets with the same elements should be counted multiple times.

+ +

An array a is a subset of an array b if a can be obtained from b by deleting some (possibly zero) elements of b.

+ +

 

+

Example 1:

+ +
Input: nums = [1,3]
+Output: 6
+Explanation: The 4 subsets of [1,3] are:
+- The empty subset has an XOR total of 0.
+- [1] has an XOR total of 1.
+- [3] has an XOR total of 3.
+- [1,3] has an XOR total of 1 XOR 3 = 2.
+0 + 1 + 3 + 2 = 6
+
+ +

Example 2:

+ +
Input: nums = [5,1,6]
+Output: 28
+Explanation: The 8 subsets of [5,1,6] are:
+- The empty subset has an XOR total of 0.
+- [5] has an XOR total of 5.
+- [1] has an XOR total of 1.
+- [6] has an XOR total of 6.
+- [5,1] has an XOR total of 5 XOR 1 = 4.
+- [5,6] has an XOR total of 5 XOR 6 = 3.
+- [1,6] has an XOR total of 1 XOR 6 = 7.
+- [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2.
+0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28
+
+ +

Example 3:

+ +
Input: nums = [3,4,5,6,7,8]
+Output: 480
+Explanation: The sum of all XOR totals for every subset is 480.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 12
  • +
  • 1 <= nums[i] <= 20
  • +
+
\ No newline at end of file From 3fa2663857f66874919fcc6b95ae23f384f94e64 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 21 May 2024 01:09:53 +0530 Subject: [PATCH 1018/1399] Attach NOTES - LeetHub --- 1863-sum-of-all-subset-xor-totals/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1863-sum-of-all-subset-xor-totals/NOTES.md diff --git a/1863-sum-of-all-subset-xor-totals/NOTES.md b/1863-sum-of-all-subset-xor-totals/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1863-sum-of-all-subset-xor-totals/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From d0ae3a6182f36dd76cf25989706eb8cb3b2d2597 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 21 May 2024 01:09:56 +0530 Subject: [PATCH 1019/1399] Create README - LeetHub From 05a29f54a838709e3349e0f15405549cad5bef3a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 21 May 2024 01:09:57 +0530 Subject: [PATCH 1020/1399] Time: 7 ms (31.51%), Space: 8.3 MB (65.21%) - LeetHub --- .../1863-sum-of-all-subset-xor-totals.cpp | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 1863-sum-of-all-subset-xor-totals/1863-sum-of-all-subset-xor-totals.cpp diff --git a/1863-sum-of-all-subset-xor-totals/1863-sum-of-all-subset-xor-totals.cpp b/1863-sum-of-all-subset-xor-totals/1863-sum-of-all-subset-xor-totals.cpp new file mode 100644 index 00000000..8aa5e6d0 --- /dev/null +++ b/1863-sum-of-all-subset-xor-totals/1863-sum-of-all-subset-xor-totals.cpp @@ -0,0 +1,24 @@ +class Solution { +public: + int subsetXORSum(vector& nums) { + + int n = nums.size(); + + int range = (1 << n); + + int ans = 0; + + for(int i = 0; i < range; ++i) + { + int curXor = 0; + for(int j = 0; j < n; ++j) + { + if(i & (1 << j)) + curXor ^= nums[j]; + } + ans += curXor; + } + + return ans; + } +}; \ No newline at end of file From cd8dd215912689f6b9088949dcbadbf1b8069d89 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 21 May 2024 01:11:15 +0530 Subject: [PATCH 1021/1399] Attach NOTES - LeetHub From 24c3c490ed586c466af3d85716d21fc7bc02bc0d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 21 May 2024 01:11:18 +0530 Subject: [PATCH 1022/1399] Time: 7 ms (31.51%), Space: 8.3 MB (65.21%) - LeetHub From d2721033a1e85090efe5acde97bfc58035448063 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 21 May 2024 07:14:57 +0530 Subject: [PATCH 1023/1399] Create README - LeetHub --- 0078-subsets/README.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 0078-subsets/README.md diff --git a/0078-subsets/README.md b/0078-subsets/README.md new file mode 100644 index 00000000..226b26bb --- /dev/null +++ b/0078-subsets/README.md @@ -0,0 +1,26 @@ +

78. Subsets

Medium


Given an integer array nums of unique elements, return all possible subsets (the power set).

+ +

The solution set must not contain duplicate subsets. Return the solution in any order.

+ +

 

+

Example 1:

+ +
Input: nums = [1,2,3]
+Output: [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
+
+ +

Example 2:

+ +
Input: nums = [0]
+Output: [[],[0]]
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 10
  • +
  • -10 <= nums[i] <= 10
  • +
  • All the numbers of nums are unique.
  • +
+
\ No newline at end of file From 79cd980a18773b8f38191c563323beaf2fd07f9b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 21 May 2024 07:14:57 +0530 Subject: [PATCH 1024/1399] Attach NOTES - LeetHub --- 0078-subsets/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0078-subsets/NOTES.md diff --git a/0078-subsets/NOTES.md b/0078-subsets/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0078-subsets/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 60474471fa2c618412fab825dad24d18714e2556 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 21 May 2024 07:15:00 +0530 Subject: [PATCH 1025/1399] Time: 4 ms (39.89%), Space: 8.3 MB (62.90%) - LeetHub --- 0078-subsets/0078-subsets.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 0078-subsets/0078-subsets.cpp diff --git a/0078-subsets/0078-subsets.cpp b/0078-subsets/0078-subsets.cpp new file mode 100644 index 00000000..3a971dec --- /dev/null +++ b/0078-subsets/0078-subsets.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + + void helper(int idx, vector& ds, vector& nums, vector>& res) + { + if(idx == nums.size()) + { + res.push_back(ds); + return; + } + + ds.push_back(nums[idx]); + helper(idx+1, ds, nums, res); + ds.pop_back(); + helper(idx+1, ds, nums, res); + } + + vector> subsets(vector& nums) { + + vector> res; + vector ds; + + helper(0, ds, nums, res); + + return res; + } +}; \ No newline at end of file From 971a6286b9f23a4c50476f2b1d02e8d2a990cc0b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 21 May 2024 07:19:57 +0530 Subject: [PATCH 1026/1399] Create README - LeetHub --- 0090-subsets-ii/README.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 0090-subsets-ii/README.md diff --git a/0090-subsets-ii/README.md b/0090-subsets-ii/README.md new file mode 100644 index 00000000..49bedbe0 --- /dev/null +++ b/0090-subsets-ii/README.md @@ -0,0 +1,20 @@ +

90. Subsets II

Medium


Given an integer array nums that may contain duplicates, return all possible subsets (the power set).

+ +

The solution set must not contain duplicate subsets. Return the solution in any order.

+ +

 

+

Example 1:

+
Input: nums = [1,2,2]
+Output: [[],[1],[1,2],[1,2,2],[2],[2,2]]
+

Example 2:

+
Input: nums = [0]
+Output: [[],[0]]
+
+

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 10
  • +
  • -10 <= nums[i] <= 10
  • +
+
\ No newline at end of file From f5262777bf331525a20ef71ea0ad22d1c4ad4c67 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 21 May 2024 07:19:58 +0530 Subject: [PATCH 1027/1399] Attach NOTES - LeetHub --- 0090-subsets-ii/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0090-subsets-ii/NOTES.md diff --git a/0090-subsets-ii/NOTES.md b/0090-subsets-ii/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0090-subsets-ii/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 02394c59eb5c14e8444c52ec39d6da005220acdb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 21 May 2024 07:20:01 +0530 Subject: [PATCH 1028/1399] Time: 5 ms (30.77%), Space: 9.3 MB (44.07%) - LeetHub --- 0090-subsets-ii/0090-subsets-ii.cpp | 31 +++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 0090-subsets-ii/0090-subsets-ii.cpp diff --git a/0090-subsets-ii/0090-subsets-ii.cpp b/0090-subsets-ii/0090-subsets-ii.cpp new file mode 100644 index 00000000..f2522e66 --- /dev/null +++ b/0090-subsets-ii/0090-subsets-ii.cpp @@ -0,0 +1,31 @@ +class Solution { +public: + + void helper(int idx, vector ds, vector& nums, vector>& res) + { + res.push_back(ds); + + for(int i = idx; i < nums.size(); ++i) + { + if(i > idx and nums[i] == nums[i-1]) + continue; + ds.push_back(nums[i]); + helper(i+1, ds, nums, res); + ds.pop_back(); + } + } + + + vector> subsetsWithDup(vector& nums) { + + sort(nums.begin(), nums.end()); + + vector> res; + vector ds; + + helper(0, ds, nums, res); + + return res; + + } +}; \ No newline at end of file From ac034f9a789fedae2bbea6d7f6662fda08c5e723 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 23 May 2024 22:39:15 +0530 Subject: [PATCH 1029/1399] Attach NOTES - LeetHub --- 2597-the-number-of-beautiful-subsets/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2597-the-number-of-beautiful-subsets/NOTES.md diff --git a/2597-the-number-of-beautiful-subsets/NOTES.md b/2597-the-number-of-beautiful-subsets/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2597-the-number-of-beautiful-subsets/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 15c1f71fa64d3c80d8f6ea035c1955e712708ad4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 23 May 2024 22:40:44 +0530 Subject: [PATCH 1030/1399] Attach NOTES - LeetHub From 35027460c9d8da2f6bd6006446e03ed6c3739831 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 26 May 2024 08:56:35 +0530 Subject: [PATCH 1031/1399] Create README - LeetHub --- 0048-rotate-image/README.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 0048-rotate-image/README.md diff --git a/0048-rotate-image/README.md b/0048-rotate-image/README.md new file mode 100644 index 00000000..c415d5c4 --- /dev/null +++ b/0048-rotate-image/README.md @@ -0,0 +1,26 @@ +

48. Rotate Image

Medium


You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise).

+ +

You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.

+ +

 

+

Example 1:

+ +
Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
+Output: [[7,4,1],[8,5,2],[9,6,3]]
+
+ +

Example 2:

+ +
Input: matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]
+Output: [[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]
+
+ +

 

+

Constraints:

+ +
    +
  • n == matrix.length == matrix[i].length
  • +
  • 1 <= n <= 20
  • +
  • -1000 <= matrix[i][j] <= 1000
  • +
+
\ No newline at end of file From 0f913ce22d840206c4f456927a7f8b441ef5d353 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 26 May 2024 23:26:49 +0530 Subject: [PATCH 1032/1399] Attach NOTES - LeetHub --- 0552-student-attendance-record-ii/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0552-student-attendance-record-ii/NOTES.md diff --git a/0552-student-attendance-record-ii/NOTES.md b/0552-student-attendance-record-ii/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0552-student-attendance-record-ii/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From b3cab41ebba3c96c0745ff43622ece7e0b64aa5c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 26 May 2024 23:26:52 +0530 Subject: [PATCH 1033/1399] Time: 1377 ms (12.88%), Space: 420.3 MB (23.44%) - LeetHub --- .../0552-student-attendance-record-ii.cpp | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 0552-student-attendance-record-ii/0552-student-attendance-record-ii.cpp diff --git a/0552-student-attendance-record-ii/0552-student-attendance-record-ii.cpp b/0552-student-attendance-record-ii/0552-student-attendance-record-ii.cpp new file mode 100644 index 00000000..250559d7 --- /dev/null +++ b/0552-student-attendance-record-ii/0552-student-attendance-record-ii.cpp @@ -0,0 +1,33 @@ +class Solution { +private: + static const int MOD = 1000000000 + 7; + +public: + int checkRecord(int n) { + vector> prevDP(2, vector(3, 1)); + + for (int i = 1; i <= n; i++) { + vector> newDP(2, vector(3, 0)); + for (int a = 0; a < 2; a++) { + for (int l = 0; l < 3; l++) { + // Pick P + newDP[a][l] += prevDP[a][2]; + newDP[a][l] %= MOD; + if (a > 0) { + // Pick A + newDP[a][l] += prevDP[a - 1][2]; + newDP[a][l] %= MOD; + } + if (l > 0) { + // Pick L + newDP[a][l] += prevDP[a][l - 1]; + newDP[a][l] %= MOD; + } + } + } + prevDP = newDP; + } + + return prevDP[1][2]; + } +}; \ No newline at end of file From cc6f650c6ac74281388df96776a65dc419f6a73e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 27 May 2024 09:31:32 +0530 Subject: [PATCH 1034/1399] Attach NOTES - LeetHub From 7871837ca459ef1c2efdd1fb3c8a0d99af543fcc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 27 May 2024 09:31:35 +0530 Subject: [PATCH 1035/1399] Time: 1184 ms (29.43%), Space: 399.1 MB (32.97%) - LeetHub --- .../0552-student-attendance-record-ii.cpp | 64 +++++++++++-------- 1 file changed, 36 insertions(+), 28 deletions(-) diff --git a/0552-student-attendance-record-ii/0552-student-attendance-record-ii.cpp b/0552-student-attendance-record-ii/0552-student-attendance-record-ii.cpp index 250559d7..f87965aa 100644 --- a/0552-student-attendance-record-ii/0552-student-attendance-record-ii.cpp +++ b/0552-student-attendance-record-ii/0552-student-attendance-record-ii.cpp @@ -1,33 +1,41 @@ class Solution { -private: - static const int MOD = 1000000000 + 7; - public: - int checkRecord(int n) { - vector> prevDP(2, vector(3, 1)); - - for (int i = 1; i <= n; i++) { - vector> newDP(2, vector(3, 0)); - for (int a = 0; a < 2; a++) { - for (int l = 0; l < 3; l++) { - // Pick P - newDP[a][l] += prevDP[a][2]; - newDP[a][l] %= MOD; - if (a > 0) { - // Pick A - newDP[a][l] += prevDP[a - 1][2]; - newDP[a][l] %= MOD; - } - if (l > 0) { - // Pick L - newDP[a][l] += prevDP[a][l - 1]; - newDP[a][l] %= MOD; - } - } - } - prevDP = newDP; + + const int mod = 1e9+7; + + int helper(int n, int absent, int late, vector>>& dp) + { + if(n == 0) + return 1; + + if(dp[n][absent][late] != -1) + return dp[n][absent][late]; + + int total = 0; + + total += helper(n-1, absent, 2, dp); + total %= mod; + + if(absent > 0) + { + total += helper(n-1, absent-1, 2, dp); + total %= mod; + } + + if(late > 0) + { + total += helper(n-1, absent, late-1, dp); + total %= mod; } - - return prevDP[1][2]; + + return dp[n][absent][late] = total; + } + + int checkRecord(int n) { + + vector>> dp(n+1, vector>(2, vector(3, -1))); + + return helper(n, 1, 2, dp); + } }; \ No newline at end of file From 3a7c67be390e9bbe97721b4b09d60d86b75ad9ca Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 27 May 2024 20:49:07 +0530 Subject: [PATCH 1036/1399] Create README - LeetHub --- .../README.md | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 1608-special-array-with-x-elements-greater-than-or-equal-x/README.md diff --git a/1608-special-array-with-x-elements-greater-than-or-equal-x/README.md b/1608-special-array-with-x-elements-greater-than-or-equal-x/README.md new file mode 100644 index 00000000..6ccc0038 --- /dev/null +++ b/1608-special-array-with-x-elements-greater-than-or-equal-x/README.md @@ -0,0 +1,40 @@ +

1608. Special Array With X Elements Greater Than or Equal X

Easy


You are given an array nums of non-negative integers. nums is considered special if there exists a number x such that there are exactly x numbers in nums that are greater than or equal to x.

+ +

Notice that x does not have to be an element in nums.

+ +

Return x if the array is special, otherwise, return -1. It can be proven that if nums is special, the value for x is unique.

+ +

 

+

Example 1:

+ +
Input: nums = [3,5]
+Output: 2
+Explanation: There are 2 values (3 and 5) that are greater than or equal to 2.
+
+ +

Example 2:

+ +
Input: nums = [0,0]
+Output: -1
+Explanation: No numbers fit the criteria for x.
+If x = 0, there should be 0 numbers >= x, but there are 2.
+If x = 1, there should be 1 number >= x, but there are 0.
+If x = 2, there should be 2 numbers >= x, but there are 0.
+x cannot be greater since there are only 2 numbers in nums.
+
+ +

Example 3:

+ +
Input: nums = [0,4,3,0,4]
+Output: 3
+Explanation: There are 3 values that are greater than or equal to 3.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 100
  • +
  • 0 <= nums[i] <= 1000
  • +
+
\ No newline at end of file From b639cc01609c709ad4c775d6f32c923c4e5f6fcf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 27 May 2024 20:49:10 +0530 Subject: [PATCH 1037/1399] Time: 5 ms (33.11%), Space: 10.8 MB (5.12%) - LeetHub --- ...ith-x-elements-greater-than-or-equal-x.cpp | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 1608-special-array-with-x-elements-greater-than-or-equal-x/1608-special-array-with-x-elements-greater-than-or-equal-x.cpp diff --git a/1608-special-array-with-x-elements-greater-than-or-equal-x/1608-special-array-with-x-elements-greater-than-or-equal-x.cpp b/1608-special-array-with-x-elements-greater-than-or-equal-x/1608-special-array-with-x-elements-greater-than-or-equal-x.cpp new file mode 100644 index 00000000..2de9660e --- /dev/null +++ b/1608-special-array-with-x-elements-greater-than-or-equal-x/1608-special-array-with-x-elements-greater-than-or-equal-x.cpp @@ -0,0 +1,23 @@ +class Solution { +public: + int specialArray(vector& nums) { + + int n = nums.size(); + + vector pref(1001, 0); + + for(auto& ele : nums) + ++pref[ele]; + + for(int i = 999; i >= 0; --i) + pref[i] += pref[i+1]; + + for(int i = 1; i <= 1000; ++i) + { + if(pref[i] == i) + return i; + } + + return -1; + } +}; \ No newline at end of file From 259c7c3e5b0438d7879674d4c727f0993e0e10bb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 27 May 2024 20:50:11 +0530 Subject: [PATCH 1038/1399] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1608-special-array-with-x-elements-greater-than-or-equal-x/NOTES.md diff --git a/1608-special-array-with-x-elements-greater-than-or-equal-x/NOTES.md b/1608-special-array-with-x-elements-greater-than-or-equal-x/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1608-special-array-with-x-elements-greater-than-or-equal-x/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From c84ba67966fc543ecdb13ca3be8a16fc9f2dfecc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 27 May 2024 20:50:35 +0530 Subject: [PATCH 1039/1399] Attach NOTES - LeetHub From 73b1430bc48cc8abb79cc5536ef519c561a3e4a8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 27 May 2024 20:50:39 +0530 Subject: [PATCH 1040/1399] Time: 5 ms (33.11%), Space: 10.8 MB (5.12%) - LeetHub From ba34a0c9d8d5a5b32070cbdc15497c75dc74aec6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 28 May 2024 07:45:11 +0530 Subject: [PATCH 1041/1399] Create README - LeetHub --- .../README.md | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 3164-find-the-number-of-good-pairs-ii/README.md diff --git a/3164-find-the-number-of-good-pairs-ii/README.md b/3164-find-the-number-of-good-pairs-ii/README.md new file mode 100644 index 00000000..b81081a4 --- /dev/null +++ b/3164-find-the-number-of-good-pairs-ii/README.md @@ -0,0 +1,38 @@ +

3164. Find the Number of Good Pairs II

Medium


You are given 2 integer arrays nums1 and nums2 of lengths n and m respectively. You are also given a positive integer k.

+ +

A pair (i, j) is called good if nums1[i] is divisible by nums2[j] * k (0 <= i <= n - 1, 0 <= j <= m - 1).

+ +

Return the total number of good pairs.

+ +

 

+

Example 1:

+ +
+

Input: nums1 = [1,3,4], nums2 = [1,3,4], k = 1

+ +

Output: 5

+ +

Explanation:

+The 5 good pairs are (0, 0), (1, 0), (1, 1), (2, 0), and (2, 2).
+ +

Example 2:

+ +
+

Input: nums1 = [1,2,4,12], nums2 = [2,4], k = 3

+ +

Output: 2

+ +

Explanation:

+ +

The 2 good pairs are (3, 0) and (3, 1).

+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n, m <= 105
  • +
  • 1 <= nums1[i], nums2[j] <= 106
  • +
  • 1 <= k <= 103
  • +
+
\ No newline at end of file From 71556d5f47bf4e46b4ac2d9d408c390b13c5dd90 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 28 May 2024 07:45:16 +0530 Subject: [PATCH 1042/1399] Time: 1850 ms (25.30%), Space: 201.3 MB (20.19%) - LeetHub --- .../3164-find-the-number-of-good-pairs-ii.cpp | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 3164-find-the-number-of-good-pairs-ii/3164-find-the-number-of-good-pairs-ii.cpp diff --git a/3164-find-the-number-of-good-pairs-ii/3164-find-the-number-of-good-pairs-ii.cpp b/3164-find-the-number-of-good-pairs-ii/3164-find-the-number-of-good-pairs-ii.cpp new file mode 100644 index 00000000..e50652d3 --- /dev/null +++ b/3164-find-the-number-of-good-pairs-ii/3164-find-the-number-of-good-pairs-ii.cpp @@ -0,0 +1,34 @@ +class Solution { +public: + long long numberOfPairs(vector& nums1, vector& nums2, int k) { + + map fact; + + for(auto& ele : nums1) + { + int num = ele; + + for(int i = 1; i * i <= num; ++i) + { + if(num % i == 0) + { + ++fact[i]; + if(num/i != i) + ++fact[num/i]; + } + } + } + + long long ans = 0; + + for(auto& ele : nums2) + { + long long num = ele * k; + + ans += fact[num]; + } + + return ans; + + } +}; \ No newline at end of file From 57988780e9eb90b25b1981f5d5934050147fedb6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 28 May 2024 09:01:18 +0530 Subject: [PATCH 1043/1399] Create README - LeetHub --- .../README.md | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 1208-get-equal-substrings-within-budget/README.md diff --git a/1208-get-equal-substrings-within-budget/README.md b/1208-get-equal-substrings-within-budget/README.md new file mode 100644 index 00000000..45da0232 --- /dev/null +++ b/1208-get-equal-substrings-within-budget/README.md @@ -0,0 +1,39 @@ +

1208. Get Equal Substrings Within Budget

Medium


You are given two strings s and t of the same length and an integer maxCost.

+ +

You want to change s to t. Changing the ith character of s to ith character of t costs |s[i] - t[i]| (i.e., the absolute difference between the ASCII values of the characters).

+ +

Return the maximum length of a substring of s that can be changed to be the same as the corresponding substring of t with a cost less than or equal to maxCost. If there is no substring from s that can be changed to its corresponding substring from t, return 0.

+ +

 

+

Example 1:

+ +
Input: s = "abcd", t = "bcdf", maxCost = 3
+Output: 3
+Explanation: "abc" of s can change to "bcd".
+That costs 3, so the maximum length is 3.
+
+ +

Example 2:

+ +
Input: s = "abcd", t = "cdef", maxCost = 3
+Output: 1
+Explanation: Each character in s costs 2 to change to character in t,  so the maximum length is 1.
+
+ +

Example 3:

+ +
Input: s = "abcd", t = "acde", maxCost = 0
+Output: 1
+Explanation: You cannot make any change, so the maximum length is 1.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 105
  • +
  • t.length == s.length
  • +
  • 0 <= maxCost <= 106
  • +
  • s and t consist of only lowercase English letters.
  • +
+
\ No newline at end of file From 87c74afc557ca32dfbb128335a876a65908f5712 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 28 May 2024 09:01:21 +0530 Subject: [PATCH 1044/1399] Time: 66 ms (5.56%), Space: 19 MB (7.76%) - LeetHub --- ...208-get-equal-substrings-within-budget.cpp | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 1208-get-equal-substrings-within-budget/1208-get-equal-substrings-within-budget.cpp diff --git a/1208-get-equal-substrings-within-budget/1208-get-equal-substrings-within-budget.cpp b/1208-get-equal-substrings-within-budget/1208-get-equal-substrings-within-budget.cpp new file mode 100644 index 00000000..664765f3 --- /dev/null +++ b/1208-get-equal-substrings-within-budget/1208-get-equal-substrings-within-budget.cpp @@ -0,0 +1,35 @@ +class Solution { +public: + int equalSubstring(string s, string t, int maxCost) { + + int n = s.size(); + + map mp; + + for(int i = 0; i < n; ++i) + { + mp[i] = abs(s[i] - t[i]); + } + + int currCost = 0, len = 0; + + int i = 0, j = 0; + + while(j < n) + { + currCost += mp[j]; + + while(currCost > maxCost) + { + currCost -= mp[i]; + ++i; + } + + len = max(len, j - i + 1); + ++j; + } + + return len; + + } +}; \ No newline at end of file From a96244509cf57455bd4f09dfa5e5d856534a95bf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 28 May 2024 09:02:16 +0530 Subject: [PATCH 1045/1399] Attach NOTES - LeetHub --- 1208-get-equal-substrings-within-budget/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1208-get-equal-substrings-within-budget/NOTES.md diff --git a/1208-get-equal-substrings-within-budget/NOTES.md b/1208-get-equal-substrings-within-budget/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1208-get-equal-substrings-within-budget/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 45e1f1e4f9045d1fc6955a8a91b1d6ccedd5668e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 28 May 2024 09:02:17 +0530 Subject: [PATCH 1046/1399] Attach NOTES - LeetHub From 0024294c37952a4a08c9613dbc3752b12d1169a3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 28 May 2024 09:02:19 +0530 Subject: [PATCH 1047/1399] Time: 66 ms (5.56%), Space: 19 MB (7.76%) - LeetHub From e309122be54a40ad9c66fddea416296f8e139327 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 28 May 2024 09:02:20 +0530 Subject: [PATCH 1048/1399] Time: 66 ms (5.56%), Space: 19 MB (7.76%) - LeetHub From 98e1589177bbf985648c29f1238791c39c16e140 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 28 May 2024 09:03:46 +0530 Subject: [PATCH 1049/1399] Attach NOTES - LeetHub From c6e02613379a200efde2bb491512b2cdc736e9b6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 28 May 2024 09:03:49 +0530 Subject: [PATCH 1050/1399] Time: 66 ms (5.56%), Space: 19 MB (7.76%) - LeetHub From cbdac42d3fd8b49331a6e5581722e85c9c559a77 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 28 May 2024 09:26:46 +0530 Subject: [PATCH 1051/1399] Attach NOTES - LeetHub From 5bb43889dbad8e86cedf2cb104508bcb279a1a0a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 28 May 2024 09:26:49 +0530 Subject: [PATCH 1052/1399] Time: 6 ms (62.23%), Space: 8.6 MB (99.71%) - LeetHub --- ...208-get-equal-substrings-within-budget.cpp | 23 ++++++------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/1208-get-equal-substrings-within-budget/1208-get-equal-substrings-within-budget.cpp b/1208-get-equal-substrings-within-budget/1208-get-equal-substrings-within-budget.cpp index 664765f3..430101aa 100644 --- a/1208-get-equal-substrings-within-budget/1208-get-equal-substrings-within-budget.cpp +++ b/1208-get-equal-substrings-within-budget/1208-get-equal-substrings-within-budget.cpp @@ -4,32 +4,23 @@ class Solution { int n = s.size(); - map mp; - - for(int i = 0; i < n; ++i) - { - mp[i] = abs(s[i] - t[i]); - } - - int currCost = 0, len = 0; - - int i = 0, j = 0; + int i = 0, j = 0, ans = 0; while(j < n) { - currCost += mp[j]; + maxCost -= abs(s[j] - t[j]); - while(currCost > maxCost) + if(maxCost < 0) { - currCost -= mp[i]; + maxCost += abs(s[i] - t[i]); ++i; } - len = max(len, j - i + 1); + ans = max(ans, j - i + 1); + ++j; } - return len; - + return ans; } }; \ No newline at end of file From 9b5eaecd2499fba9a2bd47a3e25619bf6009174d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 29 May 2024 23:32:59 +0530 Subject: [PATCH 1053/1399] Create README - LeetHub --- .../README.md | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 1404-number-of-steps-to-reduce-a-number-in-binary-representation-to-one/README.md diff --git a/1404-number-of-steps-to-reduce-a-number-in-binary-representation-to-one/README.md b/1404-number-of-steps-to-reduce-a-number-in-binary-representation-to-one/README.md new file mode 100644 index 00000000..d5797331 --- /dev/null +++ b/1404-number-of-steps-to-reduce-a-number-in-binary-representation-to-one/README.md @@ -0,0 +1,50 @@ +

1404. Number of Steps to Reduce a Number in Binary Representation to One

Medium


Given the binary representation of an integer as a string s, return the number of steps to reduce it to 1 under the following rules:

+ +
    +
  • +

    If the current number is even, you have to divide it by 2.

    +
  • +
  • +

    If the current number is odd, you have to add 1 to it.

    +
  • +
+ +

It is guaranteed that you can always reach one for all test cases.

+ +

 

+

Example 1:

+ +
Input: s = "1101"
+Output: 6
+Explanation: "1101" corressponds to number 13 in their decimal representation.
+Step 1) 13 is odd, add 1 and obtain 14. 
+Step 2) 14 is even, divide by 2 and obtain 7.
+Step 3) 7 is odd, add 1 and obtain 8.
+Step 4) 8 is even, divide by 2 and obtain 4.  
+Step 5) 4 is even, divide by 2 and obtain 2. 
+Step 6) 2 is even, divide by 2 and obtain 1.  
+
+ +

Example 2:

+ +
Input: s = "10"
+Output: 1
+Explanation: "10" corressponds to number 2 in their decimal representation.
+Step 1) 2 is even, divide by 2 and obtain 1.  
+
+ +

Example 3:

+ +
Input: s = "1"
+Output: 0
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 500
  • +
  • s consists of characters '0' or '1'
  • +
  • s[0] == '1'
  • +
+
\ No newline at end of file From c9c41d2ded489e135a444081d5a4c1d07be08757 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 29 May 2024 23:33:02 +0530 Subject: [PATCH 1054/1399] Time: 3 ms (51.92%), Space: 7.4 MB (76.47%) - LeetHub --- ...number-in-binary-representation-to-one.cpp | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 1404-number-of-steps-to-reduce-a-number-in-binary-representation-to-one/1404-number-of-steps-to-reduce-a-number-in-binary-representation-to-one.cpp diff --git a/1404-number-of-steps-to-reduce-a-number-in-binary-representation-to-one/1404-number-of-steps-to-reduce-a-number-in-binary-representation-to-one.cpp b/1404-number-of-steps-to-reduce-a-number-in-binary-representation-to-one/1404-number-of-steps-to-reduce-a-number-in-binary-representation-to-one.cpp new file mode 100644 index 00000000..3f9ac903 --- /dev/null +++ b/1404-number-of-steps-to-reduce-a-number-in-binary-representation-to-one/1404-number-of-steps-to-reduce-a-number-in-binary-representation-to-one.cpp @@ -0,0 +1,25 @@ +class Solution { +public: + int numSteps(string s) { + int step = 0; + + while (s != "1") { + + if (s.back() == '0') s.pop_back(); + else { + + while (!s.empty() && s.back() == '1') { + s.pop_back(); + ++step; + } + + if (s.empty()) return step + 1; + else s.back() = '1'; + + } + ++step; + } + + return step; + } +}; \ No newline at end of file From 8aa4420fa3e1b1c60fa7bb705adecfe827b17ba9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 30 May 2024 22:11:22 +0530 Subject: [PATCH 1055/1399] Create README - LeetHub --- .../README.md | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 1442-count-triplets-that-can-form-two-arrays-of-equal-xor/README.md diff --git a/1442-count-triplets-that-can-form-two-arrays-of-equal-xor/README.md b/1442-count-triplets-that-can-form-two-arrays-of-equal-xor/README.md new file mode 100644 index 00000000..5a85fce7 --- /dev/null +++ b/1442-count-triplets-that-can-form-two-arrays-of-equal-xor/README.md @@ -0,0 +1,37 @@ +

1442. Count Triplets That Can Form Two Arrays of Equal XOR

Medium


Given an array of integers arr.

+ +

We want to select three indices i, j and k where (0 <= i < j <= k < arr.length).

+ +

Let's define a and b as follows:

+ +
    +
  • a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]
  • +
  • b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]
  • +
+ +

Note that ^ denotes the bitwise-xor operation.

+ +

Return the number of triplets (i, j and k) Where a == b.

+ +

 

+

Example 1:

+ +
Input: arr = [2,3,1,6,7]
+Output: 4
+Explanation: The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4)
+
+ +

Example 2:

+ +
Input: arr = [1,1,1,1,1]
+Output: 10
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= arr.length <= 300
  • +
  • 1 <= arr[i] <= 108
  • +
+
\ No newline at end of file From 623a31a533b3cb25c1b76130f4ee31a2c9cf8f1c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 30 May 2024 22:11:25 +0530 Subject: [PATCH 1056/1399] Time: 62 ms (5.12%), Space: 9.1 MB (67.04%) - LeetHub --- ...-that-can-form-two-arrays-of-equal-xor.cpp | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 1442-count-triplets-that-can-form-two-arrays-of-equal-xor/1442-count-triplets-that-can-form-two-arrays-of-equal-xor.cpp diff --git a/1442-count-triplets-that-can-form-two-arrays-of-equal-xor/1442-count-triplets-that-can-form-two-arrays-of-equal-xor.cpp b/1442-count-triplets-that-can-form-two-arrays-of-equal-xor/1442-count-triplets-that-can-form-two-arrays-of-equal-xor.cpp new file mode 100644 index 00000000..5136fb83 --- /dev/null +++ b/1442-count-triplets-that-can-form-two-arrays-of-equal-xor/1442-count-triplets-that-can-form-two-arrays-of-equal-xor.cpp @@ -0,0 +1,29 @@ +class Solution { +public: + int countTriplets(vector& arr) { + + int n = arr.size(); + + int cnt = 0; + + for(int i = 0; i < n; ++i) + { + for(int j = i+1; j < n; ++j) + { + int curXorF = 0, curXorS = 0; + + for(int k = i; k < j; ++k) + curXorF ^= arr[k]; + + for(int k = j; k < n; ++k) + { + curXorS ^= arr[k]; + if(curXorF == curXorS) + ++cnt; + } + } + } + + return cnt; + } +}; From 752000cb4087d7ca0fa6b9122487e74125539984 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 31 May 2024 12:11:45 +0530 Subject: [PATCH 1057/1399] Attach NOTES - LeetHub --- 0137-single-number-ii/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0137-single-number-ii/NOTES.md diff --git a/0137-single-number-ii/NOTES.md b/0137-single-number-ii/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0137-single-number-ii/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From a9f0fb44a495f8bfdd815560c94cba815785d61f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 31 May 2024 13:48:08 +0530 Subject: [PATCH 1058/1399] Create README - LeetHub --- 0260-single-number-iii/README.md | 33 ++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 0260-single-number-iii/README.md diff --git a/0260-single-number-iii/README.md b/0260-single-number-iii/README.md new file mode 100644 index 00000000..203cd5d7 --- /dev/null +++ b/0260-single-number-iii/README.md @@ -0,0 +1,33 @@ +

260. Single Number III

Medium


Given an integer array nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. You can return the answer in any order.

+ +

You must write an algorithm that runs in linear runtime complexity and uses only constant extra space.

+ +

 

+

Example 1:

+ +
Input: nums = [1,2,1,3,2,5]
+Output: [3,5]
+Explanation:  [5, 3] is also a valid answer.
+
+ +

Example 2:

+ +
Input: nums = [-1,0]
+Output: [-1,0]
+
+ +

Example 3:

+ +
Input: nums = [0,1]
+Output: [1,0]
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= nums.length <= 3 * 104
  • +
  • -231 <= nums[i] <= 231 - 1
  • +
  • Each integer in nums will appear twice, only two integers will appear once.
  • +
+
\ No newline at end of file From 4eca7552302cf08c403235f3a7bcc98758c2487a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 31 May 2024 13:48:11 +0530 Subject: [PATCH 1059/1399] Time: 3 ms (96.61%), Space: 12.2 MB (98.70%) - LeetHub --- .../0260-single-number-iii.cpp | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 0260-single-number-iii/0260-single-number-iii.cpp diff --git a/0260-single-number-iii/0260-single-number-iii.cpp b/0260-single-number-iii/0260-single-number-iii.cpp new file mode 100644 index 00000000..8adcc2c8 --- /dev/null +++ b/0260-single-number-iii/0260-single-number-iii.cpp @@ -0,0 +1,24 @@ +class Solution { +public: + vector singleNumber(vector& nums) { + + int b1 = 0, b2 = 0; + + long long xorr = 0; + + for(auto& ele : nums) + xorr ^= ele; + + long long rightMost = (xorr&(xorr-1)) ^ xorr; + + for(auto& ele : nums) + { + if(ele & rightMost) + b1 ^= ele; + else + b2 ^= ele; + } + + return {b1, b2}; + } +}; \ No newline at end of file From f20e5f385022f3a621e6b459c703fb400edc7197 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 1 Jun 2024 07:16:57 +0530 Subject: [PATCH 1060/1399] Create README - LeetHub --- 3110-score-of-a-string/README.md | 37 ++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 3110-score-of-a-string/README.md diff --git a/3110-score-of-a-string/README.md b/3110-score-of-a-string/README.md new file mode 100644 index 00000000..5859fc38 --- /dev/null +++ b/3110-score-of-a-string/README.md @@ -0,0 +1,37 @@ +

3110. Score of a String

Easy


You are given a string s. The score of a string is defined as the sum of the absolute difference between the ASCII values of adjacent characters.

+ +

Return the score of s.

+ +

 

+

Example 1:

+ +
+

Input: s = "hello"

+ +

Output: 13

+ +

Explanation:

+ +

The ASCII values of the characters in s are: 'h' = 104, 'e' = 101, 'l' = 108, 'o' = 111. So, the score of s would be |104 - 101| + |101 - 108| + |108 - 108| + |108 - 111| = 3 + 7 + 0 + 3 = 13.

+
+ +

Example 2:

+ +
+

Input: s = "zaz"

+ +

Output: 50

+ +

Explanation:

+ +

The ASCII values of the characters in s are: 'z' = 122, 'a' = 97. So, the score of s would be |122 - 97| + |97 - 122| = 25 + 25 = 50.

+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= s.length <= 100
  • +
  • s consists only of lowercase English letters.
  • +
+
\ No newline at end of file From 887fb748b702ebe1c57055aa44bc224cc67f4752 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 1 Jun 2024 07:17:01 +0530 Subject: [PATCH 1061/1399] Time: 5 ms (27.27%), Space: 7.8 MB (78.98%) - LeetHub --- .../3110-score-of-a-string.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 3110-score-of-a-string/3110-score-of-a-string.cpp diff --git a/3110-score-of-a-string/3110-score-of-a-string.cpp b/3110-score-of-a-string/3110-score-of-a-string.cpp new file mode 100644 index 00000000..cbfc766d --- /dev/null +++ b/3110-score-of-a-string/3110-score-of-a-string.cpp @@ -0,0 +1,16 @@ +class Solution { +public: + int scoreOfString(string s) { + + int n = s.size(); + + int ans = 0; + + for(int i = 1; i < n; ++i) + { + ans += abs((s[i] - 'a') - (s[i-1] - 'a')); + } + + return ans; + } +}; \ No newline at end of file From d6cb37e0c39eba68559bd371346d8403783ee92c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 2 Jun 2024 10:30:26 +0530 Subject: [PATCH 1062/1399] Create README - LeetHub --- 0344-reverse-string/README.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 0344-reverse-string/README.md diff --git a/0344-reverse-string/README.md b/0344-reverse-string/README.md new file mode 100644 index 00000000..5c9b221e --- /dev/null +++ b/0344-reverse-string/README.md @@ -0,0 +1,20 @@ +

344. Reverse String

Easy


Write a function that reverses a string. The input string is given as an array of characters s.

+ +

You must do this by modifying the input array in-place with O(1) extra memory.

+ +

 

+

Example 1:

+
Input: s = ["h","e","l","l","o"]
+Output: ["o","l","l","e","h"]
+

Example 2:

+
Input: s = ["H","a","n","n","a","h"]
+Output: ["h","a","n","n","a","H"]
+
+

 

+

Constraints:

+ + +
\ No newline at end of file From 6967aca8b49cce74ca03f54f1f3e1f4ca4ef098f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 2 Jun 2024 10:30:30 +0530 Subject: [PATCH 1063/1399] Create README - LeetHub From f2e4fba6720a5a63e76b2c80e4681ceb810952b8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 2 Jun 2024 10:30:30 +0530 Subject: [PATCH 1064/1399] Attach NOTES - LeetHub --- 0344-reverse-string/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0344-reverse-string/NOTES.md diff --git a/0344-reverse-string/NOTES.md b/0344-reverse-string/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0344-reverse-string/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 8ea33e3354e78142de4318d278b9fae8552c449b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 2 Jun 2024 10:30:32 +0530 Subject: [PATCH 1065/1399] Create README - LeetHub From dd635148a70f2a8dbb7e535ac69417a8f9cbc336 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 2 Jun 2024 10:30:32 +0530 Subject: [PATCH 1066/1399] Attach NOTES - LeetHub From 94a011df4ab125b6abaf19da67ac07059b057cca Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 2 Jun 2024 10:30:34 +0530 Subject: [PATCH 1067/1399] Time: 19 ms (47.68%), Space: 26.8 MB (89.96%) - LeetHub --- 0344-reverse-string/0344-reverse-string.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 0344-reverse-string/0344-reverse-string.cpp diff --git a/0344-reverse-string/0344-reverse-string.cpp b/0344-reverse-string/0344-reverse-string.cpp new file mode 100644 index 00000000..c1441fef --- /dev/null +++ b/0344-reverse-string/0344-reverse-string.cpp @@ -0,0 +1,11 @@ +class Solution { +public: + void reverseString(vector& s) { + + int n = s.size(); + + for(int i = 0; i < n/2; ++i) + swap(s[i], s[n-i-1]); + + } +}; \ No newline at end of file From 7a0abbd236bd5b65daebf43d62debdb895635683 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 2 Jun 2024 10:30:36 +0530 Subject: [PATCH 1068/1399] Time: 19 ms (47.68%), Space: 26.8 MB (89.96%) - LeetHub From 50353a74e10dda874d0c671dc0fc57c671e0bb26 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 2 Jun 2024 21:15:29 +0530 Subject: [PATCH 1069/1399] Create README - LeetHub --- 3169-count-days-without-meetings/README.md | 53 ++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 3169-count-days-without-meetings/README.md diff --git a/3169-count-days-without-meetings/README.md b/3169-count-days-without-meetings/README.md new file mode 100644 index 00000000..ecd8d5e6 --- /dev/null +++ b/3169-count-days-without-meetings/README.md @@ -0,0 +1,53 @@ +

3169. Count Days Without Meetings

Medium


You are given a positive integer days representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array meetings of size n where, meetings[i] = [start_i, end_i] represents the starting and ending days of meeting i (inclusive).

+ +

Return the count of days when the employee is available for work but no meetings are scheduled.

+ +

Note: The meetings may overlap.

+ +

 

+

Example 1:

+ +
+

Input: days = 10, meetings = [[5,7],[1,3],[9,10]]

+ +

Output: 2

+ +

Explanation:

+ +

There is no meeting scheduled on the 4th and 8th days.

+
+ +

Example 2:

+ +
+

Input: days = 5, meetings = [[2,4],[1,3]]

+ +

Output: 1

+ +

Explanation:

+ +

There is no meeting scheduled on the 5th day.

+
+ +

Example 3:

+ +
+

Input: days = 6, meetings = [[1,6]]

+ +

Output: 0

+ +

Explanation:

+ +

Meetings are scheduled for all working days.

+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= days <= 109
  • +
  • 1 <= meetings.length <= 105
  • +
  • meetings[i].length == 2
  • +
  • 1 <= meetings[i][0] <= meetings[i][1] <= days
  • +
+
\ No newline at end of file From 6f47c24f9f17c27c91d4b5f450455e046b092da0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 2 Jun 2024 21:15:30 +0530 Subject: [PATCH 1070/1399] Attach NOTES - LeetHub --- 3169-count-days-without-meetings/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 3169-count-days-without-meetings/NOTES.md diff --git a/3169-count-days-without-meetings/NOTES.md b/3169-count-days-without-meetings/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/3169-count-days-without-meetings/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 4f498cfc62d78c587424db53fc93f557c5a5a7c0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 2 Jun 2024 21:15:33 +0530 Subject: [PATCH 1071/1399] Time: 296 ms (33.33%), Space: 141.5 MB (16.67%) - LeetHub --- .../3169-count-days-without-meetings.cpp | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 3169-count-days-without-meetings/3169-count-days-without-meetings.cpp diff --git a/3169-count-days-without-meetings/3169-count-days-without-meetings.cpp b/3169-count-days-without-meetings/3169-count-days-without-meetings.cpp new file mode 100644 index 00000000..e82c9dde --- /dev/null +++ b/3169-count-days-without-meetings/3169-count-days-without-meetings.cpp @@ -0,0 +1,36 @@ +class Solution { +public: + int countDays(int days, vector>& meetings) { + + int n = meetings.size(); + + vector> events; + + for(auto& e : meetings) + { + events.push_back({e[0], +1}); + events.push_back({e[1], -1}); + } + + events.push_back({0, 0}); + events.push_back({days+1, 0}); + + sort(events.begin(), events.end()); + + int ans = 0, currMeetings = 0; + + for(int i = 0; i < events.size()-1; ++i) + { + currMeetings += events[i].second; + + if(currMeetings == 0) + { + ans += max(0, events[i+1].first - events[i].first - 1); + } + + } + + return ans; + + } +}; \ No newline at end of file From e123ccf98c25876cd6487bbd29e8240754853dd5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 2 Jun 2024 21:32:26 +0530 Subject: [PATCH 1072/1399] Create README - LeetHub --- .../README.md | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 3171-find-subarray-with-bitwise-and-closest-to-k/README.md diff --git a/3171-find-subarray-with-bitwise-and-closest-to-k/README.md b/3171-find-subarray-with-bitwise-and-closest-to-k/README.md new file mode 100644 index 00000000..8e2e15f6 --- /dev/null +++ b/3171-find-subarray-with-bitwise-and-closest-to-k/README.md @@ -0,0 +1,52 @@ +

3171. Find Subarray With Bitwise AND Closest to K

Hard


You are given an array nums and an integer k. You need to find a subarray of nums such that the absolute difference between k and the bitwise AND of the subarray elements is as small as possible. In other words, select a subarray nums[l..r] such that |k - (nums[l] AND nums[l + 1] ... AND nums[r])| is minimum.

+ +

Return the minimum possible value of the absolute difference.

+ +

A subarray is a contiguous non-empty sequence of elements within an array.

+ +

 

+

Example 1:

+ +
+

Input: nums = [1,2,4,5], k = 3

+ +

Output: 1

+ +

Explanation:

+ +

The subarray nums[2..3] has AND value 4, which gives the minimum absolute difference |3 - 4| = 1.

+
+ +

Example 2:

+ +
+

Input: nums = [1,2,1,2], k = 2

+ +

Output: 0

+ +

Explanation:

+ +

The subarray nums[1..1] has AND value 2, which gives the minimum absolute difference |2 - 2| = 0.

+
+ +

Example 3:

+ +
+

Input: nums = [1], k = 10

+ +

Output: 9

+ +

Explanation:

+ +

There is a single subarray with AND value 1, which gives the minimum absolute difference |10 - 1| = 9.

+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 105
  • +
  • 1 <= nums[i] <= 109
  • +
  • 1 <= k <= 109
  • +
+
\ No newline at end of file From f59dbc361c8b58a4ef85e504f29215c556255fa0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 2 Jun 2024 21:32:29 +0530 Subject: [PATCH 1073/1399] Time: 744 ms (100.00%), Space: 345.9 MB (8.33%) - LeetHub --- ...subarray-with-bitwise-and-closest-to-k.cpp | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 3171-find-subarray-with-bitwise-and-closest-to-k/3171-find-subarray-with-bitwise-and-closest-to-k.cpp diff --git a/3171-find-subarray-with-bitwise-and-closest-to-k/3171-find-subarray-with-bitwise-and-closest-to-k.cpp b/3171-find-subarray-with-bitwise-and-closest-to-k/3171-find-subarray-with-bitwise-and-closest-to-k.cpp new file mode 100644 index 00000000..1bc8d5f5 --- /dev/null +++ b/3171-find-subarray-with-bitwise-and-closest-to-k/3171-find-subarray-with-bitwise-and-closest-to-k.cpp @@ -0,0 +1,31 @@ +class Solution { +public: + int minimumDifference(vector& nums, int k) { + + set curSet; + + int ans = INT_MAX; + + for(auto& ele : nums) + { + set newSet; + + for(auto& vals : curSet) + { + newSet.insert(vals & ele); + } + + newSet.insert(ele); + + for(auto& vals : newSet) + { + ans = min(ans, abs(k - vals)); + } + + curSet = newSet; + } + + return ans; + + } +}; \ No newline at end of file From 58689e12fabf36805b6f7442a23f0e0f73baa29b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 3 Jun 2024 22:48:45 +0530 Subject: [PATCH 1074/1399] Create README - LeetHub --- .../README.md | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 2486-append-characters-to-string-to-make-subsequence/README.md diff --git a/2486-append-characters-to-string-to-make-subsequence/README.md b/2486-append-characters-to-string-to-make-subsequence/README.md new file mode 100644 index 00000000..3029053c --- /dev/null +++ b/2486-append-characters-to-string-to-make-subsequence/README.md @@ -0,0 +1,40 @@ +

2486. Append Characters to String to Make Subsequence

Medium


You are given two strings s and t consisting of only lowercase English letters.

+ +

Return the minimum number of characters that need to be appended to the end of s so that t becomes a subsequence of s.

+ +

A subsequence is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.

+ +

 

+

Example 1:

+ +
Input: s = "coaching", t = "coding"
+Output: 4
+Explanation: Append the characters "ding" to the end of s so that s = "coachingding".
+Now, t is a subsequence of s ("coachingding").
+It can be shown that appending any 3 characters to the end of s will never make t a subsequence.
+
+ +

Example 2:

+ +
Input: s = "abcde", t = "a"
+Output: 0
+Explanation: t is already a subsequence of s ("abcde").
+
+ +

Example 3:

+ +
Input: s = "z", t = "abcde"
+Output: 5
+Explanation: Append the characters "abcde" to the end of s so that s = "zabcde".
+Now, t is a subsequence of s ("zabcde").
+It can be shown that appending any 4 characters to the end of s will never make t a subsequence.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length, t.length <= 105
  • +
  • s and t consist only of lowercase English letters.
  • +
+
\ No newline at end of file From 0c29445cf96b68249a8cdcecead8d24180aa617f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 3 Jun 2024 22:48:50 +0530 Subject: [PATCH 1075/1399] Time: 19 ms (59.76%), Space: 11.8 MB (83.42%) - LeetHub --- ...haracters-to-string-to-make-subsequence.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 2486-append-characters-to-string-to-make-subsequence/2486-append-characters-to-string-to-make-subsequence.cpp diff --git a/2486-append-characters-to-string-to-make-subsequence/2486-append-characters-to-string-to-make-subsequence.cpp b/2486-append-characters-to-string-to-make-subsequence/2486-append-characters-to-string-to-make-subsequence.cpp new file mode 100644 index 00000000..36e9a014 --- /dev/null +++ b/2486-append-characters-to-string-to-make-subsequence/2486-append-characters-to-string-to-make-subsequence.cpp @@ -0,0 +1,18 @@ +class Solution { +public: + int appendCharacters(string s, string t) { + + int idx = 0; + + for(int i = 0; i < s.size(); ++i) + { + if(t[idx] == s[i]) + ++idx; + if(idx == t.size()) + return 0; + } + + return t.size() - idx; + + } +}; \ No newline at end of file From 29ac9761651fece855352d9d9f12bcaac0d855ea Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 4 Jun 2024 07:56:43 +0530 Subject: [PATCH 1076/1399] Create README - LeetHub --- 0409-longest-palindrome/README.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 0409-longest-palindrome/README.md diff --git a/0409-longest-palindrome/README.md b/0409-longest-palindrome/README.md new file mode 100644 index 00000000..2d84a7b5 --- /dev/null +++ b/0409-longest-palindrome/README.md @@ -0,0 +1,27 @@ +

409. Longest Palindrome

Easy


Given a string s which consists of lowercase or uppercase letters, return the length of the longest palindrome that can be built with those letters.

+ +

Letters are case sensitive, for example, "Aa" is not considered a palindrome.

+ +

 

+

Example 1:

+ +
Input: s = "abccccdd"
+Output: 7
+Explanation: One longest palindrome that can be built is "dccaccd", whose length is 7.
+
+ +

Example 2:

+ +
Input: s = "a"
+Output: 1
+Explanation: The longest palindrome that can be built is "a", whose length is 1.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 2000
  • +
  • s consists of lowercase and/or uppercase English letters only.
  • +
+
\ No newline at end of file From ce17bf327ff8eda14001a29a806e0748004ca076 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 4 Jun 2024 07:56:43 +0530 Subject: [PATCH 1077/1399] Attach NOTES - LeetHub --- 0409-longest-palindrome/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0409-longest-palindrome/NOTES.md diff --git a/0409-longest-palindrome/NOTES.md b/0409-longest-palindrome/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0409-longest-palindrome/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From ced0d06e4ff5812e052975c4e0a5306042cbb4ff Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 4 Jun 2024 07:56:47 +0530 Subject: [PATCH 1078/1399] Time: 7 ms (16.33%), Space: 8.9 MB (48.94%) - LeetHub --- .../0409-longest-palindrome.cpp | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 0409-longest-palindrome/0409-longest-palindrome.cpp diff --git a/0409-longest-palindrome/0409-longest-palindrome.cpp b/0409-longest-palindrome/0409-longest-palindrome.cpp new file mode 100644 index 00000000..d2c5f246 --- /dev/null +++ b/0409-longest-palindrome/0409-longest-palindrome.cpp @@ -0,0 +1,25 @@ +class Solution { +public: + int longestPalindrome(string s) { + map m ; + bool odd = 0; + for(auto i : s) + ++m[i]; + + int ind = 0; + + for(auto i : m) + { + if(i.second % 2 == 0) + ind += i.second; + else + { + ind += i.second - 1; + odd = true; + } + } + if(odd) + ind += 1; + return ind; + } +}; \ No newline at end of file From 1c463e9a899ff9a177882fd857748bb8ae7211dc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 5 Jun 2024 20:29:49 +0530 Subject: [PATCH 1079/1399] Create README - LeetHub --- 1002-find-common-characters/README.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 1002-find-common-characters/README.md diff --git a/1002-find-common-characters/README.md b/1002-find-common-characters/README.md new file mode 100644 index 00000000..54c8be40 --- /dev/null +++ b/1002-find-common-characters/README.md @@ -0,0 +1,19 @@ +

1002. Find Common Characters

Easy


Given a string array words, return an array of all characters that show up in all strings within the words (including duplicates). You may return the answer in any order.

+ +

 

+

Example 1:

+
Input: words = ["bella","label","roller"]
+Output: ["e","l","l"]
+

Example 2:

+
Input: words = ["cool","lock","cook"]
+Output: ["c","o"]
+
+

 

+

Constraints:

+ +
    +
  • 1 <= words.length <= 100
  • +
  • 1 <= words[i].length <= 100
  • +
  • words[i] consists of lowercase English letters.
  • +
+
\ No newline at end of file From d1e7fce091d24f8377d261e375f771ed02858907 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 5 Jun 2024 20:29:52 +0530 Subject: [PATCH 1080/1399] Time: 5 ms (76.27%), Space: 11.3 MB (69.23%) - LeetHub --- .../1002-find-common-characters.cpp | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 1002-find-common-characters/1002-find-common-characters.cpp diff --git a/1002-find-common-characters/1002-find-common-characters.cpp b/1002-find-common-characters/1002-find-common-characters.cpp new file mode 100644 index 00000000..f2254e0a --- /dev/null +++ b/1002-find-common-characters/1002-find-common-characters.cpp @@ -0,0 +1,37 @@ +class Solution { +public: + vector commonChars(vector& words) { + + int n = words.size(); + + vector freq(26, 0); + + for(int i = 0; i < n; ++i) + { + vector curr(26, 0); + + for(int j = 0; j < words[i].size(); ++j) + ++curr[words[i][j] - 'a']; + + if(i != 0) + { + for(int j = 0; j < 26; ++j) + curr[j] = min(curr[j], freq[j]); + } + + freq = curr; + } + + vector ans; + + for(int i = 0; i < 26; ++i) + { + for(int j = 0; j < freq[i]; ++j) + { + ans.push_back(string(1, 'a' + i)); + } + } + + return ans; + } +}; \ No newline at end of file From d1c3bc2970731be73e8735b7a41dbf932b11078f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 5 Jun 2024 20:31:05 +0530 Subject: [PATCH 1081/1399] Attach NOTES - LeetHub --- 1002-find-common-characters/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1002-find-common-characters/NOTES.md diff --git a/1002-find-common-characters/NOTES.md b/1002-find-common-characters/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1002-find-common-characters/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 7c739409f690f9c9d58f95b95dc9d7988dafc8dd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 5 Jun 2024 20:31:08 +0530 Subject: [PATCH 1082/1399] Time: 5 ms (76.27%), Space: 11.3 MB (69.23%) - LeetHub From 676570b4ca90c858321f9b9f3b28cb94f53b6c5e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 5 Jun 2024 20:31:19 +0530 Subject: [PATCH 1083/1399] Attach NOTES - LeetHub From 21a7802ffeca1f5b6c92cc4f4ff5803079c17b78 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 5 Jun 2024 20:31:23 +0530 Subject: [PATCH 1084/1399] Time: 5 ms (76.27%), Space: 11.3 MB (69.23%) - LeetHub From c02f73e1677a06895ceb006770132fd2d37e1c67 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 6 Jun 2024 08:44:22 +0530 Subject: [PATCH 1085/1399] Attach NOTES - LeetHub From 73383e3479e34a22afd565120baa983ba82479e6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 6 Jun 2024 21:11:47 +0530 Subject: [PATCH 1086/1399] Create README - LeetHub --- 0846-hand-of-straights/README.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 0846-hand-of-straights/README.md diff --git a/0846-hand-of-straights/README.md b/0846-hand-of-straights/README.md new file mode 100644 index 00000000..fe6ec095 --- /dev/null +++ b/0846-hand-of-straights/README.md @@ -0,0 +1,32 @@ +

846. Hand of Straights

Medium


Alice has some number of cards and she wants to rearrange the cards into groups so that each group is of size groupSize, and consists of groupSize consecutive cards.

+ +

Given an integer array hand where hand[i] is the value written on the ith card and an integer groupSize, return true if she can rearrange the cards, or false otherwise.

+ +

 

+

Example 1:

+ +
Input: hand = [1,2,3,6,2,3,4,7,8], groupSize = 3
+Output: true
+Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]
+
+ +

Example 2:

+ +
Input: hand = [1,2,3,4,5], groupSize = 4
+Output: false
+Explanation: Alice's hand can not be rearranged into groups of 4.
+
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= hand.length <= 104
  • +
  • 0 <= hand[i] <= 109
  • +
  • 1 <= groupSize <= hand.length
  • +
+ +

 

+

Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/

+
\ No newline at end of file From 94f36f53bba3372f001e38207d8ca5fa7fed5ff8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 6 Jun 2024 21:11:52 +0530 Subject: [PATCH 1087/1399] Time: 58 ms (62.52%), Space: 30.6 MB (69.25%) - LeetHub --- .../0846-hand-of-straights.cpp | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 0846-hand-of-straights/0846-hand-of-straights.cpp diff --git a/0846-hand-of-straights/0846-hand-of-straights.cpp b/0846-hand-of-straights/0846-hand-of-straights.cpp new file mode 100644 index 00000000..a0c9450b --- /dev/null +++ b/0846-hand-of-straights/0846-hand-of-straights.cpp @@ -0,0 +1,51 @@ +class Solution { +public: + bool isNStraightHand(vector& hand, int groupSize) { + + int n = hand.size(); + + if(n % groupSize != 0) + return false; + + int step = n / groupSize; + + map mp; + + for(auto& ele : hand) + ++mp[ele]; + + while(step--) + { + int k = 0, prev = -1; + + if(mp.size() < groupSize) + return false; + + for(auto& ele : mp) + { + ++k; + + if(prev == -1) + prev = ele.first; + else + { + if(prev + 1 != ele.first) + return false; + } + + prev = ele.first; + + --ele.second; + + if(ele.second == 0) + mp.erase(ele.first); // remove + + if(k == groupSize) + break; + } + } + + return true; + + } +}; \ No newline at end of file From 6cf94a46b737c450867ccdec281217e078dcba7b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 6 Jun 2024 21:12:50 +0530 Subject: [PATCH 1088/1399] Attach NOTES - LeetHub --- 0846-hand-of-straights/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0846-hand-of-straights/NOTES.md diff --git a/0846-hand-of-straights/NOTES.md b/0846-hand-of-straights/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0846-hand-of-straights/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From b1b81762f57c265b8ab7ec664aa9f5792054ff11 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 6 Jun 2024 21:12:54 +0530 Subject: [PATCH 1089/1399] Time: 58 ms (62.52%), Space: 30.6 MB (69.25%) - LeetHub From a6e3eea67403bf6aa4f348809fbd1c4a4bea7a07 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 7 Jun 2024 22:18:38 +0530 Subject: [PATCH 1090/1399] Create README - LeetHub --- 0648-replace-words/README.md | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 0648-replace-words/README.md diff --git a/0648-replace-words/README.md b/0648-replace-words/README.md new file mode 100644 index 00000000..aed11e37 --- /dev/null +++ b/0648-replace-words/README.md @@ -0,0 +1,34 @@ +

648. Replace Words

Medium


In English, we have a concept called root, which can be followed by some other word to form another longer word - let's call this word derivative. For example, when the root "help" is followed by the word "ful", we can form a derivative "helpful".

+ +

Given a dictionary consisting of many roots and a sentence consisting of words separated by spaces, replace all the derivatives in the sentence with the root forming it. If a derivative can be replaced by more than one root, replace it with the root that has the shortest length.

+ +

Return the sentence after the replacement.

+ +

 

+

Example 1:

+ +
Input: dictionary = ["cat","bat","rat"], sentence = "the cattle was rattled by the battery"
+Output: "the cat was rat by the bat"
+
+ +

Example 2:

+ +
Input: dictionary = ["a","b","c"], sentence = "aadsfasf absbs bbab cadsfafs"
+Output: "a a b c"
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= dictionary.length <= 1000
  • +
  • 1 <= dictionary[i].length <= 100
  • +
  • dictionary[i] consists of only lower-case letters.
  • +
  • 1 <= sentence.length <= 106
  • +
  • sentence consists of only lower-case letters and spaces.
  • +
  • The number of words in sentence is in the range [1, 1000]
  • +
  • The length of each word in sentence is in the range [1, 1000]
  • +
  • Every two consecutive words in sentence will be separated by exactly one space.
  • +
  • sentence does not have leading or trailing spaces.
  • +
+
\ No newline at end of file From ef0810ff78323b32df4452e74b32884c58270275 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 7 Jun 2024 22:18:42 +0530 Subject: [PATCH 1091/1399] Time: 63 ms (91.05%), Space: 69.5 MB (77.56%) - LeetHub --- 0648-replace-words/0648-replace-words.cpp | 110 ++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 0648-replace-words/0648-replace-words.cpp diff --git a/0648-replace-words/0648-replace-words.cpp b/0648-replace-words/0648-replace-words.cpp new file mode 100644 index 00000000..c01b7524 --- /dev/null +++ b/0648-replace-words/0648-replace-words.cpp @@ -0,0 +1,110 @@ +class Node +{ + private: + Node* child[26] = {nullptr}; + bool isWord = false; + + public: + bool containsKey(char ch) + { + return child[ch-'a'] != nullptr; + } + + Node* get(char ch) + { + return child[ch-'a']; + } + + void put(char ch, Node* newNode) + { + child[ch-'a'] = newNode; + } + + void setEnd() + { + isWord = true; + } + + bool isEnd() + { + return isWord; + } +}; + +class Trie{ + private: + Node* root; + + public: + Trie() + { + root = new Node(); + } + + void insert(string& word) + { + int n = word.size(); + Node* temp = root; + + for(int i = 0; i < n; ++i) + { + if(!temp->containsKey(word[i])) + temp->put(word[i], new Node()); + temp = temp->get(word[i]); + } + temp->setEnd(); + } + + string search(string& word) + { + int n = word.size(); + Node* temp = root; + string ans; + + for(int i = 0; i < n; ++i) + { + if(temp->containsKey(word[i])) + { + ans += word[i]; + } + else + return "-1"; + + temp = temp->get(word[i]); + + if(temp->isEnd()) + return ans; + } + + return "-1"; + } +}; + +class Solution { +public: + string replaceWords(vector& dictionary, string sentence) { + + Trie *trie = new Trie(); + + stringstream ss(sentence); + string word, ans; + + for(auto& str : dictionary) + trie->insert(str); + + while(ss >> word) + { + string curr = trie->search(word); + + if(curr != "-1") + ans += curr; + else + ans += word; + ans += " "; + } + + ans.pop_back(); + + return ans; + } +}; \ No newline at end of file From 5793a1ec19645481143731883569d88ea1e498ce Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 7 Jun 2024 22:20:06 +0530 Subject: [PATCH 1092/1399] Attach NOTES - LeetHub --- 0648-replace-words/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0648-replace-words/NOTES.md diff --git a/0648-replace-words/NOTES.md b/0648-replace-words/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0648-replace-words/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 2a7e1be07a41472c01c556622702ebba558e8c5a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 7 Jun 2024 22:20:09 +0530 Subject: [PATCH 1093/1399] Time: 63 ms (91.05%), Space: 69.5 MB (77.56%) - LeetHub From f0ba1158554a6ffe5014cfeec8f907b1ba68e105 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 8 Jun 2024 11:52:21 +0530 Subject: [PATCH 1094/1399] Create README - LeetHub --- 0523-continuous-subarray-sum/README.md | 48 ++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 0523-continuous-subarray-sum/README.md diff --git a/0523-continuous-subarray-sum/README.md b/0523-continuous-subarray-sum/README.md new file mode 100644 index 00000000..0bf5d8e1 --- /dev/null +++ b/0523-continuous-subarray-sum/README.md @@ -0,0 +1,48 @@ +

523. Continuous Subarray Sum

Medium


Given an integer array nums and an integer k, return true if nums has a good subarray or false otherwise.

+ +

A good subarray is a subarray where:

+ +
    +
  • its length is at least two, and
  • +
  • the sum of the elements of the subarray is a multiple of k.
  • +
+ +

Note that:

+ +
    +
  • A subarray is a contiguous part of the array.
  • +
  • An integer x is a multiple of k if there exists an integer n such that x = n * k. 0 is always a multiple of k.
  • +
+ +

 

+

Example 1:

+ +
Input: nums = [23,2,4,6,7], k = 6
+Output: true
+Explanation: [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
+
+ +

Example 2:

+ +
Input: nums = [23,2,6,4,7], k = 6
+Output: true
+Explanation: [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
+42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
+
+ +

Example 3:

+ +
Input: nums = [23,2,6,4,7], k = 13
+Output: false
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 105
  • +
  • 0 <= nums[i] <= 109
  • +
  • 0 <= sum(nums[i]) <= 231 - 1
  • +
  • 1 <= k <= 231 - 1
  • +
+
\ No newline at end of file From 7beb6d732ff73d015c757b4a0780b574c426f220 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 8 Jun 2024 11:52:21 +0530 Subject: [PATCH 1095/1399] Attach NOTES - LeetHub --- 0523-continuous-subarray-sum/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0523-continuous-subarray-sum/NOTES.md diff --git a/0523-continuous-subarray-sum/NOTES.md b/0523-continuous-subarray-sum/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0523-continuous-subarray-sum/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From ac1db0e1b26380cbf85b726da4ea127ab3c29afc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 8 Jun 2024 11:52:25 +0530 Subject: [PATCH 1096/1399] Time: 245 ms (13.66%), Space: 135.3 MB (14.02%) - LeetHub --- .../0523-continuous-subarray-sum.cpp | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 0523-continuous-subarray-sum/0523-continuous-subarray-sum.cpp diff --git a/0523-continuous-subarray-sum/0523-continuous-subarray-sum.cpp b/0523-continuous-subarray-sum/0523-continuous-subarray-sum.cpp new file mode 100644 index 00000000..02ceceb8 --- /dev/null +++ b/0523-continuous-subarray-sum/0523-continuous-subarray-sum.cpp @@ -0,0 +1,28 @@ +class Solution { +public: + bool checkSubarraySum(vector& nums, int k) { + + map mp; + mp.insert({0,-1}); + + int total = 0; + int n = nums.size(); + + for(int i = 0; i < n; ++i) + { + total += nums[i]; + int rem = total % k; + + if(mp.find(rem) != mp.end()) + { + if(i - mp[rem] > 1) + return true; + } + else + { + mp[rem] = i; + } + } + return false; + } +}; \ No newline at end of file From 5955a0058fdb488d8a38bd8d187acb702c995339 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 9 Jun 2024 21:44:36 +0530 Subject: [PATCH 1097/1399] Attach NOTES - LeetHub From c5f8ff0d5f11e9a75ce7e0d2147a88de8fb1fac8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 9 Jun 2024 21:44:40 +0530 Subject: [PATCH 1098/1399] Time: 255 ms (9.42%), Space: 135.2 MB (19.20%) - LeetHub --- .../0523-continuous-subarray-sum.cpp | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/0523-continuous-subarray-sum/0523-continuous-subarray-sum.cpp b/0523-continuous-subarray-sum/0523-continuous-subarray-sum.cpp index 02ceceb8..47037b9c 100644 --- a/0523-continuous-subarray-sum/0523-continuous-subarray-sum.cpp +++ b/0523-continuous-subarray-sum/0523-continuous-subarray-sum.cpp @@ -2,16 +2,19 @@ class Solution { public: bool checkSubarraySum(vector& nums, int k) { - map mp; - mp.insert({0,-1}); - - int total = 0; int n = nums.size(); + map mp; + + int sum = 0; + + mp[0] = -1; + for(int i = 0; i < n; ++i) { - total += nums[i]; - int rem = total % k; + sum += nums[i]; + + int rem = sum % k; if(mp.find(rem) != mp.end()) { @@ -19,10 +22,9 @@ class Solution { return true; } else - { mp[rem] = i; - } } + return false; } }; \ No newline at end of file From 00c54433484ba26a5f9ade85b443804567446484 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 10 Jun 2024 11:34:19 +0530 Subject: [PATCH 1099/1399] Create README - LeetHub --- 1051-height-checker/README.md | 45 +++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 1051-height-checker/README.md diff --git a/1051-height-checker/README.md b/1051-height-checker/README.md new file mode 100644 index 00000000..59363b7e --- /dev/null +++ b/1051-height-checker/README.md @@ -0,0 +1,45 @@ +

1051. Height Checker

Easy


A school is trying to take an annual photo of all the students. The students are asked to stand in a single file line in non-decreasing order by height. Let this ordering be represented by the integer array expected where expected[i] is the expected height of the ith student in line.

+ +

You are given an integer array heights representing the current order that the students are standing in. Each heights[i] is the height of the ith student in line (0-indexed).

+ +

Return the number of indices where heights[i] != expected[i].

+ +

 

+

Example 1:

+ +
Input: heights = [1,1,4,2,1,3]
+Output: 3
+Explanation: 
+heights:  [1,1,4,2,1,3]
+expected: [1,1,1,2,3,4]
+Indices 2, 4, and 5 do not match.
+
+ +

Example 2:

+ +
Input: heights = [5,1,2,3,4]
+Output: 5
+Explanation:
+heights:  [5,1,2,3,4]
+expected: [1,2,3,4,5]
+All indices do not match.
+
+ +

Example 3:

+ +
Input: heights = [1,2,3,4,5]
+Output: 0
+Explanation:
+heights:  [1,2,3,4,5]
+expected: [1,2,3,4,5]
+All indices match.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= heights.length <= 100
  • +
  • 1 <= heights[i] <= 100
  • +
+
\ No newline at end of file From 436ac288fe38cd262d2d71993a47114f49107016 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 10 Jun 2024 11:34:23 +0530 Subject: [PATCH 1100/1399] Time: 2 ms (48.33%), Space: 10 MB (78.64%) - LeetHub --- 1051-height-checker/1051-height-checker.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 1051-height-checker/1051-height-checker.cpp diff --git a/1051-height-checker/1051-height-checker.cpp b/1051-height-checker/1051-height-checker.cpp new file mode 100644 index 00000000..d54f8363 --- /dev/null +++ b/1051-height-checker/1051-height-checker.cpp @@ -0,0 +1,21 @@ +class Solution { +public: + int heightChecker(vector& heights) { + + int n = heights.size(); + + vector copy = heights; + + sort(copy.begin(), copy.end()); + + int cnt = 0; + + for(int i = 0; i < n; ++i) + { + cnt += (heights[i] != copy[i]); + } + + return cnt; + + } +}; \ No newline at end of file From 7cbd875d13cec749b97b1e3abfc753f5d74433db Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 11 Jun 2024 22:41:37 +0530 Subject: [PATCH 1101/1399] Create README - LeetHub --- 1122-relative-sort-array/README.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 1122-relative-sort-array/README.md diff --git a/1122-relative-sort-array/README.md b/1122-relative-sort-array/README.md new file mode 100644 index 00000000..fabd6b8c --- /dev/null +++ b/1122-relative-sort-array/README.md @@ -0,0 +1,27 @@ +

1122. Relative Sort Array

Easy


Given two arrays arr1 and arr2, the elements of arr2 are distinct, and all elements in arr2 are also in arr1.

+ +

Sort the elements of arr1 such that the relative ordering of items in arr1 are the same as in arr2. Elements that do not appear in arr2 should be placed at the end of arr1 in ascending order.

+ +

 

+

Example 1:

+ +
Input: arr1 = [2,3,1,3,2,4,6,7,9,2,19], arr2 = [2,1,4,3,9,6]
+Output: [2,2,2,1,4,3,3,9,6,7,19]
+
+ +

Example 2:

+ +
Input: arr1 = [28,6,22,8,44,17], arr2 = [22,28,8,6]
+Output: [22,28,8,6,17,44]
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= arr1.length, arr2.length <= 1000
  • +
  • 0 <= arr1[i], arr2[i] <= 1000
  • +
  • All the elements of arr2 are distinct.
  • +
  • Each arr2[i] is in arr1.
  • +
+
\ No newline at end of file From b6f37dcf28be3e18cc387371e6b0fef8c0dbdd60 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 11 Jun 2024 22:41:41 +0530 Subject: [PATCH 1102/1399] Time: 0 ms (100.00%), Space: 10 MB (54.14%) - LeetHub --- .../1122-relative-sort-array.cpp | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 1122-relative-sort-array/1122-relative-sort-array.cpp diff --git a/1122-relative-sort-array/1122-relative-sort-array.cpp b/1122-relative-sort-array/1122-relative-sort-array.cpp new file mode 100644 index 00000000..e9aae6f3 --- /dev/null +++ b/1122-relative-sort-array/1122-relative-sort-array.cpp @@ -0,0 +1,29 @@ +class Solution { +public: + vector relativeSortArray(vector& arr1, vector& arr2) { + + map mp; + + for(auto& ele : arr1) + ++mp[ele]; + + int j = 0; + + for(int i = 0; i < arr2.size(); ++i) + { + for(int k = 0; k < mp[arr2[i]]; ++k) + { + arr1[j++] = arr2[i]; + } + mp.erase(arr2[i]); + } + + for(auto&[f, e] : mp) + { + for(int k = 0; k < e; ++k) + arr1[j++] = f; + } + + return arr1; + } +}; \ No newline at end of file From 96e19a9141b17d0d63d64e91b069e501d80f29a8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 11 Jun 2024 22:42:34 +0530 Subject: [PATCH 1103/1399] Attach NOTES - LeetHub --- 1122-relative-sort-array/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1122-relative-sort-array/NOTES.md diff --git a/1122-relative-sort-array/NOTES.md b/1122-relative-sort-array/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1122-relative-sort-array/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 9e4fbef810a563e5b7f82b653ef6a7ed7a029820 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 11 Jun 2024 22:42:37 +0530 Subject: [PATCH 1104/1399] Time: 0 ms (100.00%), Space: 10 MB (54.14%) - LeetHub From cc0d00fa37af44acf47327417e2d41fd839bf1d6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 12 Jun 2024 08:47:09 +0530 Subject: [PATCH 1105/1399] Create README - LeetHub --- .../README.md | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 1378-replace-employee-id-with-the-unique-identifier/README.md diff --git a/1378-replace-employee-id-with-the-unique-identifier/README.md b/1378-replace-employee-id-with-the-unique-identifier/README.md new file mode 100644 index 00000000..283a0d5d --- /dev/null +++ b/1378-replace-employee-id-with-the-unique-identifier/README.md @@ -0,0 +1,73 @@ +

1378. Replace Employee ID With The Unique Identifier

Easy


Table: Employees

+ +
+---------------+---------+
+| Column Name   | Type    |
++---------------+---------+
+| id            | int     |
+| name          | varchar |
++---------------+---------+
+id is the primary key (column with unique values) for this table.
+Each row of this table contains the id and the name of an employee in a company.
+
+ +

 

+ +

Table: EmployeeUNI

+ +
+---------------+---------+
+| Column Name   | Type    |
++---------------+---------+
+| id            | int     |
+| unique_id     | int     |
++---------------+---------+
+(id, unique_id) is the primary key (combination of columns with unique values) for this table.
+Each row of this table contains the id and the corresponding unique id of an employee in the company.
+
+ +

 

+ +

Write a solution to show the unique ID of each user, If a user does not have a unique ID replace just show null.

+ +

Return the result table in any order.

+ +

The result format is in the following example.

+ +

 

+

Example 1:

+ +
Input: 
+Employees table:
++----+----------+
+| id | name     |
++----+----------+
+| 1  | Alice    |
+| 7  | Bob      |
+| 11 | Meir     |
+| 90 | Winston  |
+| 3  | Jonathan |
++----+----------+
+EmployeeUNI table:
++----+-----------+
+| id | unique_id |
++----+-----------+
+| 3  | 1         |
+| 11 | 2         |
+| 90 | 3         |
++----+-----------+
+Output: 
++-----------+----------+
+| unique_id | name     |
++-----------+----------+
+| null      | Alice    |
+| null      | Bob      |
+| 2         | Meir     |
+| 3         | Winston  |
+| 1         | Jonathan |
++-----------+----------+
+Explanation: 
+Alice and Bob do not have a unique ID, We will show null instead.
+The unique ID of Meir is 2.
+The unique ID of Winston is 3.
+The unique ID of Jonathan is 1.
+
+
\ No newline at end of file From 6cc21e3164723307d8edd7c765b631f8c8774b26 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 12 Jun 2024 08:47:12 +0530 Subject: [PATCH 1106/1399] Time: 1187 ms (93.44%), Space: 0B (100.00%) - LeetHub --- .../1378-replace-employee-id-with-the-unique-identifier.sql | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 1378-replace-employee-id-with-the-unique-identifier/1378-replace-employee-id-with-the-unique-identifier.sql diff --git a/1378-replace-employee-id-with-the-unique-identifier/1378-replace-employee-id-with-the-unique-identifier.sql b/1378-replace-employee-id-with-the-unique-identifier/1378-replace-employee-id-with-the-unique-identifier.sql new file mode 100644 index 00000000..9fce03ec --- /dev/null +++ b/1378-replace-employee-id-with-the-unique-identifier/1378-replace-employee-id-with-the-unique-identifier.sql @@ -0,0 +1,6 @@ +# Write your MySQL query statement below + + +select e2.unique_id , e1.name from Employees e1 +left join EmployeeUNI e2 +on e1.id = e2.id; \ No newline at end of file From 5af133da31f08b0c2eaef1dfcdd7394f68724cbe Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 12 Jun 2024 08:50:18 +0530 Subject: [PATCH 1107/1399] Attach NOTES - LeetHub --- 1378-replace-employee-id-with-the-unique-identifier/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1378-replace-employee-id-with-the-unique-identifier/NOTES.md diff --git a/1378-replace-employee-id-with-the-unique-identifier/NOTES.md b/1378-replace-employee-id-with-the-unique-identifier/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1378-replace-employee-id-with-the-unique-identifier/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From d27f02d8322144bddf3fb2b9a1aa38f1bacb0974 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 12 Jun 2024 08:50:21 +0530 Subject: [PATCH 1108/1399] Time: 1187 ms (93.44%), Space: 0B (100.00%) - LeetHub From 3305fa2becd3adeb2e087c2c35c620e42e71d5c0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 13 Jun 2024 07:18:10 +0530 Subject: [PATCH 1109/1399] Create README - LeetHub --- 1068-product-sales-analysis-i/README.md | 73 +++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 1068-product-sales-analysis-i/README.md diff --git a/1068-product-sales-analysis-i/README.md b/1068-product-sales-analysis-i/README.md new file mode 100644 index 00000000..cfcc2de3 --- /dev/null +++ b/1068-product-sales-analysis-i/README.md @@ -0,0 +1,73 @@ +

1068. Product Sales Analysis I

Easy


Table: Sales

+ +
+-------------+-------+
+| Column Name | Type  |
++-------------+-------+
+| sale_id     | int   |
+| product_id  | int   |
+| year        | int   |
+| quantity    | int   |
+| price       | int   |
++-------------+-------+
+(sale_id, year) is the primary key (combination of columns with unique values) of this table.
+product_id is a foreign key (reference column) to Product table.
+Each row of this table shows a sale on the product product_id in a certain year.
+Note that the price is per unit.
+
+ +

 

+ +

Table: Product

+ +
+--------------+---------+
+| Column Name  | Type    |
++--------------+---------+
+| product_id   | int     |
+| product_name | varchar |
++--------------+---------+
+product_id is the primary key (column with unique values) of this table.
+Each row of this table indicates the product name of each product.
+
+ +

 

+ +

Write a solution to report the product_name, year, and price for each sale_id in the Sales table.

+ +

Return the resulting table in any order.

+ +

The result format is in the following example.

+ +

 

+

Example 1:

+ +
Input: 
+Sales table:
++---------+------------+------+----------+-------+
+| sale_id | product_id | year | quantity | price |
++---------+------------+------+----------+-------+ 
+| 1       | 100        | 2008 | 10       | 5000  |
+| 2       | 100        | 2009 | 12       | 5000  |
+| 7       | 200        | 2011 | 15       | 9000  |
++---------+------------+------+----------+-------+
+Product table:
++------------+--------------+
+| product_id | product_name |
++------------+--------------+
+| 100        | Nokia        |
+| 200        | Apple        |
+| 300        | Samsung      |
++------------+--------------+
+Output: 
++--------------+-------+-------+
+| product_name | year  | price |
++--------------+-------+-------+
+| Nokia        | 2008  | 5000  |
+| Nokia        | 2009  | 5000  |
+| Apple        | 2011  | 9000  |
++--------------+-------+-------+
+Explanation: 
+From sale_id = 1, we can conclude that Nokia was sold for 5000 in the year 2008.
+From sale_id = 2, we can conclude that Nokia was sold for 5000 in the year 2009.
+From sale_id = 7, we can conclude that Apple was sold for 9000 in the year 2011.
+
+
\ No newline at end of file From 4a03bfb5f63fbd9b0d8883544414742202074778 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 13 Jun 2024 07:18:13 +0530 Subject: [PATCH 1110/1399] Time: 1460 ms (89.73%), Space: 0B (100.00%) - LeetHub --- .../1068-product-sales-analysis-i.sql | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 1068-product-sales-analysis-i/1068-product-sales-analysis-i.sql diff --git a/1068-product-sales-analysis-i/1068-product-sales-analysis-i.sql b/1068-product-sales-analysis-i/1068-product-sales-analysis-i.sql new file mode 100644 index 00000000..37c91497 --- /dev/null +++ b/1068-product-sales-analysis-i/1068-product-sales-analysis-i.sql @@ -0,0 +1,6 @@ +# Write your MySQL query statement below + + +select Product.product_name, Sales.year, Sales.price from Sales +left join Product on +Sales.product_id = Product.product_id; \ No newline at end of file From 99e94ccbe3dd88c5968df49ca448ec24a65a9b17 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 13 Jun 2024 07:19:15 +0530 Subject: [PATCH 1111/1399] Attach NOTES - LeetHub --- 1068-product-sales-analysis-i/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1068-product-sales-analysis-i/NOTES.md diff --git a/1068-product-sales-analysis-i/NOTES.md b/1068-product-sales-analysis-i/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1068-product-sales-analysis-i/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 808bbfe8ca371a68da95a23ac0e699029e063f81 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 13 Jun 2024 07:19:18 +0530 Subject: [PATCH 1112/1399] Time: 1460 ms (89.73%), Space: 0B (100.00%) - LeetHub From 7997bfc1f1f999e9c34874c0e3849c05439c56a7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 13 Jun 2024 07:22:20 +0530 Subject: [PATCH 1113/1399] Attach NOTES - LeetHub From 993f90262c34c64bf2e513596e73d844b34fa967 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 13 Jun 2024 07:22:23 +0530 Subject: [PATCH 1114/1399] Time: 1460 ms (89.73%), Space: 0B (100.00%) - LeetHub From a22b1578ecae82a8074a1d2bb77490b6ff306fbf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 13 Jun 2024 07:22:38 +0530 Subject: [PATCH 1115/1399] Attach NOTES - LeetHub From f44d1d1be51bfaa5e512d2edc490bc3a5dbb8175 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 13 Jun 2024 07:22:42 +0530 Subject: [PATCH 1116/1399] Time: 1266 ms (92.76%), Space: 0B (100.00%) - LeetHub --- 1068-product-sales-analysis-i/1068-product-sales-analysis-i.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1068-product-sales-analysis-i/1068-product-sales-analysis-i.sql b/1068-product-sales-analysis-i/1068-product-sales-analysis-i.sql index 37c91497..e4ba918b 100644 --- a/1068-product-sales-analysis-i/1068-product-sales-analysis-i.sql +++ b/1068-product-sales-analysis-i/1068-product-sales-analysis-i.sql @@ -2,5 +2,5 @@ select Product.product_name, Sales.year, Sales.price from Sales -left join Product on +inner join Product on Sales.product_id = Product.product_id; \ No newline at end of file From 9589ecae0a97153cd563b97686f3cb6137c28395 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 13 Jun 2024 07:22:47 +0530 Subject: [PATCH 1117/1399] Attach NOTES - LeetHub From d8e3899f17b07d707078e7b6a040d535bf88c159 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 13 Jun 2024 07:22:49 +0530 Subject: [PATCH 1118/1399] Attach NOTES - LeetHub From 2d4cca78e881433bd6bf387188dc8af8bfb5524f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 13 Jun 2024 07:22:51 +0530 Subject: [PATCH 1119/1399] Time: 1266 ms (92.76%), Space: 0B (100.00%) - LeetHub From c4eba70367cddad18331cd9970ed10fde368bdce Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 13 Jun 2024 07:22:53 +0530 Subject: [PATCH 1120/1399] Time: 1266 ms (92.76%), Space: 0B (100.00%) - LeetHub From 26d79b7df9d2284704963a24842c4508ece80a1a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 13 Jun 2024 07:22:54 +0530 Subject: [PATCH 1121/1399] Time: 1266 ms (92.76%), Space: 0B (100.00%) - LeetHub From 17da11ae5ce675c7e4db88354d1a7dc30ed80200 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 13 Jun 2024 07:23:17 +0530 Subject: [PATCH 1122/1399] Attach NOTES - LeetHub From 5be3b7102f109d89e2a8874d1a89e22153ac13c5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 13 Jun 2024 07:23:21 +0530 Subject: [PATCH 1123/1399] Time: 1266 ms (92.76%), Space: 0B (100.00%) - LeetHub From 3cf3132ea85961e675319884084935d295770c71 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 13 Jun 2024 07:52:01 +0530 Subject: [PATCH 1124/1399] Create README - LeetHub --- .../README.md | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 2037-minimum-number-of-moves-to-seat-everyone/README.md diff --git a/2037-minimum-number-of-moves-to-seat-everyone/README.md b/2037-minimum-number-of-moves-to-seat-everyone/README.md new file mode 100644 index 00000000..04a8e685 --- /dev/null +++ b/2037-minimum-number-of-moves-to-seat-everyone/README.md @@ -0,0 +1,58 @@ +

2037. Minimum Number of Moves to Seat Everyone

Easy


There are n seats and n students in a room. You are given an array seats of length n, where seats[i] is the position of the ith seat. You are also given the array students of length n, where students[j] is the position of the jth student.

+ +

You may perform the following move any number of times:

+ +
    +
  • Increase or decrease the position of the ith student by 1 (i.e., moving the ith student from position x to x + 1 or x - 1)
  • +
+ +

Return the minimum number of moves required to move each student to a seat such that no two students are in the same seat.

+ +

Note that there may be multiple seats or students in the same position at the beginning.

+ +

 

+

Example 1:

+ +
Input: seats = [3,1,5], students = [2,7,4]
+Output: 4
+Explanation: The students are moved as follows:
+- The first student is moved from from position 2 to position 1 using 1 move.
+- The second student is moved from from position 7 to position 5 using 2 moves.
+- The third student is moved from from position 4 to position 3 using 1 move.
+In total, 1 + 2 + 1 = 4 moves were used.
+
+ +

Example 2:

+ +
Input: seats = [4,1,5,9], students = [1,3,2,6]
+Output: 7
+Explanation: The students are moved as follows:
+- The first student is not moved.
+- The second student is moved from from position 3 to position 4 using 1 move.
+- The third student is moved from from position 2 to position 5 using 3 moves.
+- The fourth student is moved from from position 6 to position 9 using 3 moves.
+In total, 0 + 1 + 3 + 3 = 7 moves were used.
+
+ +

Example 3:

+ +
Input: seats = [2,2,6,6], students = [1,3,2,6]
+Output: 4
+Explanation: Note that there are two seats at position 2 and two seats at position 6.
+The students are moved as follows:
+- The first student is moved from from position 1 to position 2 using 1 move.
+- The second student is moved from from position 3 to position 6 using 3 moves.
+- The third student is not moved.
+- The fourth student is not moved.
+In total, 1 + 3 + 0 + 0 = 4 moves were used.
+
+ +

 

+

Constraints:

+ +
    +
  • n == seats.length == students.length
  • +
  • 1 <= n <= 100
  • +
  • 1 <= seats[i], students[j] <= 100
  • +
+
\ No newline at end of file From c0467b13265a16499c6aef72e1891dbe85ca656a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 13 Jun 2024 07:52:04 +0530 Subject: [PATCH 1125/1399] Time: 4 ms (74.53%), Space: 21 MB (17.35%) - LeetHub --- ...nimum-number-of-moves-to-seat-everyone.cpp | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 2037-minimum-number-of-moves-to-seat-everyone/2037-minimum-number-of-moves-to-seat-everyone.cpp diff --git a/2037-minimum-number-of-moves-to-seat-everyone/2037-minimum-number-of-moves-to-seat-everyone.cpp b/2037-minimum-number-of-moves-to-seat-everyone/2037-minimum-number-of-moves-to-seat-everyone.cpp new file mode 100644 index 00000000..6bbe6bac --- /dev/null +++ b/2037-minimum-number-of-moves-to-seat-everyone/2037-minimum-number-of-moves-to-seat-everyone.cpp @@ -0,0 +1,34 @@ +class Solution { +public: + int minMovesToSeat(vector& seats, vector& students) { + + int n = seats.size(); + + sort(seats.begin(), seats.end()); + sort(students.begin(), students.end()); + + int st = 0, end = n-1; + + int moves = 0; + + while(st <= end) + { + int f = abs(seats[st] - students[st]); + int s = abs(seats[end] - students[end]); + + if(f < s) + { + moves += f; + ++st; + } + else + { + moves += s; + --end; + } + } + + return moves; + + } +}; \ No newline at end of file From dc00e736d4e4265945ba2752416c84cb2884e335 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 13 Jun 2024 07:53:26 +0530 Subject: [PATCH 1126/1399] Attach NOTES - LeetHub --- 2037-minimum-number-of-moves-to-seat-everyone/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2037-minimum-number-of-moves-to-seat-everyone/NOTES.md diff --git a/2037-minimum-number-of-moves-to-seat-everyone/NOTES.md b/2037-minimum-number-of-moves-to-seat-everyone/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2037-minimum-number-of-moves-to-seat-everyone/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From a0ba302ea7de2c4d60591b5f6d0af7a277914203 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 13 Jun 2024 07:53:28 +0530 Subject: [PATCH 1127/1399] Time: 7 ms (60.18%), Space: 21 MB (52.06%) - LeetHub --- ...inimum-number-of-moves-to-seat-everyone.cpp | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) diff --git a/2037-minimum-number-of-moves-to-seat-everyone/2037-minimum-number-of-moves-to-seat-everyone.cpp b/2037-minimum-number-of-moves-to-seat-everyone/2037-minimum-number-of-moves-to-seat-everyone.cpp index 6bbe6bac..ffdf2002 100644 --- a/2037-minimum-number-of-moves-to-seat-everyone/2037-minimum-number-of-moves-to-seat-everyone.cpp +++ b/2037-minimum-number-of-moves-to-seat-everyone/2037-minimum-number-of-moves-to-seat-everyone.cpp @@ -7,25 +7,11 @@ class Solution { sort(seats.begin(), seats.end()); sort(students.begin(), students.end()); - int st = 0, end = n-1; - int moves = 0; - while(st <= end) + for(int i = 0; i < n; ++i) { - int f = abs(seats[st] - students[st]); - int s = abs(seats[end] - students[end]); - - if(f < s) - { - moves += f; - ++st; - } - else - { - moves += s; - --end; - } + moves += abs(seats[i] - students[i]); } return moves; From 08d478f987fad7bea0a18d9bf7d41a62f7557431 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 13 Jun 2024 07:54:02 +0530 Subject: [PATCH 1128/1399] Attach NOTES - LeetHub From 1ecf17d3278ea2e467afdde2c857362bb41b5917 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 14 Jun 2024 08:16:16 +0530 Subject: [PATCH 1129/1399] Create README - LeetHub --- .../README.md | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 0945-minimum-increment-to-make-array-unique/README.md diff --git a/0945-minimum-increment-to-make-array-unique/README.md b/0945-minimum-increment-to-make-array-unique/README.md new file mode 100644 index 00000000..b81b99e4 --- /dev/null +++ b/0945-minimum-increment-to-make-array-unique/README.md @@ -0,0 +1,30 @@ +

945. Minimum Increment to Make Array Unique

Medium


You are given an integer array nums. In one move, you can pick an index i where 0 <= i < nums.length and increment nums[i] by 1.

+ +

Return the minimum number of moves to make every value in nums unique.

+ +

The test cases are generated so that the answer fits in a 32-bit integer.

+ +

 

+

Example 1:

+ +
Input: nums = [1,2,2]
+Output: 1
+Explanation: After 1 move, the array could be [1, 2, 3].
+
+ +

Example 2:

+ +
Input: nums = [3,2,1,2,1,7]
+Output: 6
+Explanation: After 6 moves, the array could be [3, 4, 1, 2, 5, 7].
+It can be shown with 5 or less moves that it is impossible for the array to have all unique values.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 105
  • +
  • 0 <= nums[i] <= 105
  • +
+
\ No newline at end of file From 66873f7d2fb29682a10ba892c6efbc798bd7005c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 14 Jun 2024 08:16:18 +0530 Subject: [PATCH 1130/1399] Attach NOTES - LeetHub --- 0945-minimum-increment-to-make-array-unique/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0945-minimum-increment-to-make-array-unique/NOTES.md diff --git a/0945-minimum-increment-to-make-array-unique/NOTES.md b/0945-minimum-increment-to-make-array-unique/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0945-minimum-increment-to-make-array-unique/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 1b5f48bec9c7620c92f0d835fe7c54b55fea1f59 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 14 Jun 2024 08:16:21 +0530 Subject: [PATCH 1131/1399] Time: 125 ms (42.72%), Space: 69.1 MB (79.13%) - LeetHub --- ...minimum-increment-to-make-array-unique.cpp | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 0945-minimum-increment-to-make-array-unique/0945-minimum-increment-to-make-array-unique.cpp diff --git a/0945-minimum-increment-to-make-array-unique/0945-minimum-increment-to-make-array-unique.cpp b/0945-minimum-increment-to-make-array-unique/0945-minimum-increment-to-make-array-unique.cpp new file mode 100644 index 00000000..f03f57f6 --- /dev/null +++ b/0945-minimum-increment-to-make-array-unique/0945-minimum-increment-to-make-array-unique.cpp @@ -0,0 +1,25 @@ +class Solution { +public: + int minIncrementForUnique(vector& nums) { + + int n = nums.size(); + + sort(nums.begin(), nums.end()); + + int curr = nums[0], ans = 0; + + for(int i = 1; i < n; ++i) + { + if(nums[i] <= curr) + { + int val = abs(nums[i] - curr) + 1; + ans += val; + nums[i] += val; + } + curr = nums[i]; + } + + return ans; + + } +}; \ No newline at end of file From 2f36d465d4ab3e176368f141c848e62c0e443e79 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 14 Jun 2024 08:19:29 +0530 Subject: [PATCH 1132/1399] Attach NOTES - LeetHub From 6d88fbbd24d38a665b962afed3a7e3b0d552e9d0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 14 Jun 2024 08:19:31 +0530 Subject: [PATCH 1133/1399] Time: 119 ms (62.01%), Space: 69.1 MB (50.39%) - LeetHub --- .../0945-minimum-increment-to-make-array-unique.cpp | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/0945-minimum-increment-to-make-array-unique/0945-minimum-increment-to-make-array-unique.cpp b/0945-minimum-increment-to-make-array-unique/0945-minimum-increment-to-make-array-unique.cpp index f03f57f6..c46746a6 100644 --- a/0945-minimum-increment-to-make-array-unique/0945-minimum-increment-to-make-array-unique.cpp +++ b/0945-minimum-increment-to-make-array-unique/0945-minimum-increment-to-make-array-unique.cpp @@ -6,17 +6,15 @@ class Solution { sort(nums.begin(), nums.end()); - int curr = nums[0], ans = 0; + int ans = 0; for(int i = 1; i < n; ++i) { - if(nums[i] <= curr) + if(nums[i-1] >= nums[i]) { - int val = abs(nums[i] - curr) + 1; - ans += val; - nums[i] += val; + ans += (nums[i-1] - nums[i] + 1); + nums[i] = nums[i-1] + 1; } - curr = nums[i]; } return ans; From ab6aade5c1210b8bb53444210dca211d23a83bbf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 15 Jun 2024 08:17:32 +0530 Subject: [PATCH 1134/1399] Attach NOTES - LeetHub --- 0502-ipo/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0502-ipo/NOTES.md diff --git a/0502-ipo/NOTES.md b/0502-ipo/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0502-ipo/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 00282fbf024772d0b0a6bf09a48d429878bf5795 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Jun 2024 07:14:03 +0530 Subject: [PATCH 1135/1399] Create README - LeetHub --- .../README.md | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 3186-maximum-total-damage-with-spell-casting/README.md diff --git a/3186-maximum-total-damage-with-spell-casting/README.md b/3186-maximum-total-damage-with-spell-casting/README.md new file mode 100644 index 00000000..3408ac47 --- /dev/null +++ b/3186-maximum-total-damage-with-spell-casting/README.md @@ -0,0 +1,43 @@ +

3186. Maximum Total Damage With Spell Casting

Medium


A magician has various spells.

+ +

You are given an array power, where each element represents the damage of a spell. Multiple spells can have the same damage value.

+ +

It is a known fact that if a magician decides to cast a spell with a damage of power[i], they cannot cast any spell with a damage of power[i] - 2, power[i] - 1, power[i] + 1, or power[i] + 2.

+ +

Each spell can be cast only once.

+ +

Return the maximum possible total damage that a magician can cast.

+ +

 

+

Example 1:

+ +
+

Input: power = [1,1,3,4]

+ +

Output: 6

+ +

Explanation:

+ +

The maximum possible damage of 6 is produced by casting spells 0, 1, 3 with damage 1, 1, 4.

+
+ +

Example 2:

+ +
+

Input: power = [7,1,6,6]

+ +

Output: 13

+ +

Explanation:

+ +

The maximum possible damage of 13 is produced by casting spells 1, 2, 3 with damage 1, 6, 6.

+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= power.length <= 105
  • +
  • 1 <= power[i] <= 109
  • +
+
\ No newline at end of file From 28603df185eef832ab5d7e4bc5161ccee786be1b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Jun 2024 07:14:07 +0530 Subject: [PATCH 1136/1399] Time: 627 ms (60.00%), Space: 205 MB (60.00%) - LeetHub --- ...aximum-total-damage-with-spell-casting.cpp | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 3186-maximum-total-damage-with-spell-casting/3186-maximum-total-damage-with-spell-casting.cpp diff --git a/3186-maximum-total-damage-with-spell-casting/3186-maximum-total-damage-with-spell-casting.cpp b/3186-maximum-total-damage-with-spell-casting/3186-maximum-total-damage-with-spell-casting.cpp new file mode 100644 index 00000000..a7c22a40 --- /dev/null +++ b/3186-maximum-total-damage-with-spell-casting/3186-maximum-total-damage-with-spell-casting.cpp @@ -0,0 +1,34 @@ +using ll = long long; + +class Solution { +public: + + long long maximumTotalDamage(vector& power) { + + map mp; + + for(auto& ele : power) + ++mp[ele]; + + vector vec; + + for(auto& [f, _] : mp) + vec.push_back(f); + + vector dp(vec.size()+1, 0); + + for(int i = vec.size()-1; i >= 0; --i) + { + ll notTake = dp[i+1]; + + int ubIdx = upper_bound(vec.begin(), vec.end(), vec[i] + 2) - vec.begin(); + + ll take = vec[i] * 1LL * mp[vec[i]] + dp[ubIdx]; + + dp[i] = max(take, notTake); + } + + return dp[0]; + + } +}; \ No newline at end of file From a2fff22a031139bc2fcf711e966711b989c7d70c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Jun 2024 07:18:23 +0530 Subject: [PATCH 1137/1399] Attach NOTES - LeetHub --- 3186-maximum-total-damage-with-spell-casting/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 3186-maximum-total-damage-with-spell-casting/NOTES.md diff --git a/3186-maximum-total-damage-with-spell-casting/NOTES.md b/3186-maximum-total-damage-with-spell-casting/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/3186-maximum-total-damage-with-spell-casting/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From dedc46e698f7e4efbb890098b7f1d3933f33cd43 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Jun 2024 07:18:26 +0530 Subject: [PATCH 1138/1399] Time: 627 ms (60.00%), Space: 205 MB (60.00%) - LeetHub From d32cbe77911f363c6d16ce95abd43d32954b3bde Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Jun 2024 20:06:56 +0530 Subject: [PATCH 1139/1399] Create README - LeetHub --- 0330-patching-array/README.md | 39 +++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 0330-patching-array/README.md diff --git a/0330-patching-array/README.md b/0330-patching-array/README.md new file mode 100644 index 00000000..fcebede2 --- /dev/null +++ b/0330-patching-array/README.md @@ -0,0 +1,39 @@ +

330. Patching Array

Hard


Given a sorted integer array nums and an integer n, add/patch elements to the array such that any number in the range [1, n] inclusive can be formed by the sum of some elements in the array.

+ +

Return the minimum number of patches required.

+ +

 

+

Example 1:

+ +
Input: nums = [1,3], n = 6
+Output: 1
+Explanation:
+Combinations of nums are [1], [3], [1,3], which form possible sums of: 1, 3, 4.
+Now if we add/patch 2 to nums, the combinations are: [1], [2], [3], [1,3], [2,3], [1,2,3].
+Possible sums are 1, 2, 3, 4, 5, 6, which now covers the range [1, 6].
+So we only need 1 patch.
+
+ +

Example 2:

+ +
Input: nums = [1,5,10], n = 20
+Output: 2
+Explanation: The two patches can be [2, 4].
+
+ +

Example 3:

+ +
Input: nums = [1,2,2], n = 5
+Output: 0
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 1000
  • +
  • 1 <= nums[i] <= 104
  • +
  • nums is sorted in ascending order.
  • +
  • 1 <= n <= 231 - 1
  • +
+
\ No newline at end of file From 2cd7625ddac277df83c9d81ce31d43ce86249f02 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Jun 2024 20:06:59 +0530 Subject: [PATCH 1140/1399] Time: 3 ms (83.06%), Space: 13.7 MB (45.23%) - LeetHub --- 0330-patching-array/0330-patching-array.cpp | 25 +++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 0330-patching-array/0330-patching-array.cpp diff --git a/0330-patching-array/0330-patching-array.cpp b/0330-patching-array/0330-patching-array.cpp new file mode 100644 index 00000000..ae09c27a --- /dev/null +++ b/0330-patching-array/0330-patching-array.cpp @@ -0,0 +1,25 @@ +class Solution { +public: + int minPatches(vector& nums, int n) { + + long long maxReach = 0; + int patch = 0, i = 0; + + while(maxReach < n) + { + if(i < nums.size() and nums[i] <= maxReach + 1) + { + maxReach += nums[i]; + ++i; + } + else + { + ++patch; + maxReach += (maxReach + 1); + } + } + + return patch; + + } +}; \ No newline at end of file From 12912d08023edae08906b4c3cdc7c9bbc2928d90 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Jun 2024 20:40:21 +0530 Subject: [PATCH 1141/1399] Create README - LeetHub --- 0633-sum-of-square-numbers/README.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 0633-sum-of-square-numbers/README.md diff --git a/0633-sum-of-square-numbers/README.md b/0633-sum-of-square-numbers/README.md new file mode 100644 index 00000000..efeadaa1 --- /dev/null +++ b/0633-sum-of-square-numbers/README.md @@ -0,0 +1,23 @@ +

633. Sum of Square Numbers

Medium


Given a non-negative integer c, decide whether there're two integers a and b such that a2 + b2 = c.

+ +

 

+

Example 1:

+ +
Input: c = 5
+Output: true
+Explanation: 1 * 1 + 2 * 2 = 5
+
+ +

Example 2:

+ +
Input: c = 3
+Output: false
+
+ +

 

+

Constraints:

+ +
    +
  • 0 <= c <= 231 - 1
  • +
+
\ No newline at end of file From a4c0672f409ed575beaf1a1d8dd8794ca5d87dd9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Jun 2024 20:40:25 +0530 Subject: [PATCH 1142/1399] Time: 3 ms (68.00%), Space: 7.1 MB (61.35%) - LeetHub --- .../0633-sum-of-square-numbers.cpp | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 0633-sum-of-square-numbers/0633-sum-of-square-numbers.cpp diff --git a/0633-sum-of-square-numbers/0633-sum-of-square-numbers.cpp b/0633-sum-of-square-numbers/0633-sum-of-square-numbers.cpp new file mode 100644 index 00000000..338c2700 --- /dev/null +++ b/0633-sum-of-square-numbers/0633-sum-of-square-numbers.cpp @@ -0,0 +1,23 @@ +using ll = long long; + +class Solution { +public: + bool judgeSquareSum(int c) { + + ll low = 0, high = sqrt(c); + + while(low <= high) + { + ll mid = low * low + high * high; + + if(mid < c) + ++low; + else if(mid > c) + --high; + else + return true; + } + + return false; + } +}; \ No newline at end of file From 9599bffd7f5f89f9cb4a29ba4500fea6fb67f854 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Jun 2024 20:42:39 +0530 Subject: [PATCH 1143/1399] Attach NOTES - LeetHub --- 0633-sum-of-square-numbers/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0633-sum-of-square-numbers/NOTES.md diff --git a/0633-sum-of-square-numbers/NOTES.md b/0633-sum-of-square-numbers/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0633-sum-of-square-numbers/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 98bf2ecc453b13d896a06f94f39302cf46333d14 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Jun 2024 20:42:41 +0530 Subject: [PATCH 1144/1399] Time: 3 ms (68.00%), Space: 7.1 MB (61.35%) - LeetHub From efd0a08fe2ce65ee012c9ddafb06ff60bf160b5e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Jun 2024 20:43:26 +0530 Subject: [PATCH 1145/1399] Attach NOTES - LeetHub From 9117da2948ab3e72aad0a233b4f2981d7cd19ff0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Jun 2024 20:51:02 +0530 Subject: [PATCH 1146/1399] Attach NOTES - LeetHub From ae2634a6a0ef4fabc0eedf3fb99e0e600a85665b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Jun 2024 20:51:05 +0530 Subject: [PATCH 1147/1399] Time: 3 ms (68.00%), Space: 7.1 MB (61.35%) - LeetHub From 5625af51b1130375a954cf6bd1a75bc8e0189114 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Jun 2024 20:54:44 +0530 Subject: [PATCH 1148/1399] Attach NOTES - LeetHub From 33beaa1a9b5494b55f2d4189c6bfe18094be46bf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Jun 2024 20:54:47 +0530 Subject: [PATCH 1149/1399] Time: 3 ms (68.00%), Space: 7.1 MB (61.35%) - LeetHub From c401e6eb9dcf0803ea580b9744c8f2b27cd0ee80 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Jun 2024 20:56:29 +0530 Subject: [PATCH 1150/1399] Attach NOTES - LeetHub From 2b61104f21f0c5a8c8d7b79ae72863d13f634234 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Jun 2024 20:56:32 +0530 Subject: [PATCH 1151/1399] Time: 3 ms (68.00%), Space: 7.1 MB (61.35%) - LeetHub From 45b1f96d59d02b1892a9c854e26bc24a3adfb0bd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Jun 2024 20:56:41 +0530 Subject: [PATCH 1152/1399] Attach NOTES - LeetHub From 5af58bcf621fc3caa8bd8dd0663d55c0d6cdf08f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Jun 2024 20:56:45 +0530 Subject: [PATCH 1153/1399] Time: 3 ms (68.00%), Space: 7.1 MB (61.35%) - LeetHub From 3aa43b69e960a1bdf04d8317f6ba5a6e64fd0443 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Jun 2024 21:02:17 +0530 Subject: [PATCH 1154/1399] Attach NOTES - LeetHub From f6861ab1f6fafe339233f210e46dcbdcf0d84c95 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Jun 2024 21:02:20 +0530 Subject: [PATCH 1155/1399] Time: 49 ms (6.20%), Space: 7 MB (91.95%) - LeetHub --- .../0633-sum-of-square-numbers.cpp | 31 ++++++++++++++----- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/0633-sum-of-square-numbers/0633-sum-of-square-numbers.cpp b/0633-sum-of-square-numbers/0633-sum-of-square-numbers.cpp index 338c2700..bd9769d0 100644 --- a/0633-sum-of-square-numbers/0633-sum-of-square-numbers.cpp +++ b/0633-sum-of-square-numbers/0633-sum-of-square-numbers.cpp @@ -2,22 +2,39 @@ using ll = long long; class Solution { public: - bool judgeSquareSum(int c) { + + bool isPerfectSquare(int num) + { + if(num == 0 or num == 1) + return true; - ll low = 0, high = sqrt(c); + ll low = 0, high = num; while(low <= high) { - ll mid = low * low + high * high; + ll mid = (low + high) / 2; - if(mid < c) - ++low; - else if(mid > c) - --high; + if(mid * mid == num) + return true; + else if(mid * mid < num) + low = mid+1; else + high = mid-1; + + } + return false; + } + + bool judgeSquareSum(int c) { + + for(ll i = 0; i * i <= c; ++i) + { + int a = i * i; + if(isPerfectSquare(c - a)) return true; } return false; + } }; \ No newline at end of file From accc5ad968303f3366108dc3bd6585180202ac28 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Jun 2024 21:10:51 +0530 Subject: [PATCH 1156/1399] Attach NOTES - LeetHub From 5840e88e8605f5e7d800a0a46e18b4db01831c7d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 17 Jun 2024 21:10:54 +0530 Subject: [PATCH 1157/1399] Time: 0 ms (100.00%), Space: 7 MB (61.35%) - LeetHub --- .../0633-sum-of-square-numbers.cpp | 42 ++++++------------- 1 file changed, 12 insertions(+), 30 deletions(-) diff --git a/0633-sum-of-square-numbers/0633-sum-of-square-numbers.cpp b/0633-sum-of-square-numbers/0633-sum-of-square-numbers.cpp index bd9769d0..185bb33f 100644 --- a/0633-sum-of-square-numbers/0633-sum-of-square-numbers.cpp +++ b/0633-sum-of-square-numbers/0633-sum-of-square-numbers.cpp @@ -1,40 +1,22 @@ -using ll = long long; - class Solution { public: - - bool isPerfectSquare(int num) - { - if(num == 0 or num == 1) - return true; - - ll low = 0, high = num; - - while(low <= high) - { - ll mid = (low + high) / 2; - - if(mid * mid == num) - return true; - else if(mid * mid < num) - low = mid+1; - else - high = mid-1; - - } - return false; - } - bool judgeSquareSum(int c) { - for(ll i = 0; i * i <= c; ++i) + for(int i = 2; i * i <= c; ++i) { - int a = i * i; - if(isPerfectSquare(c - a)) - return true; + int cnt = 0; + + while(c % i == 0) + { + ++cnt; + c /= i; + } + + if(i % 4 == 3 and cnt % 2 != 0) + return false; } - return false; + return c%4 != 3; } }; \ No newline at end of file From 088cd4b6ac8d341df5bfd7c7b199129a1271fcf8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 18 Jun 2024 20:09:12 +0530 Subject: [PATCH 1158/1399] Create README - LeetHub --- 3187-peaks-in-array/README.md | 66 +++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 3187-peaks-in-array/README.md diff --git a/3187-peaks-in-array/README.md b/3187-peaks-in-array/README.md new file mode 100644 index 00000000..822bb1d9 --- /dev/null +++ b/3187-peaks-in-array/README.md @@ -0,0 +1,66 @@ +

3187. Peaks in Array

Hard


A peak in an array arr is an element that is greater than its previous and next element in arr.

+ +

You are given an integer array nums and a 2D integer array queries.

+ +

You have to process queries of two types:

+ +
    +
  • queries[i] = [1, li, ri], determine the count of peak elements in the subarray nums[li..ri].
  • +
  • queries[i] = [2, indexi, vali], change nums[indexi] to vali.
  • +
+ +

Return an array answer containing the results of the queries of the first type in order.

+ +

Notes:

+ +
    +
  • The first and the last element of an array or a subarray cannot be a peak.
  • +
+ +

 

+

Example 1:

+ +
+

Input: nums = [3,1,4,2,5], queries = [[2,3,4],[1,0,4]]

+ +

Output: [0]

+ +

Explanation:

+ +

First query: We change nums[3] to 4 and nums becomes [3,1,4,4,5].

+ +

Second query: The number of peaks in the [3,1,4,4,5] is 0.

+
+ +

Example 2:

+ +
+

Input: nums = [4,1,4,2,1,5], queries = [[2,2,4],[1,0,2],[1,0,4]]

+ +

Output: [0,1]

+ +

Explanation:

+ +

First query: nums[2] should become 4, but it is already set to 4.

+ +

Second query: The number of peaks in the [4,1,4] is 0.

+ +

Third query: The second 4 is a peak in the [4,1,4,2,1].

+
+ +

 

+

Constraints:

+ +
    +
  • 3 <= nums.length <= 105
  • +
  • 1 <= nums[i] <= 105
  • +
  • 1 <= queries.length <= 105
  • +
  • queries[i][0] == 1 or queries[i][0] == 2
  • +
  • For all i that: +
      +
    • queries[i][0] == 1: 0 <= queries[i][1] <= queries[i][2] <= nums.length - 1
    • +
    • queries[i][0] == 2: 0 <= queries[i][1] <= nums.length - 1, 1 <= queries[i][2] <= 105
    • +
    +
  • +
+
\ No newline at end of file From 0da40dfc8c4e3060710290dc73cb8d09864c434c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 18 Jun 2024 20:09:15 +0530 Subject: [PATCH 1159/1399] Time: 656 ms (27.72%), Space: 250.2 MB (35.06%) - LeetHub --- 3187-peaks-in-array/3187-peaks-in-array.cpp | 133 ++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 3187-peaks-in-array/3187-peaks-in-array.cpp diff --git a/3187-peaks-in-array/3187-peaks-in-array.cpp b/3187-peaks-in-array/3187-peaks-in-array.cpp new file mode 100644 index 00000000..f3beefc1 --- /dev/null +++ b/3187-peaks-in-array/3187-peaks-in-array.cpp @@ -0,0 +1,133 @@ +template +class SEG +{ +public: + vector tree, lazy, arr; + T N; + SEG() {} + SEG(vector v) + { + N = v.size(); + arr.resize(N); + for (int i = 0; i < N; ++i) + arr[i] = v[i]; + tree.resize(4 * N + 5); + lazy.resize(4 * N + 5, 0); + + buildTree(0, 0, N - 1); + } + + bool isPeak(int idx) + { + if(idx <= 0 or idx >= N-1) + return false; + + return (arr[idx] > arr[idx-1] and arr[idx] > arr[idx+1]); + } + + void buildTree(int idx, int low, int high) + { + if (low == high) + { + tree[idx] = (isPeak(low) ? 1 : 0); + return; + } + + int mid = (low + high) >> 1; + buildTree(2 * idx + 1, low, mid); + buildTree(2 * idx + 2, mid + 1, high); + + tree[idx] = tree[2 * idx + 1] + tree[2 * idx + 2]; + + return; + } + + + int queryTree(int idx, int low, int high, int l, int r) + { + // no overlap + // l r low high or low high l r + if (r < low or l > high) + return 0; + + // complete overlap + // [l low high r] + if (low >= l and high <= r) + return tree[idx]; + + // partial overlap + int mid = (low + high) / 2; + int left = queryTree(2 * idx + 1, low, mid, l, r); + int right = queryTree(2 * idx + 2, mid + 1, high, l, r); + + return left + right; + } + +public: + void updateTree(int idx, int low, int high, int ind, int val) + { + if (low == high) + { + tree[idx] = (isPeak(low) ? 1 : 0); + return; + } + int mid = (low + high) >> 1; + if (ind <= mid) + updateTree(2 * idx + 1, low, mid, ind, val); + else + updateTree(2 * idx + 2, mid + 1, high, ind, val); + + tree[idx] = tree[2 * idx + 1] + tree[2 * idx + 2]; + } + + int query(int l, int r) + { + return queryTree(0, 0, N - 1, l, r); + } + + void update(int ind, int val) + { + updateTree(0, 0, N - 1, ind, val); + } + + void build() + { + buildTree(0, 0, N - 1); + } +}; + + +class Solution { +public: + vector countOfPeaks(vector& nums, vector>& queries) { + + vector ans; + + int n = nums.size(); + + SEG seg(nums); + + for(auto& q : queries) + { + int type = q[0]; + int x = q[1]; + int y = q[2]; + + if(type == 1) + { + ans.push_back(seg.query(x+1, y-1)); + } + else + { + seg.arr[x] = y; + seg.update(x, y); + if(x - 1 > 0) + seg.update(x-1, nums[x-1]); + if(x + 1 < n-1) + seg.update(x+1, nums[x+1]); + } + } + + return ans; + } +}; \ No newline at end of file From b60bdcb385afc53e6eba03e0422db8b13b22f4a5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 19 Jun 2024 08:38:31 +0530 Subject: [PATCH 1160/1399] Attach NOTES - LeetHub From b6918299d146980441b3de7e1c124ca356b4faa6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 19 Jun 2024 08:38:34 +0530 Subject: [PATCH 1161/1399] Time: 143 ms (5.04%), Space: 68.9 MB (65.37%) - LeetHub --- ...imum-number-of-days-to-make-m-bouquets.cpp | 54 ++++++++----------- 1 file changed, 23 insertions(+), 31 deletions(-) diff --git a/1482-minimum-number-of-days-to-make-m-bouquets/1482-minimum-number-of-days-to-make-m-bouquets.cpp b/1482-minimum-number-of-days-to-make-m-bouquets/1482-minimum-number-of-days-to-make-m-bouquets.cpp index b9454582..df9f3f33 100644 --- a/1482-minimum-number-of-days-to-make-m-bouquets/1482-minimum-number-of-days-to-make-m-bouquets.cpp +++ b/1482-minimum-number-of-days-to-make-m-bouquets/1482-minimum-number-of-days-to-make-m-bouquets.cpp @@ -1,53 +1,45 @@ +using ll = long long; + class Solution { public: - int minDays(vector& bloomDay, int m, int k) { + + int minimumNumberOfDays(int m, int k, int mid, vector& bloomDay) + { + ll bouquet = 0; + ll currSum = 0; - int n = bloomDay.size(); + for(auto& day : bloomDay) + { + currSum = (day <= mid ? currSum+1 : 0); + bouquet += (currSum / k); + currSum %= k; + } - if((long long)k * m > n) - return -1; + return bouquet >= m; + } + + int minDays(vector& bloomDay, int m, int k) { int low = *min_element(bloomDay.begin(), bloomDay.end()); int high = *max_element(bloomDay.begin(), bloomDay.end()); - auto canBloom = [&](int mid) - { - int bloom = 0; - int cnt = 0; - for(int i = 0; i < n; ++i) - { - if(bloomDay[i] <= mid) - { - ++cnt; - } - else - { - bloom += (cnt / k); - cnt = 0; - } - } - - bloom += (cnt / k); - - return bloom; - }; - - int ans = -1; + int ans = INT_MAX; while(low <= high) { - int mid = low + (high - low) / 2; + int mid = (low + high) / 2; - if(canBloom(mid) >= m) + if(minimumNumberOfDays(m, k, mid, bloomDay)) { ans = mid; high = mid-1; } else + { low = mid+1; + } } - - return ans; + return (ans == INT_MAX ? -1 : ans); } }; \ No newline at end of file From e87e9534765c0e4843f8947d2efae6ff2386dd03 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 20 Jun 2024 09:16:32 +0530 Subject: [PATCH 1162/1399] Create README - LeetHub --- .../README.md | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 1552-magnetic-force-between-two-balls/README.md diff --git a/1552-magnetic-force-between-two-balls/README.md b/1552-magnetic-force-between-two-balls/README.md new file mode 100644 index 00000000..eee52ed4 --- /dev/null +++ b/1552-magnetic-force-between-two-balls/README.md @@ -0,0 +1,32 @@ +

1552. Magnetic Force Between Two Balls

Medium


In the universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.

+ +

Rick stated that magnetic force between two different balls at positions x and y is |x - y|.

+ +

Given the integer array position and the integer m. Return the required force.

+ +

 

+

Example 1:

+ +
Input: position = [1,2,3,4,7], m = 3
+Output: 3
+Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
+
+ +

Example 2:

+ +
Input: position = [5,4,3,2,1,1000000000], m = 2
+Output: 999999999
+Explanation: We can use baskets 1 and 1000000000.
+
+ +

 

+

Constraints:

+ +
    +
  • n == position.length
  • +
  • 2 <= n <= 105
  • +
  • 1 <= position[i] <= 109
  • +
  • All integers in position are distinct.
  • +
  • 2 <= m <= position.length
  • +
+
\ No newline at end of file From 72994a5f6c6cc88f406928b2f3200499f4a08001 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 20 Jun 2024 09:16:32 +0530 Subject: [PATCH 1163/1399] Attach NOTES - LeetHub --- 1552-magnetic-force-between-two-balls/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1552-magnetic-force-between-two-balls/NOTES.md diff --git a/1552-magnetic-force-between-two-balls/NOTES.md b/1552-magnetic-force-between-two-balls/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1552-magnetic-force-between-two-balls/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 87ccf7aa3ee9277894630ccfaf4d9216f4991501 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 20 Jun 2024 09:16:35 +0530 Subject: [PATCH 1164/1399] Time: 126 ms (57.07%), Space: 61.5 MB (35.79%) - LeetHub --- .../1552-magnetic-force-between-two-balls.cpp | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 1552-magnetic-force-between-two-balls/1552-magnetic-force-between-two-balls.cpp diff --git a/1552-magnetic-force-between-two-balls/1552-magnetic-force-between-two-balls.cpp b/1552-magnetic-force-between-two-balls/1552-magnetic-force-between-two-balls.cpp new file mode 100644 index 00000000..613e1044 --- /dev/null +++ b/1552-magnetic-force-between-two-balls/1552-magnetic-force-between-two-balls.cpp @@ -0,0 +1,45 @@ +class Solution { +public: + + int isPossible(int mid, int m, vector& position) + { + int prev = 0, n = position.size(); + int balls = 1 ; + + for(int i = 1; i < n; ++i) + { + if(position[i] - position[prev] >= mid) + { + ++balls; + prev = i; + } + } + + return balls >= m; + } + + int maxDistance(vector& position, int m) { + + int n = position.size(); + int low = 0, high = *max_element(position.begin(), position.end()); + int ans = 0; + + sort(position.begin(), position.end()); + + while(low <= high) + { + int mid = (low + high) / 2; + + if(isPossible(mid, m, position)) + { + ans = mid; + low = mid+1; + } + else + high = mid-1; + } + + return ans; + + } +}; \ No newline at end of file From f6c07e5402572a9b0ed5333427a46be6036ea88a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 20 Jun 2024 09:17:07 +0530 Subject: [PATCH 1165/1399] Attach NOTES - LeetHub From 607444c2aae1f1df4654abc849e905e8d61d7e9e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 20 Jun 2024 09:17:10 +0530 Subject: [PATCH 1166/1399] Time: 126 ms (57.07%), Space: 61.5 MB (35.79%) - LeetHub From a70c63f96999890f36a94834aeba3b97121187ed Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 20 Jun 2024 09:17:49 +0530 Subject: [PATCH 1167/1399] Attach NOTES - LeetHub From 6c9b5c2328c01aa75c7ac60e27f73865c4058aee Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 20 Jun 2024 09:17:52 +0530 Subject: [PATCH 1168/1399] Time: 131 ms (36.05%), Space: 61.3 MB (73.97%) - LeetHub --- .../1552-magnetic-force-between-two-balls.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/1552-magnetic-force-between-two-balls/1552-magnetic-force-between-two-balls.cpp b/1552-magnetic-force-between-two-balls/1552-magnetic-force-between-two-balls.cpp index 613e1044..b510ea76 100644 --- a/1552-magnetic-force-between-two-balls/1552-magnetic-force-between-two-balls.cpp +++ b/1552-magnetic-force-between-two-balls/1552-magnetic-force-between-two-balls.cpp @@ -21,11 +21,12 @@ class Solution { int maxDistance(vector& position, int m) { int n = position.size(); - int low = 0, high = *max_element(position.begin(), position.end()); - int ans = 0; sort(position.begin(), position.end()); + int low = 1, high = position.back() - position[0]; + int ans = 0; + while(low <= high) { int mid = (low + high) / 2; From 057a55263527f324db31b72d6b2c9e73dd3b72b3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 21 Jun 2024 20:13:10 +0530 Subject: [PATCH 1169/1399] Create README - LeetHub --- 1052-grumpy-bookstore-owner/README.md | 35 +++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 1052-grumpy-bookstore-owner/README.md diff --git a/1052-grumpy-bookstore-owner/README.md b/1052-grumpy-bookstore-owner/README.md new file mode 100644 index 00000000..dce57e16 --- /dev/null +++ b/1052-grumpy-bookstore-owner/README.md @@ -0,0 +1,35 @@ +

1052. Grumpy Bookstore Owner

Medium


There is a bookstore owner that has a store open for n minutes. Every minute, some number of customers enter the store. You are given an integer array customers of length n where customers[i] is the number of the customer that enters the store at the start of the ith minute and all those customers leave after the end of that minute.

+ +

On some minutes, the bookstore owner is grumpy. You are given a binary array grumpy where grumpy[i] is 1 if the bookstore owner is grumpy during the ith minute, and is 0 otherwise.

+ +

When the bookstore owner is grumpy, the customers of that minute are not satisfied, otherwise, they are satisfied.

+ +

The bookstore owner knows a secret technique to keep themselves not grumpy for minutes consecutive minutes, but can only use it once.

+ +

Return the maximum number of customers that can be satisfied throughout the day.

+ +

 

+

Example 1:

+ +
Input: customers = [1,0,1,2,1,1,7,5], grumpy = [0,1,0,1,0,1,0,1], minutes = 3
+Output: 16
+Explanation: The bookstore owner keeps themselves not grumpy for the last 3 minutes. 
+The maximum number of customers that can be satisfied = 1 + 1 + 1 + 1 + 7 + 5 = 16.
+
+ +

Example 2:

+ +
Input: customers = [1], grumpy = [0], minutes = 1
+Output: 1
+
+ +

 

+

Constraints:

+ +
    +
  • n == customers.length == grumpy.length
  • +
  • 1 <= minutes <= n <= 2 * 104
  • +
  • 0 <= customers[i] <= 1000
  • +
  • grumpy[i] is either 0 or 1.
  • +
+
\ No newline at end of file From 00d2a85140b527bb8c50083654ab00e66b40fcb7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 21 Jun 2024 20:13:13 +0530 Subject: [PATCH 1170/1399] Time: 43 ms (12.05%), Space: 45.8 MB (5.22%) - LeetHub --- .../1052-grumpy-bookstore-owner.cpp | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 1052-grumpy-bookstore-owner/1052-grumpy-bookstore-owner.cpp diff --git a/1052-grumpy-bookstore-owner/1052-grumpy-bookstore-owner.cpp b/1052-grumpy-bookstore-owner/1052-grumpy-bookstore-owner.cpp new file mode 100644 index 00000000..d4f877be --- /dev/null +++ b/1052-grumpy-bookstore-owner/1052-grumpy-bookstore-owner.cpp @@ -0,0 +1,44 @@ +class Solution { +public: + + int helper(int idx, bool flag, int n, int minutes, vector& grumpy, vector& customers, vector& pref, vector>& dp) + { + if(idx >= n) + return 0; + + if(dp[idx][flag] != -1) + return dp[idx][flag]; + + int ans = (grumpy[idx] == 0 ? customers[idx] : 0) + helper(idx+1, flag, n, minutes, grumpy, customers, pref, dp); + + int secretMove = 0; + + if(!flag) + { + secretMove = (idx + minutes - 1 < n ? (pref[idx+minutes-1] - (idx-1 >= 0 ? pref[idx-1] : 0)) : pref[n-1] - (idx-1 >= 0 ? pref[idx-1] : 0)) + helper(idx + minutes, 1, n, minutes, grumpy, customers, pref, dp); + + ans = max(ans, secretMove); + } + + return dp[idx][flag] = ans; + } + + int maxSatisfied(vector& customers, vector& grumpy, int minutes) { + + int n = customers.size(); + + vector> dp(n, vector(2, -1)); + + vector pref(n, 0); + + pref[0] = customers[0]; + + for(int i = 1; i < n; ++i) + { + pref[i] = pref[i-1] + customers[i]; + } + + return helper(0, 0, n, minutes, grumpy, customers, pref, dp); + + } +}; \ No newline at end of file From 2758ea968772ce22a87a5db0dbe6c14018e01ac6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 21 Jun 2024 20:14:12 +0530 Subject: [PATCH 1171/1399] Attach NOTES - LeetHub --- 1052-grumpy-bookstore-owner/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1052-grumpy-bookstore-owner/NOTES.md diff --git a/1052-grumpy-bookstore-owner/NOTES.md b/1052-grumpy-bookstore-owner/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1052-grumpy-bookstore-owner/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 2f06f70e63c883e316e3c791e24e2a93cb55c73e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 21 Jun 2024 20:14:29 +0530 Subject: [PATCH 1172/1399] Attach NOTES - LeetHub From 4535991b49a31713a44197d690218a0d5f848150 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 21 Jun 2024 20:14:32 +0530 Subject: [PATCH 1173/1399] Time: 43 ms (12.05%), Space: 45.8 MB (5.22%) - LeetHub From b308d869d8bfbc2e2ea2e9cda936c22ff13fed12 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 22 Jun 2024 07:36:19 +0530 Subject: [PATCH 1174/1399] Attach NOTES - LeetHub --- 1248-count-number-of-nice-subarrays/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1248-count-number-of-nice-subarrays/NOTES.md diff --git a/1248-count-number-of-nice-subarrays/NOTES.md b/1248-count-number-of-nice-subarrays/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1248-count-number-of-nice-subarrays/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 441e1fe2fb96c3397671cef8945b748f246d2c97 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 22 Jun 2024 07:36:22 +0530 Subject: [PATCH 1175/1399] Time: 129 ms (20.37%), Space: 89.9 MB (18.43%) - LeetHub --- .../1248-count-number-of-nice-subarrays.cpp | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 1248-count-number-of-nice-subarrays/1248-count-number-of-nice-subarrays.cpp diff --git a/1248-count-number-of-nice-subarrays/1248-count-number-of-nice-subarrays.cpp b/1248-count-number-of-nice-subarrays/1248-count-number-of-nice-subarrays.cpp new file mode 100644 index 00000000..aac4bacf --- /dev/null +++ b/1248-count-number-of-nice-subarrays/1248-count-number-of-nice-subarrays.cpp @@ -0,0 +1,30 @@ +class Solution { +public: + int numberOfSubarrays(vector& nums, int k) { + + int n = nums.size(); + + int i = 0, j = 0, ans = 0; + + list indexes; + + while(j < n) + { + if(nums[j] & 1) + indexes.push_back(j); + + if(indexes.size() > k) + { + i = indexes.front() + 1; + indexes.pop_front(); + } + + if(indexes.size() == k) ans += (indexes.front() - i + 1); + + ++j; + } + + return ans; + + } +}; \ No newline at end of file From d1db13a1d278251feb1a987b3e30de15b41ca944 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 22 Jun 2024 07:38:52 +0530 Subject: [PATCH 1176/1399] Attach NOTES - LeetHub From 8e31c16b604992531f5f9729cc9d193dffe7a2ba Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 22 Jun 2024 07:38:55 +0530 Subject: [PATCH 1177/1399] Time: 129 ms (20.37%), Space: 89.9 MB (18.43%) - LeetHub From 9c529592097d4112153f1cbe7cdb8433ed198048 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 22 Jun 2024 07:40:06 +0530 Subject: [PATCH 1178/1399] Attach NOTES - LeetHub From fb3a1be1b0ee7ae6c14d3a747d6f820885ee3904 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 22 Jun 2024 07:41:37 +0530 Subject: [PATCH 1179/1399] Attach NOTES - LeetHub From 061afcdd109bd955b76918b98b09a8ca05e13306 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 22 Jun 2024 07:41:41 +0530 Subject: [PATCH 1180/1399] Time: 129 ms (20.37%), Space: 89.9 MB (18.43%) - LeetHub From 1109ab5c3d8212ad41d323279ad8371687ca5e4d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 22 Jun 2024 07:44:56 +0530 Subject: [PATCH 1181/1399] Attach NOTES - LeetHub From 587fea5cb197fe0f8bc3565944a892cb1894c179 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 22 Jun 2024 08:43:40 +0530 Subject: [PATCH 1182/1399] Attach NOTES - LeetHub --- 0189-rotate-array/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0189-rotate-array/NOTES.md diff --git a/0189-rotate-array/NOTES.md b/0189-rotate-array/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0189-rotate-array/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 7fb9c3d53a79f8a02fbd06f9d524c1cefa7b2611 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 22 Jun 2024 08:43:44 +0530 Subject: [PATCH 1183/1399] Time: 20 ms (61.86%), Space: 27.3 MB (55.61%) - LeetHub --- 0189-rotate-array/0189-rotate-array.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 0189-rotate-array/0189-rotate-array.cpp diff --git a/0189-rotate-array/0189-rotate-array.cpp b/0189-rotate-array/0189-rotate-array.cpp new file mode 100644 index 00000000..3795c8f9 --- /dev/null +++ b/0189-rotate-array/0189-rotate-array.cpp @@ -0,0 +1,16 @@ +class Solution { +public: + void rotate(vector& nums, int k) { + + int n = nums.size(); + + k %= n; + + reverse(nums.begin(), nums.begin() + (n - k)); + + reverse(nums.begin() + (n-k), nums.end()); + + reverse(nums.begin(), nums.end()); + + } +}; \ No newline at end of file From eec391a9773e80945baac144888c7ca8d93496c5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 23 Jun 2024 18:42:50 +0530 Subject: [PATCH 1184/1399] Create README - LeetHub --- .../README.md | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 1438-longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit/README.md diff --git a/1438-longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit/README.md b/1438-longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit/README.md new file mode 100644 index 00000000..8081e912 --- /dev/null +++ b/1438-longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit/README.md @@ -0,0 +1,43 @@ +

1438. Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit

Medium


Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.

+ +

 

+

Example 1:

+ +
Input: nums = [8,2,4,7], limit = 4
+Output: 2 
+Explanation: All subarrays are: 
+[8] with maximum absolute diff |8-8| = 0 <= 4.
+[8,2] with maximum absolute diff |8-2| = 6 > 4. 
+[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
+[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
+[2] with maximum absolute diff |2-2| = 0 <= 4.
+[2,4] with maximum absolute diff |2-4| = 2 <= 4.
+[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
+[4] with maximum absolute diff |4-4| = 0 <= 4.
+[4,7] with maximum absolute diff |4-7| = 3 <= 4.
+[7] with maximum absolute diff |7-7| = 0 <= 4. 
+Therefore, the size of the longest subarray is 2.
+
+ +

Example 2:

+ +
Input: nums = [10,1,2,4,7,2], limit = 5
+Output: 4 
+Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
+
+ +

Example 3:

+ +
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
+Output: 3
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 105
  • +
  • 1 <= nums[i] <= 109
  • +
  • 0 <= limit <= 109
  • +
+
\ No newline at end of file From 1535f314bf399fd052e12cc7d3ccbd52a2734e2c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 23 Jun 2024 18:42:51 +0530 Subject: [PATCH 1185/1399] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1438-longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit/NOTES.md diff --git a/1438-longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit/NOTES.md b/1438-longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1438-longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 4029b300b11649dec4c2ccdffb3a72dd167eb5e2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 23 Jun 2024 18:42:54 +0530 Subject: [PATCH 1186/1399] Time: 151 ms (16.71%), Space: 97.9 MB (5.23%) - LeetHub --- ...olute-diff-less-than-or-equal-to-limit.cpp | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 1438-longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit/1438-longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit.cpp diff --git a/1438-longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit/1438-longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit.cpp b/1438-longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit/1438-longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit.cpp new file mode 100644 index 00000000..e129d313 --- /dev/null +++ b/1438-longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit/1438-longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit.cpp @@ -0,0 +1,37 @@ +class Solution { +public: + int longestSubarray(vector& nums, int limit) { + + list maxi, mini; + + int i = 0, j = 0, n = nums.size(); + + int ans = 0; + + while(j < n) + { + while(!maxi.empty() and nums[maxi.back()] < nums[j]) + maxi.pop_back(); + + while(!mini.empty() and nums[mini.back()] > nums[j]) + mini.pop_back(); + + maxi.push_back(j); + mini.push_back(j); + + while(!maxi.empty() and !mini.empty() and nums[maxi.front()] - nums[mini.front()] > limit) + { + if(maxi.front() == i) maxi.pop_front(); + if(mini.front() == i) mini.pop_front(); + ++i; + } + + ans = max(ans, j - i + 1); + + ++j; + } + + return ans; + + } +}; \ No newline at end of file From 8f231f74ed4e920796dcde9087250fea3fd3a1c7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 24 Jun 2024 22:53:52 +0530 Subject: [PATCH 1187/1399] Create README - LeetHub --- .../README.md | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 0995-minimum-number-of-k-consecutive-bit-flips/README.md diff --git a/0995-minimum-number-of-k-consecutive-bit-flips/README.md b/0995-minimum-number-of-k-consecutive-bit-flips/README.md new file mode 100644 index 00000000..4a559ed8 --- /dev/null +++ b/0995-minimum-number-of-k-consecutive-bit-flips/README.md @@ -0,0 +1,41 @@ +

995. Minimum Number of K Consecutive Bit Flips

Hard


You are given a binary array nums and an integer k.

+ +

A k-bit flip is choosing a subarray of length k from nums and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0.

+ +

Return the minimum number of k-bit flips required so that there is no 0 in the array. If it is not possible, return -1.

+ +

A subarray is a contiguous part of an array.

+ +

 

+

Example 1:

+ +
Input: nums = [0,1,0], k = 1
+Output: 2
+Explanation: Flip nums[0], then flip nums[2].
+
+ +

Example 2:

+ +
Input: nums = [1,1,0], k = 2
+Output: -1
+Explanation: No matter how we flip subarrays of size 2, we cannot make the array become [1,1,1].
+
+ +

Example 3:

+ +
Input: nums = [0,0,0,1,0,1,1,0], k = 3
+Output: 3
+Explanation: 
+Flip nums[0],nums[1],nums[2]: nums becomes [1,1,1,1,0,1,1,0]
+Flip nums[4],nums[5],nums[6]: nums becomes [1,1,1,1,1,0,0,0]
+Flip nums[5],nums[6],nums[7]: nums becomes [1,1,1,1,1,1,1,1]
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 105
  • +
  • 1 <= k <= nums.length
  • +
+
\ No newline at end of file From fcaf5ae9a0747802f8003ffea72849d976d9203d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 24 Jun 2024 22:53:54 +0530 Subject: [PATCH 1188/1399] Time: 89 ms (63.93%), Space: 117.9 MB (5.12%) - LeetHub --- ...imum-number-of-k-consecutive-bit-flips.cpp | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 0995-minimum-number-of-k-consecutive-bit-flips/0995-minimum-number-of-k-consecutive-bit-flips.cpp diff --git a/0995-minimum-number-of-k-consecutive-bit-flips/0995-minimum-number-of-k-consecutive-bit-flips.cpp b/0995-minimum-number-of-k-consecutive-bit-flips/0995-minimum-number-of-k-consecutive-bit-flips.cpp new file mode 100644 index 00000000..fbafd718 --- /dev/null +++ b/0995-minimum-number-of-k-consecutive-bit-flips/0995-minimum-number-of-k-consecutive-bit-flips.cpp @@ -0,0 +1,47 @@ +class Solution { +public: + int minKBitFlips(vector& nums, int k) { + + int n = nums.size(), ans = 0; + + vector> pref(n, {0, 0}); + + bool flag = true; + + for(int i = 0; i <= n-k; ++i) + { + pref[i].second = (i-1 >= 0 ? pref[i-1].second : 0); + + if(i - k >= 0) + { + if(pref[i-k].first) + --pref[i].second; + } + + int moves = pref[i].second; + + int op = (nums[i] % 2 == 0 ? (moves % 2 == 0 ? 1 : 0) : (moves % 2 == 0 ? 0 : 1)); + + if(op){ + pref[i].first = 1; + ++ans; + } + + pref[i].second += op; + } + + for(int i = n-k+1; i < n; ++i) + { + int moves = (i-1 >= 0 ? pref[i-1].second : 0); + + if(i - k >= 0 and pref[i-k].first) + --moves; + + flag &= (nums[i] & 1 and moves % 2 == 0 or nums[i] % 2 == 0 and moves & 1); + + pref[i].second = moves; + } + + return (flag ? ans : -1); + } +}; \ No newline at end of file From 911e27a81a4a4a50173cb13722d854f5ed042741 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 24 Jun 2024 22:59:45 +0530 Subject: [PATCH 1189/1399] Attach NOTES - LeetHub --- 0995-minimum-number-of-k-consecutive-bit-flips/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0995-minimum-number-of-k-consecutive-bit-flips/NOTES.md diff --git a/0995-minimum-number-of-k-consecutive-bit-flips/NOTES.md b/0995-minimum-number-of-k-consecutive-bit-flips/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0995-minimum-number-of-k-consecutive-bit-flips/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 89acf3e5c07692b5163a15cece45137e8aac7f76 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 24 Jun 2024 22:59:48 +0530 Subject: [PATCH 1190/1399] Time: 89 ms (63.93%), Space: 117.9 MB (5.12%) - LeetHub From ad4e63160596a040c5eb9b59fd99fb19e6db94aa Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 24 Jun 2024 23:06:12 +0530 Subject: [PATCH 1191/1399] Attach NOTES - LeetHub From eb92bf7e684065f3606f3aafc478295281b5bd18 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 24 Jun 2024 23:06:14 +0530 Subject: [PATCH 1192/1399] Time: 104 ms (18.85%), Space: 117.8 MB (5.12%) - LeetHub --- ...imum-number-of-k-consecutive-bit-flips.cpp | 25 +++++++------------ 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/0995-minimum-number-of-k-consecutive-bit-flips/0995-minimum-number-of-k-consecutive-bit-flips.cpp b/0995-minimum-number-of-k-consecutive-bit-flips/0995-minimum-number-of-k-consecutive-bit-flips.cpp index fbafd718..c2bc0ee2 100644 --- a/0995-minimum-number-of-k-consecutive-bit-flips/0995-minimum-number-of-k-consecutive-bit-flips.cpp +++ b/0995-minimum-number-of-k-consecutive-bit-flips/0995-minimum-number-of-k-consecutive-bit-flips.cpp @@ -8,7 +8,7 @@ class Solution { bool flag = true; - for(int i = 0; i <= n-k; ++i) + for(int i = 0; i < n; ++i) { pref[i].second = (i-1 >= 0 ? pref[i-1].second : 0); @@ -23,25 +23,18 @@ class Solution { int op = (nums[i] % 2 == 0 ? (moves % 2 == 0 ? 1 : 0) : (moves % 2 == 0 ? 0 : 1)); if(op){ - pref[i].first = 1; - ++ans; + if(i <= n-k) + { + pref[i].first = 1; + ++ans; + } + else + flag = false; } - + pref[i].second += op; } - for(int i = n-k+1; i < n; ++i) - { - int moves = (i-1 >= 0 ? pref[i-1].second : 0); - - if(i - k >= 0 and pref[i-k].first) - --moves; - - flag &= (nums[i] & 1 and moves % 2 == 0 or nums[i] % 2 == 0 and moves & 1); - - pref[i].second = moves; - } - return (flag ? ans : -1); } }; \ No newline at end of file From c73765792bc4d4a3652f948f2820d6e85759c66b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 25 Jun 2024 08:03:10 +0530 Subject: [PATCH 1193/1399] Attach NOTES - LeetHub From d775fd200a98184909a7b53b36fda1576bb3e233 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 25 Jun 2024 08:11:25 +0530 Subject: [PATCH 1194/1399] Create README - LeetHub --- 0084-largest-rectangle-in-histogram/README.md | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 0084-largest-rectangle-in-histogram/README.md diff --git a/0084-largest-rectangle-in-histogram/README.md b/0084-largest-rectangle-in-histogram/README.md new file mode 100644 index 00000000..8d5fb02f --- /dev/null +++ b/0084-largest-rectangle-in-histogram/README.md @@ -0,0 +1,25 @@ +

84. Largest Rectangle in Histogram

Hard


Given an array of integers heights representing the histogram's bar height where the width of each bar is 1, return the area of the largest rectangle in the histogram.

+ +

 

+

Example 1:

+ +
Input: heights = [2,1,5,6,2,3]
+Output: 10
+Explanation: The above is a histogram where width of each bar is 1.
+The largest rectangle is shown in the red area, which has an area = 10 units.
+
+ +

Example 2:

+ +
Input: heights = [2,4]
+Output: 4
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= heights.length <= 105
  • +
  • 0 <= heights[i] <= 104
  • +
+
\ No newline at end of file From 67697c7de799d9db8fca5e58cca0ecf5dbf24615 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 25 Jun 2024 08:28:16 +0530 Subject: [PATCH 1195/1399] Attach NOTES - LeetHub --- 0042-trapping-rain-water/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0042-trapping-rain-water/NOTES.md diff --git a/0042-trapping-rain-water/NOTES.md b/0042-trapping-rain-water/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0042-trapping-rain-water/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From e3dba23ffe17d91c90e53ea53c0b0df1b5b73edd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 25 Jun 2024 08:28:18 +0530 Subject: [PATCH 1196/1399] Time: 9 ms (68.10%), Space: 22.2 MB (95.57%) - LeetHub --- .../0042-trapping-rain-water.cpp | 29 +++++++++++-------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/0042-trapping-rain-water/0042-trapping-rain-water.cpp b/0042-trapping-rain-water/0042-trapping-rain-water.cpp index 2f7a6ad3..2731cd5d 100644 --- a/0042-trapping-rain-water/0042-trapping-rain-water.cpp +++ b/0042-trapping-rain-water/0042-trapping-rain-water.cpp @@ -2,22 +2,27 @@ class Solution { public: int trap(vector& height) { - int start = 0,end = height.size()-1; - int ans = 0; - int maxleft = height[start], maxright = height[end]; - while(start <= end) + int n = height.size(); + + int leftMax = 0, rightMax = 0; + + int ans = 0, i = 0, j = n-1; + + while(i < j) { - maxleft = max(maxleft,height[start]); - maxright = max(maxright,height[end]); + leftMax = max(leftMax, height[i]); + rightMax = max(rightMax, height[j]); - ans += min(maxleft,maxright) - min(height[start],height[end]); - if(height[start] < height[end]) - ++start; + if(height[i] <= height[j]) + { + ans += min(leftMax, rightMax) - height[i++]; + } else - --end; + { + ans += min(leftMax, rightMax) - height[j--]; + } } return ans; } -}; - +}; \ No newline at end of file From a215728d84850adc67142a21924805abb34a3b5f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 25 Jun 2024 08:40:46 +0530 Subject: [PATCH 1197/1399] Attach NOTES - LeetHub From 01771bf372ee885a4c39321005d750ff2633fa9a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 25 Jun 2024 08:40:49 +0530 Subject: [PATCH 1198/1399] Time: 0 ms (100.00%), Space: 23.2 MB (28.72%) - LeetHub --- .../0042-trapping-rain-water.cpp | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/0042-trapping-rain-water/0042-trapping-rain-water.cpp b/0042-trapping-rain-water/0042-trapping-rain-water.cpp index 2731cd5d..915373f6 100644 --- a/0042-trapping-rain-water/0042-trapping-rain-water.cpp +++ b/0042-trapping-rain-water/0042-trapping-rain-water.cpp @@ -4,23 +4,24 @@ class Solution { int n = height.size(); - int leftMax = 0, rightMax = 0; + vector leftMax(n, 0), rightMax(n, 0); - int ans = 0, i = 0, j = n-1; + int ans = 0; - while(i < j) + for(int i = 0; i < n; ++i) { - leftMax = max(leftMax, height[i]); - rightMax = max(rightMax, height[j]); - - if(height[i] <= height[j]) - { - ans += min(leftMax, rightMax) - height[i++]; - } - else - { - ans += min(leftMax, rightMax) - height[j--]; - } + leftMax[i] = (i == 0 ? height[i] : max(height[i], leftMax[i-1])); + } + + for(int i = n-1; i >= 0; --i) + { + rightMax[i] = (i == n-1 ? height[i] : max(height[i], rightMax[i+1])); + } + + for(int i = 0; i < n; ++i) + { + int canHold = min(leftMax[i], rightMax[i]); + ans += (canHold - height[i] > 0 ? canHold - height[i] : 0); } return ans; From 3090a09616b75d2e2adb88f2fc6879ab0748b2d9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 25 Jun 2024 08:42:28 +0530 Subject: [PATCH 1199/1399] Attach NOTES - LeetHub From 376fd26870a3437669d72a26ef1508ad10deff25 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 25 Jun 2024 08:42:31 +0530 Subject: [PATCH 1200/1399] Time: 7 ms (87.59%), Space: 22.1 MB (99.87%) - LeetHub --- .../0042-trapping-rain-water.cpp | 30 +++++++++---------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/0042-trapping-rain-water/0042-trapping-rain-water.cpp b/0042-trapping-rain-water/0042-trapping-rain-water.cpp index 915373f6..6ea2c60c 100644 --- a/0042-trapping-rain-water/0042-trapping-rain-water.cpp +++ b/0042-trapping-rain-water/0042-trapping-rain-water.cpp @@ -1,29 +1,27 @@ class Solution { public: int trap(vector& height) { - + int n = height.size(); - vector leftMax(n, 0), rightMax(n, 0); + int i = 0, j = n-1, ans = 0; - int ans = 0; + int leftMax = 0, rightMax = 0; - for(int i = 0; i < n; ++i) - { - leftMax[i] = (i == 0 ? height[i] : max(height[i], leftMax[i-1])); - } - - for(int i = n-1; i >= 0; --i) + while(i < j) { - rightMax[i] = (i == n-1 ? height[i] : max(height[i], rightMax[i+1])); - } - - for(int i = 0; i < n; ++i) - { - int canHold = min(leftMax[i], rightMax[i]); - ans += (canHold - height[i] > 0 ? canHold - height[i] : 0); + leftMax = max(leftMax, height[i]); + rightMax = max(rightMax, height[j]); + + ans += min(leftMax, rightMax) - min(height[i], height[j]); + + if(height[i] <= height[j]) + ++i; + else + --j; } return ans; + } }; \ No newline at end of file From c80fb75d14d9c532c485149bdde2153d020a03d0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 25 Jun 2024 12:41:45 +0530 Subject: [PATCH 1201/1399] Create README - LeetHub From 4b898a9c77f8f14031b794c20e500329d792f5fc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 25 Jun 2024 13:15:03 +0530 Subject: [PATCH 1202/1399] Create README - LeetHub --- .../README.md | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 1038-binary-search-tree-to-greater-sum-tree/README.md diff --git a/1038-binary-search-tree-to-greater-sum-tree/README.md b/1038-binary-search-tree-to-greater-sum-tree/README.md new file mode 100644 index 00000000..c503c670 --- /dev/null +++ b/1038-binary-search-tree-to-greater-sum-tree/README.md @@ -0,0 +1,35 @@ +

1038. Binary Search Tree to Greater Sum Tree

Medium


Given the root of a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus the sum of all keys greater than the original key in BST.

+ +

As a reminder, a binary search tree is a tree that satisfies these constraints:

+ +
    +
  • The left subtree of a node contains only nodes with keys less than the node's key.
  • +
  • The right subtree of a node contains only nodes with keys greater than the node's key.
  • +
  • Both the left and right subtrees must also be binary search trees.
  • +
+ +

 

+

Example 1:

+ +
Input: root = [4,1,6,0,2,5,7,null,null,null,3,null,null,null,8]
+Output: [30,36,21,36,35,26,15,null,null,null,33,null,null,null,8]
+
+ +

Example 2:

+ +
Input: root = [0,null,1]
+Output: [1,null,1]
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is in the range [1, 100].
  • +
  • 0 <= Node.val <= 100
  • +
  • All the values in the tree are unique.
  • +
+ +

 

+

Note: This question is the same as 538: https://leetcode.com/problems/convert-bst-to-greater-tree/

+
\ No newline at end of file From 50a0b2d2370a51ac5c4d81a3eefe43d0545fdb19 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 25 Jun 2024 13:15:06 +0530 Subject: [PATCH 1203/1399] Time: 2 ms (49.63%), Space: 10.1 MB (42.71%) - LeetHub --- ...binary-search-tree-to-greater-sum-tree.cpp | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 1038-binary-search-tree-to-greater-sum-tree/1038-binary-search-tree-to-greater-sum-tree.cpp diff --git a/1038-binary-search-tree-to-greater-sum-tree/1038-binary-search-tree-to-greater-sum-tree.cpp b/1038-binary-search-tree-to-greater-sum-tree/1038-binary-search-tree-to-greater-sum-tree.cpp new file mode 100644 index 00000000..fd6e8ffc --- /dev/null +++ b/1038-binary-search-tree-to-greater-sum-tree/1038-binary-search-tree-to-greater-sum-tree.cpp @@ -0,0 +1,38 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + + void helper(TreeNode* root, int& sum) + { + if(!root) + return; + + helper(root->right, sum); + + root->val += sum; + + sum = root->val; + + helper(root-> left, sum); + } + + TreeNode* bstToGst(TreeNode* root) { + + int sum = 0; + + helper(root, sum); + + return root; + + } +}; \ No newline at end of file From 15abb93c81e1f0e11645605a57613d7c257ac271 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 26 Jun 2024 22:34:49 +0530 Subject: [PATCH 1204/1399] Create README - LeetHub --- 1382-balance-a-binary-search-tree/README.md | 26 +++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 1382-balance-a-binary-search-tree/README.md diff --git a/1382-balance-a-binary-search-tree/README.md b/1382-balance-a-binary-search-tree/README.md new file mode 100644 index 00000000..b30659a0 --- /dev/null +++ b/1382-balance-a-binary-search-tree/README.md @@ -0,0 +1,26 @@ +

1382. Balance a Binary Search Tree

Medium


Given the root of a binary search tree, return a balanced binary search tree with the same node values. If there is more than one answer, return any of them.

+ +

A binary search tree is balanced if the depth of the two subtrees of every node never differs by more than 1.

+ +

 

+

Example 1:

+ +
Input: root = [1,null,2,null,3,null,4,null,null]
+Output: [2,1,3,null,null,null,4]
+Explanation: This is not the only correct answer, [3,1,4,null,2] is also correct.
+
+ +

Example 2:

+ +
Input: root = [2,1,3]
+Output: [2,1,3]
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is in the range [1, 104].
  • +
  • 1 <= Node.val <= 105
  • +
+
\ No newline at end of file From 47f249475057dfbb4df5b7bcb877205261bbb98d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 26 Jun 2024 22:34:54 +0530 Subject: [PATCH 1205/1399] Time: 85 ms (70.08%), Space: 62.6 MB (60.81%) - LeetHub --- .../1382-balance-a-binary-search-tree.cpp | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 1382-balance-a-binary-search-tree/1382-balance-a-binary-search-tree.cpp diff --git a/1382-balance-a-binary-search-tree/1382-balance-a-binary-search-tree.cpp b/1382-balance-a-binary-search-tree/1382-balance-a-binary-search-tree.cpp new file mode 100644 index 00000000..6ee5d2f7 --- /dev/null +++ b/1382-balance-a-binary-search-tree/1382-balance-a-binary-search-tree.cpp @@ -0,0 +1,51 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + + void inorder(TreeNode* root, vector& arr) + { + if(root) + { + inorder(root->left, arr); + arr.push_back(root->val); + inorder(root->right, arr); + } + } + + TreeNode* balance(vector& arr, int low, int high) + { + if(low > high) + return nullptr; + + int mid = (low + high) / 2; + + TreeNode* root = new TreeNode(arr[mid]); + + root->left = balance(arr, low, mid-1); + + root->right = balance(arr, mid+1, high); + + return root; + + } + + TreeNode* balanceBST(TreeNode* root) { + + vector arr; + + inorder(root, arr); + + return balance(arr, 0, arr.size()-1); + + } +}; \ No newline at end of file From e31651e1678ea653d727156bbc3723519191fcc7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 26 Jun 2024 22:36:18 +0530 Subject: [PATCH 1206/1399] Attach NOTES - LeetHub --- 1382-balance-a-binary-search-tree/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1382-balance-a-binary-search-tree/NOTES.md diff --git a/1382-balance-a-binary-search-tree/NOTES.md b/1382-balance-a-binary-search-tree/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1382-balance-a-binary-search-tree/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 5e5d1dfe74eb4f6d5494d8395f4dcf988d065466 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 26 Jun 2024 22:36:21 +0530 Subject: [PATCH 1207/1399] Time: 85 ms (70.08%), Space: 62.6 MB (60.81%) - LeetHub From 607dacd6be423f662e8c94c4b6dd39c78fadab98 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 26 Jun 2024 23:56:26 +0530 Subject: [PATCH 1208/1399] Create README - LeetHub --- 0295-find-median-from-data-stream/README.md | 50 +++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 0295-find-median-from-data-stream/README.md diff --git a/0295-find-median-from-data-stream/README.md b/0295-find-median-from-data-stream/README.md new file mode 100644 index 00000000..40d10740 --- /dev/null +++ b/0295-find-median-from-data-stream/README.md @@ -0,0 +1,50 @@ +

295. Find Median from Data Stream

Hard


The median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value, and the median is the mean of the two middle values.

+ +
    +
  • For example, for arr = [2,3,4], the median is 3.
  • +
  • For example, for arr = [2,3], the median is (2 + 3) / 2 = 2.5.
  • +
+ +

Implement the MedianFinder class:

+ +
    +
  • MedianFinder() initializes the MedianFinder object.
  • +
  • void addNum(int num) adds the integer num from the data stream to the data structure.
  • +
  • double findMedian() returns the median of all elements so far. Answers within 10-5 of the actual answer will be accepted.
  • +
+ +

 

+

Example 1:

+ +
Input
+["MedianFinder", "addNum", "addNum", "findMedian", "addNum", "findMedian"]
+[[], [1], [2], [], [3], []]
+Output
+[null, null, null, 1.5, null, 2.0]
+
+Explanation
+MedianFinder medianFinder = new MedianFinder();
+medianFinder.addNum(1);    // arr = [1]
+medianFinder.addNum(2);    // arr = [1, 2]
+medianFinder.findMedian(); // return 1.5 (i.e., (1 + 2) / 2)
+medianFinder.addNum(3);    // arr[1, 2, 3]
+medianFinder.findMedian(); // return 2.0
+
+ +

 

+

Constraints:

+ +
    +
  • -105 <= num <= 105
  • +
  • There will be at least one element in the data structure before calling findMedian.
  • +
  • At most 5 * 104 calls will be made to addNum and findMedian.
  • +
+ +

 

+

Follow up:

+ +
    +
  • If all integer numbers from the stream are in the range [0, 100], how would you optimize your solution?
  • +
  • If 99% of all integer numbers from the stream are in the range [0, 100], how would you optimize your solution?
  • +
+
\ No newline at end of file From ffff5b67e965b3d54eb58ad80aff9cb24e794a8f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 26 Jun 2024 23:56:30 +0530 Subject: [PATCH 1209/1399] Time: 279 ms (41.98%), Space: 121.9 MB (20.74%) - LeetHub --- .../0295-find-median-from-data-stream.cpp | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 0295-find-median-from-data-stream/0295-find-median-from-data-stream.cpp diff --git a/0295-find-median-from-data-stream/0295-find-median-from-data-stream.cpp b/0295-find-median-from-data-stream/0295-find-median-from-data-stream.cpp new file mode 100644 index 00000000..b21b83b7 --- /dev/null +++ b/0295-find-median-from-data-stream/0295-find-median-from-data-stream.cpp @@ -0,0 +1,52 @@ +class MedianFinder { +public: + + priority_queue maxHeap; + priority_queue, greater> minHeap; + + MedianFinder() { + + } + + void addNum(int num) { + if(maxHeap.empty() and minHeap.empty()) + maxHeap.push(num); + else if(num > maxHeap.top()) + minHeap.push(num); + else + maxHeap.push(num); + + int n = maxHeap.size(); + int m = minHeap.size(); + + if(abs(n-m) == 2) + { + if(n > m) + { + minHeap.push(maxHeap.top()); + maxHeap.pop(); + } + else + { + maxHeap.push(minHeap.top()); + minHeap.pop(); + } + } + } + + double findMedian() { + if(maxHeap.size() == minHeap.size()) + return (maxHeap.top() + minHeap.top()) / 2.0; + else if(maxHeap.size() > minHeap.size()) + return maxHeap.top(); + else + return minHeap.top(); + } +}; + +/** + * Your MedianFinder object will be instantiated and called as such: + * MedianFinder* obj = new MedianFinder(); + * obj->addNum(num); + * double param_2 = obj->findMedian(); + */ \ No newline at end of file From e4f7c62706791756f05974c92e291cebca89da6e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 27 Jun 2024 07:38:45 +0530 Subject: [PATCH 1210/1399] Attach NOTES - LeetHub From 8dd5b3c42e8afd4e047fd4a88380402a2b988e60 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 27 Jun 2024 07:48:49 +0530 Subject: [PATCH 1211/1399] Create README - LeetHub --- 1092-shortest-common-supersequence/README.md | 29 ++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 1092-shortest-common-supersequence/README.md diff --git a/1092-shortest-common-supersequence/README.md b/1092-shortest-common-supersequence/README.md new file mode 100644 index 00000000..f93f6296 --- /dev/null +++ b/1092-shortest-common-supersequence/README.md @@ -0,0 +1,29 @@ +

1092. Shortest Common Supersequence

Hard


Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences. If there are multiple valid strings, return any of them.

+ +

A string s is a subsequence of string t if deleting some number of characters from t (possibly 0) results in the string s.

+ +

 

+

Example 1:

+ +
Input: str1 = "abac", str2 = "cab"
+Output: "cabac"
+Explanation: 
+str1 = "abac" is a subsequence of "cabac" because we can delete the first "c".
+str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac".
+The answer provided is the shortest such string that satisfies these properties.
+
+ +

Example 2:

+ +
Input: str1 = "aaaaaaaa", str2 = "aaaaaaaa"
+Output: "aaaaaaaa"
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= str1.length, str2.length <= 1000
  • +
  • str1 and str2 consist of lowercase English letters.
  • +
+
\ No newline at end of file From 614d9c5f9befc8d1319b427623c96a33d324160c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 27 Jun 2024 07:48:49 +0530 Subject: [PATCH 1212/1399] Attach NOTES - LeetHub --- 1092-shortest-common-supersequence/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1092-shortest-common-supersequence/NOTES.md diff --git a/1092-shortest-common-supersequence/NOTES.md b/1092-shortest-common-supersequence/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1092-shortest-common-supersequence/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 0ade45a03be0cac30df78989944b4c8765e25df0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 27 Jun 2024 07:48:53 +0530 Subject: [PATCH 1213/1399] Time: 13 ms (95.11%), Space: 25.5 MB (83.03%) - LeetHub --- .../1092-shortest-common-supersequence.cpp | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 1092-shortest-common-supersequence/1092-shortest-common-supersequence.cpp diff --git a/1092-shortest-common-supersequence/1092-shortest-common-supersequence.cpp b/1092-shortest-common-supersequence/1092-shortest-common-supersequence.cpp new file mode 100644 index 00000000..47e9291d --- /dev/null +++ b/1092-shortest-common-supersequence/1092-shortest-common-supersequence.cpp @@ -0,0 +1,58 @@ +class Solution { +public: + string shortestCommonSupersequence(string str1, string str2) { + + int n = str1.size(), m = str2.size(); + + vector> dp(n+1, vector(m+1, 0)); + + for(int i = 1; i <= n; ++i) + { + for(int j = 1; j <= m; ++j) + { + if(str1[i-1] == str2[j-1]) + dp[i][j] = 1 + dp[i-1][j-1]; + else + dp[i][j] = max(dp[i-1][j], dp[i][j-1]); + } + } + + int i = n, j = m; + string ans; + + while(i > 0 and j > 0) + { + if(str1[i-1] == str2[j-1]) + { + ans += str1[i-1]; + --i, --j; + } + else if(dp[i-1][j] > dp[i][j-1]) + { + ans += str1[i-1]; + --i; + } + else + { + ans += str2[j-1]; + --j; + } + } + + while(i > 0) + { + ans += str1[i-1]; + --i; + } + + while(j > 0) + { + ans += str2[j-1]; + --j; + } + + reverse(ans.begin(), ans.end()); + + return ans; + } +}; \ No newline at end of file From 3217eacab1eb4e4085198a6f8ee8552618bc7958 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 27 Jun 2024 19:13:06 +0530 Subject: [PATCH 1214/1399] Create README - LeetHub --- 1791-find-center-of-star-graph/README.md | 30 ++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 1791-find-center-of-star-graph/README.md diff --git a/1791-find-center-of-star-graph/README.md b/1791-find-center-of-star-graph/README.md new file mode 100644 index 00000000..fc958c58 --- /dev/null +++ b/1791-find-center-of-star-graph/README.md @@ -0,0 +1,30 @@ +

1791. Find Center of Star Graph

Easy


There is an undirected star graph consisting of n nodes labeled from 1 to n. A star graph is a graph where there is one center node and exactly n - 1 edges that connect the center node with every other node.

+ +

You are given a 2D integer array edges where each edges[i] = [ui, vi] indicates that there is an edge between the nodes ui and vi. Return the center of the given star graph.

+ +

 

+

Example 1:

+ +
Input: edges = [[1,2],[2,3],[4,2]]
+Output: 2
+Explanation: As shown in the figure above, node 2 is connected to every other node, so 2 is the center.
+
+ +

Example 2:

+ +
Input: edges = [[1,2],[5,1],[1,3],[1,4]]
+Output: 1
+
+ +

 

+

Constraints:

+ +
    +
  • 3 <= n <= 105
  • +
  • edges.length == n - 1
  • +
  • edges[i].length == 2
  • +
  • 1 <= ui, vi <= n
  • +
  • ui != vi
  • +
  • The given edges represent a valid star graph.
  • +
+
\ No newline at end of file From ed88f06aa560f8379a6b511fde6d729a61e094af Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 27 Jun 2024 19:13:09 +0530 Subject: [PATCH 1215/1399] Time: 118 ms (95.71%), Space: 70.7 MB (92.91%) - LeetHub --- .../1791-find-center-of-star-graph.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 1791-find-center-of-star-graph/1791-find-center-of-star-graph.cpp diff --git a/1791-find-center-of-star-graph/1791-find-center-of-star-graph.cpp b/1791-find-center-of-star-graph/1791-find-center-of-star-graph.cpp new file mode 100644 index 00000000..fd3ab8d2 --- /dev/null +++ b/1791-find-center-of-star-graph/1791-find-center-of-star-graph.cpp @@ -0,0 +1,11 @@ +class Solution { +public: + int findCenter(vector>& edges) { + + if(edges[0][0]==edges[1][0] || edges[0][0]==edges[1][1]) + return edges[0][0]; + + else return edges[0][1]; + + } +}; \ No newline at end of file From 3972f5d4d014b8dca8f9d2f994c2feb519366268 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 28 Jun 2024 21:08:21 +0530 Subject: [PATCH 1216/1399] Create README - LeetHub --- .../README.md | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 2285-maximum-total-importance-of-roads/README.md diff --git a/2285-maximum-total-importance-of-roads/README.md b/2285-maximum-total-importance-of-roads/README.md new file mode 100644 index 00000000..c5b07133 --- /dev/null +++ b/2285-maximum-total-importance-of-roads/README.md @@ -0,0 +1,48 @@ +

2285. Maximum Total Importance of Roads

Medium


You are given an integer n denoting the number of cities in a country. The cities are numbered from 0 to n - 1.

+ +

You are also given a 2D integer array roads where roads[i] = [ai, bi] denotes that there exists a bidirectional road connecting cities ai and bi.

+ +

You need to assign each city with an integer value from 1 to n, where each value can only be used once. The importance of a road is then defined as the sum of the values of the two cities it connects.

+ +

Return the maximum total importance of all roads possible after assigning the values optimally.

+ +

 

+

Example 1:

+ +
Input: n = 5, roads = [[0,1],[1,2],[2,3],[0,2],[1,3],[2,4]]
+Output: 43
+Explanation: The figure above shows the country and the assigned values of [2,4,5,3,1].
+- The road (0,1) has an importance of 2 + 4 = 6.
+- The road (1,2) has an importance of 4 + 5 = 9.
+- The road (2,3) has an importance of 5 + 3 = 8.
+- The road (0,2) has an importance of 2 + 5 = 7.
+- The road (1,3) has an importance of 4 + 3 = 7.
+- The road (2,4) has an importance of 5 + 1 = 6.
+The total importance of all roads is 6 + 9 + 8 + 7 + 7 + 6 = 43.
+It can be shown that we cannot obtain a greater total importance than 43.
+
+ +

Example 2:

+ +
Input: n = 5, roads = [[0,3],[2,4],[1,3]]
+Output: 20
+Explanation: The figure above shows the country and the assigned values of [4,3,2,5,1].
+- The road (0,3) has an importance of 4 + 5 = 9.
+- The road (2,4) has an importance of 2 + 1 = 3.
+- The road (1,3) has an importance of 3 + 5 = 8.
+The total importance of all roads is 9 + 3 + 8 = 20.
+It can be shown that we cannot obtain a greater total importance than 20.
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= n <= 5 * 104
  • +
  • 1 <= roads.length <= 5 * 104
  • +
  • roads[i].length == 2
  • +
  • 0 <= ai, bi <= n - 1
  • +
  • ai != bi
  • +
  • There are no duplicate roads.
  • +
+
\ No newline at end of file From 77fba1ba083e55135f7fb3588cf3f7bc2278b9e8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 28 Jun 2024 21:08:25 +0530 Subject: [PATCH 1217/1399] Time: 531 ms (8.97%), Space: 176.7 MB (15.35%) - LeetHub --- ...2285-maximum-total-importance-of-roads.cpp | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 2285-maximum-total-importance-of-roads/2285-maximum-total-importance-of-roads.cpp diff --git a/2285-maximum-total-importance-of-roads/2285-maximum-total-importance-of-roads.cpp b/2285-maximum-total-importance-of-roads/2285-maximum-total-importance-of-roads.cpp new file mode 100644 index 00000000..b992faad --- /dev/null +++ b/2285-maximum-total-importance-of-roads/2285-maximum-total-importance-of-roads.cpp @@ -0,0 +1,56 @@ +class Solution { +public: + long long maximumImportance(int n, vector>& roads) { + + vector adj[n+1]; + vector indegree(n, 0); + map, int> have; + + for(auto& edge : roads) + { + ++indegree[edge[0]]; + ++indegree[edge[1]]; + adj[edge[0]].push_back(edge[1]); + adj[edge[1]].push_back(edge[0]); + } + + vector> vp; + + for(int i = 0; i < n; ++i) + { + vp.push_back({indegree[i], i}); + } + + sort(vp.begin(), vp.end()); + + vector vals(n, 0); + + for(int i = 0; i < n; ++i) + { + vals[vp[i].second] = i+1; + } + + vector visited(n, false); + + long long ans = 0; + + for(int i = 0; i < n; ++i) + { + for(auto& node : adj[i]) + { + int a = i, b = node; + if(a > b) swap(a, b); + + if(have.find({a, b}) == have.end()) + { + ans += vals[a]; + ans += vals[b]; + ++have[{a, b}]; + } + } + } + + return ans; + + } +}; \ No newline at end of file From 8ce859771c3699e5f8683f4311827affa5c907ac Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 29 Jun 2024 08:13:04 +0530 Subject: [PATCH 1218/1399] Create README - LeetHub --- .../README.md | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 2192-all-ancestors-of-a-node-in-a-directed-acyclic-graph/README.md diff --git a/2192-all-ancestors-of-a-node-in-a-directed-acyclic-graph/README.md b/2192-all-ancestors-of-a-node-in-a-directed-acyclic-graph/README.md new file mode 100644 index 00000000..8831f995 --- /dev/null +++ b/2192-all-ancestors-of-a-node-in-a-directed-acyclic-graph/README.md @@ -0,0 +1,49 @@ +

2192. All Ancestors of a Node in a Directed Acyclic Graph

Medium


You are given a positive integer n representing the number of nodes of a Directed Acyclic Graph (DAG). The nodes are numbered from 0 to n - 1 (inclusive).

+ +

You are also given a 2D integer array edges, where edges[i] = [fromi, toi] denotes that there is a unidirectional edge from fromi to toi in the graph.

+ +

Return a list answer, where answer[i] is the list of ancestors of the ith node, sorted in ascending order.

+ +

A node u is an ancestor of another node v if u can reach v via a set of edges.

+ +

 

+

Example 1:

+ +
Input: n = 8, edgeList = [[0,3],[0,4],[1,3],[2,4],[2,7],[3,5],[3,6],[3,7],[4,6]]
+Output: [[],[],[],[0,1],[0,2],[0,1,3],[0,1,2,3,4],[0,1,2,3]]
+Explanation:
+The above diagram represents the input graph.
+- Nodes 0, 1, and 2 do not have any ancestors.
+- Node 3 has two ancestors 0 and 1.
+- Node 4 has two ancestors 0 and 2.
+- Node 5 has three ancestors 0, 1, and 3.
+- Node 6 has five ancestors 0, 1, 2, 3, and 4.
+- Node 7 has four ancestors 0, 1, 2, and 3.
+
+ +

Example 2:

+ +
Input: n = 5, edgeList = [[0,1],[0,2],[0,3],[0,4],[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]]
+Output: [[],[0],[0,1],[0,1,2],[0,1,2,3]]
+Explanation:
+The above diagram represents the input graph.
+- Node 0 does not have any ancestor.
+- Node 1 has one ancestor 0.
+- Node 2 has two ancestors 0 and 1.
+- Node 3 has three ancestors 0, 1, and 2.
+- Node 4 has four ancestors 0, 1, 2, and 3.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= 1000
  • +
  • 0 <= edges.length <= min(2000, n * (n - 1) / 2)
  • +
  • edges[i].length == 2
  • +
  • 0 <= fromi, toi <= n - 1
  • +
  • fromi != toi
  • +
  • There are no duplicate edges.
  • +
  • The graph is directed and acyclic.
  • +
+
\ No newline at end of file From 01dc4296ac61c2106c09ea1c560df5546b276827 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 29 Jun 2024 08:13:08 +0530 Subject: [PATCH 1219/1399] Time: 128 ms (60.70%), Space: 62.5 MB (99.12%) - LeetHub --- ...-of-a-node-in-a-directed-acyclic-graph.cpp | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 2192-all-ancestors-of-a-node-in-a-directed-acyclic-graph/2192-all-ancestors-of-a-node-in-a-directed-acyclic-graph.cpp diff --git a/2192-all-ancestors-of-a-node-in-a-directed-acyclic-graph/2192-all-ancestors-of-a-node-in-a-directed-acyclic-graph.cpp b/2192-all-ancestors-of-a-node-in-a-directed-acyclic-graph/2192-all-ancestors-of-a-node-in-a-directed-acyclic-graph.cpp new file mode 100644 index 00000000..f2780c4f --- /dev/null +++ b/2192-all-ancestors-of-a-node-in-a-directed-acyclic-graph/2192-all-ancestors-of-a-node-in-a-directed-acyclic-graph.cpp @@ -0,0 +1,45 @@ +class Solution { +public: + vector> getAncestors(int n, vector>& edges) { + + + vector adj[n]; + + for(auto& edge : edges) + { + int u = edge[0]; + int v = edge[1]; + + adj[v].push_back(u); + } + + vector> ans(n); + + vector visited(n, false); + + function dfs = [&](int sv, int idx) -> void{ + + visited[sv] = true; + + for(auto& node : adj[sv]) + { + if(!visited[node]) + { + ans[idx].push_back(node); + dfs(node, idx); + } + } + }; + + for(int i = 0; i < n; ++i) + { + dfs(i, i); + + fill(visited.begin(), visited.end(), false); + + sort(ans[i].begin(), ans[i].end()); + } + + return ans; + } +}; \ No newline at end of file From 44df5e93950b097ef4b07b20592137c889820f55 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 29 Jun 2024 08:14:08 +0530 Subject: [PATCH 1220/1399] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2192-all-ancestors-of-a-node-in-a-directed-acyclic-graph/NOTES.md diff --git a/2192-all-ancestors-of-a-node-in-a-directed-acyclic-graph/NOTES.md b/2192-all-ancestors-of-a-node-in-a-directed-acyclic-graph/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2192-all-ancestors-of-a-node-in-a-directed-acyclic-graph/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 62b9d3ff6b7535470efccd6650971d256ade3c0e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 29 Jun 2024 08:14:11 +0530 Subject: [PATCH 1221/1399] Time: 128 ms (60.70%), Space: 62.5 MB (99.12%) - LeetHub From b39f923740d09aefb835ffb345ca53e3c2b98761 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 29 Jun 2024 08:19:57 +0530 Subject: [PATCH 1222/1399] Attach NOTES - LeetHub From b44ea023637b1021ebb91e8a9fb379891f1c2921 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 29 Jun 2024 08:20:00 +0530 Subject: [PATCH 1223/1399] Time: 128 ms (60.70%), Space: 62.5 MB (99.12%) - LeetHub From fc16bf853b72eea322079efff74d3e0dff3d5a4d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 30 Jun 2024 09:34:46 +0530 Subject: [PATCH 1224/1399] Create README - LeetHub --- .../README.md | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 3202-find-the-maximum-length-of-valid-subsequence-ii/README.md diff --git a/3202-find-the-maximum-length-of-valid-subsequence-ii/README.md b/3202-find-the-maximum-length-of-valid-subsequence-ii/README.md new file mode 100644 index 00000000..d8d412fe --- /dev/null +++ b/3202-find-the-maximum-length-of-valid-subsequence-ii/README.md @@ -0,0 +1,41 @@ +

3202. Find the Maximum Length of Valid Subsequence II

Medium


You are given an integer array nums and a positive integer k. +

A subsequence sub of nums with length x is called valid if it satisfies:

+ +
    +
  • (sub[0] + sub[1]) % k == (sub[1] + sub[2]) % k == ... == (sub[x - 2] + sub[x - 1]) % k.
  • +
+Return the length of the longest valid subsequence of nums. +

 

+

Example 1:

+ +
+

Input: nums = [1,2,3,4,5], k = 2

+ +

Output: 5

+ +

Explanation:

+ +

The longest valid subsequence is [1, 2, 3, 4, 5].

+
+ +

Example 2:

+ +
+

Input: nums = [1,4,2,3,1,4], k = 3

+ +

Output: 4

+ +

Explanation:

+ +

The longest valid subsequence is [1, 4, 1, 4].

+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= nums.length <= 103
  • +
  • 1 <= nums[i] <= 107
  • +
  • 1 <= k <= 103
  • +
+
\ No newline at end of file From 005e6121521472f98f985fe54d76fe81ed046d1b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 30 Jun 2024 09:34:46 +0530 Subject: [PATCH 1225/1399] Attach NOTES - LeetHub --- 3202-find-the-maximum-length-of-valid-subsequence-ii/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 3202-find-the-maximum-length-of-valid-subsequence-ii/NOTES.md diff --git a/3202-find-the-maximum-length-of-valid-subsequence-ii/NOTES.md b/3202-find-the-maximum-length-of-valid-subsequence-ii/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/3202-find-the-maximum-length-of-valid-subsequence-ii/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From db01fa34bdaf6dff323582bc98fb2af23a7b1387 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 30 Jun 2024 09:34:50 +0530 Subject: [PATCH 1226/1399] Time: 107 ms (12.50%), Space: 127.1 MB (12.50%) - LeetHub --- ...maximum-length-of-valid-subsequence-ii.cpp | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 3202-find-the-maximum-length-of-valid-subsequence-ii/3202-find-the-maximum-length-of-valid-subsequence-ii.cpp diff --git a/3202-find-the-maximum-length-of-valid-subsequence-ii/3202-find-the-maximum-length-of-valid-subsequence-ii.cpp b/3202-find-the-maximum-length-of-valid-subsequence-ii/3202-find-the-maximum-length-of-valid-subsequence-ii.cpp new file mode 100644 index 00000000..30e02d3d --- /dev/null +++ b/3202-find-the-maximum-length-of-valid-subsequence-ii/3202-find-the-maximum-length-of-valid-subsequence-ii.cpp @@ -0,0 +1,33 @@ +class Solution { +public: + + int helper(vector& nums, int need, int k) + { + int ans = 0; + + vector dp(k+1, 0); + + for(int i = 0; i < nums.size(); ++i) + { + int rem = nums[i] % k; + + dp[rem] = max(dp[rem], dp[(need - rem + k)%k] + 1); + + ans = max(ans, dp[rem]); + } + + return ans; + } + + int maximumLength(vector& nums, int k) { + + int ans = 0; + + for(int i = 0; i < k; ++i) + { + ans = max(ans, helper(nums, i, k)); + } + + return ans; + } +}; \ No newline at end of file From e6fc0a8ceaa519ccc49cd9de130026f74259ea20 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 30 Jun 2024 09:53:55 +0530 Subject: [PATCH 1227/1399] Attach NOTES - LeetHub From 831b51aa41599e58b9e97c10c2fc6a9c47f38992 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 30 Jun 2024 09:53:58 +0530 Subject: [PATCH 1228/1399] Time: 86 ms (62.50%), Space: 127.2 MB (12.50%) - LeetHub From e508e5e51a8e00e837c7a587504b4abf317c0246 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 30 Jun 2024 10:19:32 +0530 Subject: [PATCH 1229/1399] Attach NOTES - LeetHub From 57e95110755d54c9ef496b1dd9039f90e80752d4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 30 Jun 2024 10:19:35 +0530 Subject: [PATCH 1230/1399] Time: 95 ms (62.50%), Space: 127.1 MB (12.50%) - LeetHub --- ...maximum-length-of-valid-subsequence-ii.cpp | 30 +++++++------------ 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/3202-find-the-maximum-length-of-valid-subsequence-ii/3202-find-the-maximum-length-of-valid-subsequence-ii.cpp b/3202-find-the-maximum-length-of-valid-subsequence-ii/3202-find-the-maximum-length-of-valid-subsequence-ii.cpp index 30e02d3d..88f28610 100644 --- a/3202-find-the-maximum-length-of-valid-subsequence-ii/3202-find-the-maximum-length-of-valid-subsequence-ii.cpp +++ b/3202-find-the-maximum-length-of-valid-subsequence-ii/3202-find-the-maximum-length-of-valid-subsequence-ii.cpp @@ -1,31 +1,21 @@ class Solution { public: - - int helper(vector& nums, int need, int k) - { - int ans = 0; - - vector dp(k+1, 0); - - for(int i = 0; i < nums.size(); ++i) - { - int rem = nums[i] % k; - - dp[rem] = max(dp[rem], dp[(need - rem + k)%k] + 1); - - ans = max(ans, dp[rem]); - } - - return ans; - } - int maximumLength(vector& nums, int k) { int ans = 0; for(int i = 0; i < k; ++i) { - ans = max(ans, helper(nums, i, k)); + vector dp(k, 0); + + for(auto& x : nums) + { + int rem = x % k; + + dp[rem] = max(dp[rem], dp[(i - rem + k)%k] + 1); + + ans = max(ans, dp[rem]); + } } return ans; From 73cbd34ddc3423617b80e66ff1f3859f71b5cb7d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 30 Jun 2024 22:39:38 +0530 Subject: [PATCH 1231/1399] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1579-remove-max-number-of-edges-to-keep-graph-fully-traversable/NOTES.md diff --git a/1579-remove-max-number-of-edges-to-keep-graph-fully-traversable/NOTES.md b/1579-remove-max-number-of-edges-to-keep-graph-fully-traversable/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1579-remove-max-number-of-edges-to-keep-graph-fully-traversable/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 5335e5b93d44b92628692682d11f5bbffba17045 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 1 Jul 2024 19:15:02 +0530 Subject: [PATCH 1232/1399] Attach NOTES - LeetHub From 649a58db3850707f66b8b816cb0191f7512efed6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 1 Jul 2024 19:16:51 +0530 Subject: [PATCH 1233/1399] Create README - LeetHub --- 1550-three-consecutive-odds/README.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 1550-three-consecutive-odds/README.md diff --git a/1550-three-consecutive-odds/README.md b/1550-three-consecutive-odds/README.md new file mode 100644 index 00000000..6efb9b86 --- /dev/null +++ b/1550-three-consecutive-odds/README.md @@ -0,0 +1,24 @@ +

1550. Three Consecutive Odds

Easy


Given an integer array arr, return true if there are three consecutive odd numbers in the array. Otherwise, return false. +

 

+

Example 1:

+ +
Input: arr = [2,6,4,1]
+Output: false
+Explanation: There are no three consecutive odds.
+
+ +

Example 2:

+ +
Input: arr = [1,2,34,3,4,5,7,23,12]
+Output: true
+Explanation: [5,7,23] are three consecutive odds.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= arr.length <= 1000
  • +
  • 1 <= arr[i] <= 1000
  • +
+
\ No newline at end of file From 3a67519544483fae1f8d7739d89209c9b94cc165 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 1 Jul 2024 19:16:54 +0530 Subject: [PATCH 1234/1399] Time: 0 ms (100.00%), Space: 10.5 MB (83.42%) - LeetHub --- .../1550-three-consecutive-odds.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 1550-three-consecutive-odds/1550-three-consecutive-odds.cpp diff --git a/1550-three-consecutive-odds/1550-three-consecutive-odds.cpp b/1550-three-consecutive-odds/1550-three-consecutive-odds.cpp new file mode 100644 index 00000000..9d510a25 --- /dev/null +++ b/1550-three-consecutive-odds/1550-three-consecutive-odds.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + bool threeConsecutiveOdds(vector& arr) { + + int n = arr.size(); + + if(n < 3) + return false; + + for(int i = 2; i < n; ++i) + { + if(arr[i] & 1 and arr[i-1] & 1 and arr[i-2] & 1) return true; + } + + return false; + } +}; \ No newline at end of file From ebe57ff75ffeddf9f729cb2618c6bb2f478d7068 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 1 Jul 2024 19:17:36 +0530 Subject: [PATCH 1235/1399] Attach NOTES - LeetHub --- 1550-three-consecutive-odds/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1550-three-consecutive-odds/NOTES.md diff --git a/1550-three-consecutive-odds/NOTES.md b/1550-three-consecutive-odds/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1550-three-consecutive-odds/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 7cef70cbdad64178c6e30df3236a4b1b5be64090 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 1 Jul 2024 19:17:39 +0530 Subject: [PATCH 1236/1399] Time: 0 ms (100.00%), Space: 10.5 MB (83.42%) - LeetHub From a848dc54649df27f205d087424244165ea8668bd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 2 Jul 2024 07:38:58 +0530 Subject: [PATCH 1237/1399] Create README - LeetHub --- 0350-intersection-of-two-arrays-ii/README.md | 33 ++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 0350-intersection-of-two-arrays-ii/README.md diff --git a/0350-intersection-of-two-arrays-ii/README.md b/0350-intersection-of-two-arrays-ii/README.md new file mode 100644 index 00000000..20f1306d --- /dev/null +++ b/0350-intersection-of-two-arrays-ii/README.md @@ -0,0 +1,33 @@ +

350. Intersection of Two Arrays II

Easy


Given two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must appear as many times as it shows in both arrays and you may return the result in any order.

+ +

 

+

Example 1:

+ +
Input: nums1 = [1,2,2,1], nums2 = [2,2]
+Output: [2,2]
+
+ +

Example 2:

+ +
Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
+Output: [4,9]
+Explanation: [9,4] is also accepted.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums1.length, nums2.length <= 1000
  • +
  • 0 <= nums1[i], nums2[i] <= 1000
  • +
+ +

 

+

Follow up:

+ +
    +
  • What if the given array is already sorted? How would you optimize your algorithm?
  • +
  • What if nums1's size is small compared to nums2's size? Which algorithm is better?
  • +
  • What if elements of nums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?
  • +
+
\ No newline at end of file From 66ff9c88c5309f6c7aae21a8db238827e11c88e9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 2 Jul 2024 07:39:02 +0530 Subject: [PATCH 1238/1399] Time: 7 ms (27.72%), Space: 13.4 MB (38.99%) - LeetHub --- .../0350-intersection-of-two-arrays-ii.cpp | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 0350-intersection-of-two-arrays-ii/0350-intersection-of-two-arrays-ii.cpp diff --git a/0350-intersection-of-two-arrays-ii/0350-intersection-of-two-arrays-ii.cpp b/0350-intersection-of-two-arrays-ii/0350-intersection-of-two-arrays-ii.cpp new file mode 100644 index 00000000..f0f633ae --- /dev/null +++ b/0350-intersection-of-two-arrays-ii/0350-intersection-of-two-arrays-ii.cpp @@ -0,0 +1,30 @@ +class Solution { +public: + vector intersect(vector& nums1, vector& nums2) { + + map mp, mp2; + + vector ans; + + for(auto& ele : nums1) + { + ++mp[ele]; + } + + for(auto& ele : nums2) + { + ++mp2[ele]; + } + + for(auto&[f,e] : mp) + { + if(mp2.find(f) != mp2.end()) + { + for(int i = 0; i < min(e, mp2[f]); ++i) + ans.push_back(f); + } + } + + return ans; + } +}; \ No newline at end of file From 10540a235acef3ea990629c771c8d8aaf81a5365 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 3 Jul 2024 09:01:01 +0530 Subject: [PATCH 1239/1399] Create README - LeetHub --- .../README.md | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 1509-minimum-difference-between-largest-and-smallest-value-in-three-moves/README.md diff --git a/1509-minimum-difference-between-largest-and-smallest-value-in-three-moves/README.md b/1509-minimum-difference-between-largest-and-smallest-value-in-three-moves/README.md new file mode 100644 index 00000000..ea8a7485 --- /dev/null +++ b/1509-minimum-difference-between-largest-and-smallest-value-in-three-moves/README.md @@ -0,0 +1,48 @@ +

1509. Minimum Difference Between Largest and Smallest Value in Three Moves

Medium


You are given an integer array nums.

+ +

In one move, you can choose one element of nums and change it to any value.

+ +

Return the minimum difference between the largest and smallest value of nums after performing at most three moves.

+ +

 

+

Example 1:

+ +
Input: nums = [5,3,2,4]
+Output: 0
+Explanation: We can make at most 3 moves.
+In the first move, change 2 to 3. nums becomes [5,3,3,4].
+In the second move, change 4 to 3. nums becomes [5,3,3,3].
+In the third move, change 5 to 3. nums becomes [3,3,3,3].
+After performing 3 moves, the difference between the minimum and maximum is 3 - 3 = 0.
+
+ +

Example 2:

+ +
Input: nums = [1,5,0,10,14]
+Output: 1
+Explanation: We can make at most 3 moves.
+In the first move, change 5 to 0. nums becomes [1,0,0,10,14].
+In the second move, change 10 to 0. nums becomes [1,0,0,0,14].
+In the third move, change 14 to 1. nums becomes [1,0,0,0,1].
+After performing 3 moves, the difference between the minimum and maximum is 1 - 0 = 1.
+It can be shown that there is no way to make the difference 0 in 3 moves.
+ +

Example 3:

+ +
Input: nums = [3,100,20]
+Output: 0
+Explanation: We can make at most 3 moves.
+In the first move, change 100 to 7. nums becomes [3,7,20].
+In the second move, change 20 to 7. nums becomes [3,7,7].
+In the third move, change 3 to 7. nums becomes [7,7,7].
+After performing 3 moves, the difference between the minimum and maximum is 7 - 7 = 0.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 105
  • +
  • -109 <= nums[i] <= 109
  • +
+
\ No newline at end of file From df0a089a6aeb0b7a51ab333fc47b5f334a6e685c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 3 Jul 2024 09:01:04 +0530 Subject: [PATCH 1240/1399] Time: 66 ms (72.70%), Space: 38.7 MB (50.84%) - LeetHub --- ...gest-and-smallest-value-in-three-moves.cpp | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 1509-minimum-difference-between-largest-and-smallest-value-in-three-moves/1509-minimum-difference-between-largest-and-smallest-value-in-three-moves.cpp diff --git a/1509-minimum-difference-between-largest-and-smallest-value-in-three-moves/1509-minimum-difference-between-largest-and-smallest-value-in-three-moves.cpp b/1509-minimum-difference-between-largest-and-smallest-value-in-three-moves/1509-minimum-difference-between-largest-and-smallest-value-in-three-moves.cpp new file mode 100644 index 00000000..f0cfa336 --- /dev/null +++ b/1509-minimum-difference-between-largest-and-smallest-value-in-three-moves/1509-minimum-difference-between-largest-and-smallest-value-in-three-moves.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + int minDifference(vector& nums) { + + int n = nums.size(); + + if(n <= 3) + return 0; + + auto calc = [&](int i, int j) + { + return nums[j] - nums[i]; + }; + + sort(nums.begin(), nums.end()); + + return min({calc(3, n-1), calc(0, n-4), calc(1, n-3), calc(2, n-2)}); + + } +}; \ No newline at end of file From 97342965b03c5bdcb0744d3c6b2b71704331ad39 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 3 Jul 2024 09:01:33 +0530 Subject: [PATCH 1241/1399] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1509-minimum-difference-between-largest-and-smallest-value-in-three-moves/NOTES.md diff --git a/1509-minimum-difference-between-largest-and-smallest-value-in-three-moves/NOTES.md b/1509-minimum-difference-between-largest-and-smallest-value-in-three-moves/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1509-minimum-difference-between-largest-and-smallest-value-in-three-moves/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 3a721ac8670f437fb16431a0213e117c5d1d9e1c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 3 Jul 2024 09:01:37 +0530 Subject: [PATCH 1242/1399] Time: 66 ms (72.70%), Space: 38.7 MB (50.84%) - LeetHub From 717efaa484b719b08360f13d7365879bb00ab20b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 3 Jul 2024 09:01:51 +0530 Subject: [PATCH 1243/1399] Attach NOTES - LeetHub From b605a4404557889f968fbf01ae0a72c60f4271a8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 3 Jul 2024 09:01:55 +0530 Subject: [PATCH 1244/1399] Time: 66 ms (72.70%), Space: 38.7 MB (50.84%) - LeetHub From 89913d741b5618e1921d517700d1006572d43a91 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 4 Jul 2024 07:59:50 +0530 Subject: [PATCH 1245/1399] Create README - LeetHub --- 2181-merge-nodes-in-between-zeros/README.md | 38 +++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 2181-merge-nodes-in-between-zeros/README.md diff --git a/2181-merge-nodes-in-between-zeros/README.md b/2181-merge-nodes-in-between-zeros/README.md new file mode 100644 index 00000000..b70d21cf --- /dev/null +++ b/2181-merge-nodes-in-between-zeros/README.md @@ -0,0 +1,38 @@ +

2181. Merge Nodes in Between Zeros

Medium


You are given the head of a linked list, which contains a series of integers separated by 0's. The beginning and end of the linked list will have Node.val == 0.

+ +

For every two consecutive 0's, merge all the nodes lying in between them into a single node whose value is the sum of all the merged nodes. The modified list should not contain any 0's.

+ +

Return the head of the modified linked list.

+ +

 

+

Example 1:

+ +
Input: head = [0,3,1,0,4,5,2,0]
+Output: [4,11]
+Explanation: 
+The above figure represents the given linked list. The modified list contains
+- The sum of the nodes marked in green: 3 + 1 = 4.
+- The sum of the nodes marked in red: 4 + 5 + 2 = 11.
+
+ +

Example 2:

+ +
Input: head = [0,1,0,3,0,2,2,0]
+Output: [1,3,4]
+Explanation: 
+The above figure represents the given linked list. The modified list contains
+- The sum of the nodes marked in green: 1 = 1.
+- The sum of the nodes marked in red: 3 = 3.
+- The sum of the nodes marked in yellow: 2 + 2 = 4.
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the list is in the range [3, 2 * 105].
  • +
  • 0 <= Node.val <= 1000
  • +
  • There are no two consecutive nodes with Node.val == 0.
  • +
  • The beginning and end of the linked list have Node.val == 0.
  • +
+
\ No newline at end of file From c616c4b8251b121e4d2cd424f25ea15dfae751eb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 4 Jul 2024 07:59:54 +0530 Subject: [PATCH 1246/1399] Time: 446 ms (54.18%), Space: 256.7 MB (76.60%) - LeetHub --- .../2181-merge-nodes-in-between-zeros.cpp | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 2181-merge-nodes-in-between-zeros/2181-merge-nodes-in-between-zeros.cpp diff --git a/2181-merge-nodes-in-between-zeros/2181-merge-nodes-in-between-zeros.cpp b/2181-merge-nodes-in-between-zeros/2181-merge-nodes-in-between-zeros.cpp new file mode 100644 index 00000000..26bc8244 --- /dev/null +++ b/2181-merge-nodes-in-between-zeros/2181-merge-nodes-in-between-zeros.cpp @@ -0,0 +1,39 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + ListNode* mergeNodes(ListNode* head) { + + ListNode* dummy = new ListNode(), *ptr = dummy; + + int curSum = 0; + + head = head->next; + + while(head) + { + curSum += head->val; + if(head->val == 0) + { + head->val = curSum; + ptr->next = head; + ptr = ptr->next; + curSum = 0 ; + } + head = head->next; + } + + ptr->next = nullptr; + + return dummy->next; + + } +}; \ No newline at end of file From 8aefba178d05d1fbe082fb79d67f9f126999d568 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 4 Jul 2024 23:04:39 +0530 Subject: [PATCH 1247/1399] Create README - LeetHub --- .../README.md | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 3203-find-minimum-diameter-after-merging-two-trees/README.md diff --git a/3203-find-minimum-diameter-after-merging-two-trees/README.md b/3203-find-minimum-diameter-after-merging-two-trees/README.md new file mode 100644 index 00000000..97f18b2d --- /dev/null +++ b/3203-find-minimum-diameter-after-merging-two-trees/README.md @@ -0,0 +1,48 @@ +

3203. Find Minimum Diameter After Merging Two Trees

Hard


There exist two undirected trees with n and m nodes, numbered from 0 to n - 1 and from 0 to m - 1, respectively. You are given two 2D integer arrays edges1 and edges2 of lengths n - 1 and m - 1, respectively, where edges1[i] = [ai, bi] indicates that there is an edge between nodes ai and bi in the first tree and edges2[i] = [ui, vi] indicates that there is an edge between nodes ui and vi in the second tree.

+ +

You must connect one node from the first tree with another node from the second tree with an edge.

+ +

Return the minimum possible diameter of the resulting tree.

+ +

The diameter of a tree is the length of the longest path between any two nodes in the tree.

+ +

 

+

Example 1:

+ +
+

Input: edges1 = [[0,1],[0,2],[0,3]], edges2 = [[0,1]]

+ +

Output: 3

+ +

Explanation:

+ +

We can obtain a tree of diameter 3 by connecting node 0 from the first tree with any node from the second tree.

+
+ +

Example 2:

+ +
+

Input: edges1 = [[0,1],[0,2],[0,3],[2,4],[2,5],[3,6],[2,7]], edges2 = [[0,1],[0,2],[0,3],[2,4],[2,5],[3,6],[2,7]]

+ +

Output: 5

+ +

Explanation:

+ +

We can obtain a tree of diameter 5 by connecting node 0 from the first tree with node 0 from the second tree.

+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n, m <= 105
  • +
  • edges1.length == n - 1
  • +
  • edges2.length == m - 1
  • +
  • edges1[i].length == edges2[i].length == 2
  • +
  • edges1[i] = [ai, bi]
  • +
  • 0 <= ai, bi < n
  • +
  • edges2[i] = [ui, vi]
  • +
  • 0 <= ui, vi < m
  • +
  • The input is generated such that edges1 and edges2 represent valid trees.
  • +
+
\ No newline at end of file From 66a91ef91b794fa59648d3e1517cf985dd5a1fe0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 4 Jul 2024 23:04:43 +0530 Subject: [PATCH 1248/1399] Time: 2269 ms (7.75%), Space: 305 MB (62.16%) - LeetHub --- ...nimum-diameter-after-merging-two-trees.cpp | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 3203-find-minimum-diameter-after-merging-two-trees/3203-find-minimum-diameter-after-merging-two-trees.cpp diff --git a/3203-find-minimum-diameter-after-merging-two-trees/3203-find-minimum-diameter-after-merging-two-trees.cpp b/3203-find-minimum-diameter-after-merging-two-trees/3203-find-minimum-diameter-after-merging-two-trees.cpp new file mode 100644 index 00000000..0ad2e2d1 --- /dev/null +++ b/3203-find-minimum-diameter-after-merging-two-trees/3203-find-minimum-diameter-after-merging-two-trees.cpp @@ -0,0 +1,52 @@ +class Solution { +public: + + void constructingList(vector>& edges, vector adj[]) + { + for(auto& edge : edges) + { + int u = edge[0]; + int v = edge[1]; + + adj[u].push_back(v); + adj[v].push_back(u); + } + } + + int maximumDiameter(vector adj[], int n) + { + vector dist(n, 0); + + function dfs = [&](int sv, int d) + { + dist[sv] = d; + for(auto& node : adj[sv]) + { + if(dist[node] == 0) + dfs(node, d+1); + } + }; + + dfs(0, 1); + int farthest = max_element(dist.begin(), dist.end()) - dist.begin(); + fill(dist.begin(), dist.end(), 0); + dfs(farthest, 1); + return *max_element(dist.begin(), dist.end()) - 1; + } + + int minimumDiameterAfterMerge(vector>& edges1, vector>& edges2) { + + int n = edges1.size(); + int m = edges2.size(); + + vector adj[n+1], adj2[m+1]; + + constructingList(edges1, adj); + constructingList(edges2, adj2); + + int diameter1 = maximumDiameter(adj, n+1); + int diameter2 = maximumDiameter(adj2, m+1); + + return max({diameter1, diameter2, (diameter1 + 1)/2 + (diameter2 + 1)/2 + 1}); + } +}; \ No newline at end of file From b3c96367ad1d396d3b8589349bd17b674c6d2769 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 5 Jul 2024 08:44:20 +0530 Subject: [PATCH 1249/1399] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2058-find-the-minimum-and-maximum-number-of-nodes-between-critical-points/NOTES.md diff --git a/2058-find-the-minimum-and-maximum-number-of-nodes-between-critical-points/NOTES.md b/2058-find-the-minimum-and-maximum-number-of-nodes-between-critical-points/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2058-find-the-minimum-and-maximum-number-of-nodes-between-critical-points/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 491d8deef2465c11cde90d61bb3cc6ddb5abc69e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 5 Jul 2024 08:44:24 +0530 Subject: [PATCH 1250/1399] Time: 170 ms (46.44%), Space: 115.8 MB (58.25%) - LeetHub --- ...umber-of-nodes-between-critical-points.cpp | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 2058-find-the-minimum-and-maximum-number-of-nodes-between-critical-points/2058-find-the-minimum-and-maximum-number-of-nodes-between-critical-points.cpp diff --git a/2058-find-the-minimum-and-maximum-number-of-nodes-between-critical-points/2058-find-the-minimum-and-maximum-number-of-nodes-between-critical-points.cpp b/2058-find-the-minimum-and-maximum-number-of-nodes-between-critical-points/2058-find-the-minimum-and-maximum-number-of-nodes-between-critical-points.cpp new file mode 100644 index 00000000..5e5ca5a6 --- /dev/null +++ b/2058-find-the-minimum-and-maximum-number-of-nodes-between-critical-points/2058-find-the-minimum-and-maximum-number-of-nodes-between-critical-points.cpp @@ -0,0 +1,49 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + vector nodesBetweenCriticalPoints(ListNode* head) { + + ListNode* prev = nullptr; + + int leftEnd = -1, nearestEnd = -1; + int minDist = INT_MAX, maxDist = INT_MIN; + int pos = 1; + + while(head) + { + if(head->next and prev) + { + if((prev->val < head->val and head->next->val < head->val) or (prev->val > head->val and head->next->val > head->val)) + { + if(leftEnd == -1) + { + leftEnd = pos; + nearestEnd = pos; + } + else + { + minDist = min(minDist, pos - nearestEnd); + maxDist = max(maxDist, pos - leftEnd); + nearestEnd = pos; + } + } + } + prev = head; + head = head->next; + ++pos; + } + + if(minDist == INT_MAX or maxDist == INT_MIN) + return {-1, -1}; + return {minDist, maxDist}; + } +}; \ No newline at end of file From 2bd20ba49b184a603f49cbf27ab173d14f33f4eb Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 6 Jul 2024 19:12:39 +0530 Subject: [PATCH 1251/1399] Create README - LeetHub --- 2582-pass-the-pillow/README.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 2582-pass-the-pillow/README.md diff --git a/2582-pass-the-pillow/README.md b/2582-pass-the-pillow/README.md new file mode 100644 index 00000000..fcea9416 --- /dev/null +++ b/2582-pass-the-pillow/README.md @@ -0,0 +1,32 @@ +

2582. Pass the Pillow

Easy


There are n people standing in a line labeled from 1 to n. The first person in the line is holding a pillow initially. Every second, the person holding the pillow passes it to the next person standing in the line. Once the pillow reaches the end of the line, the direction changes, and people continue passing the pillow in the opposite direction.

+ +
    +
  • For example, once the pillow reaches the nth person they pass it to the n - 1th person, then to the n - 2th person and so on.
  • +
+ +

Given the two positive integers n and time, return the index of the person holding the pillow after time seconds.

+

 

+

Example 1:

+ +
Input: n = 4, time = 5
+Output: 2
+Explanation: People pass the pillow in the following way: 1 -> 2 -> 3 -> 4 -> 3 -> 2.
+After five seconds, the 2nd person is holding the pillow.
+
+ +

Example 2:

+ +
Input: n = 3, time = 2
+Output: 3
+Explanation: People pass the pillow in the following way: 1 -> 2 -> 3.
+After two seconds, the 3rd person is holding the pillow.
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= n <= 1000
  • +
  • 1 <= time <= 1000
  • +
+
\ No newline at end of file From 97cf6439585f3d161fe9905adfefc9fd4d8921e3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 6 Jul 2024 19:12:43 +0530 Subject: [PATCH 1252/1399] Time: 0 ms (100.00%), Space: 7.1 MB (30.58%) - LeetHub --- 2582-pass-the-pillow/2582-pass-the-pillow.cpp | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 2582-pass-the-pillow/2582-pass-the-pillow.cpp diff --git a/2582-pass-the-pillow/2582-pass-the-pillow.cpp b/2582-pass-the-pillow/2582-pass-the-pillow.cpp new file mode 100644 index 00000000..4e73b840 --- /dev/null +++ b/2582-pass-the-pillow/2582-pass-the-pillow.cpp @@ -0,0 +1,29 @@ +class Solution { +public: + int passThePillow(int n, int time) { + + int dir = 1, pos = 1; + + while(time--) + { + if(dir) + { + ++pos; + if(pos == n) + { + dir = 0; + } + } + else + { + --pos; + if(pos == 1) + { + dir = 1; + } + } + } + + return pos; + } +}; \ No newline at end of file From 422214b82c37ab8ad3d7726f3c5518cff86daad0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 6 Jul 2024 19:14:02 +0530 Subject: [PATCH 1253/1399] Attach NOTES - LeetHub --- 2582-pass-the-pillow/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2582-pass-the-pillow/NOTES.md diff --git a/2582-pass-the-pillow/NOTES.md b/2582-pass-the-pillow/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2582-pass-the-pillow/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 6e59790c4b50b699e4d85f65295519a694157230 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 6 Jul 2024 19:14:05 +0530 Subject: [PATCH 1254/1399] Time: 0 ms (100.00%), Space: 7.1 MB (30.58%) - LeetHub From 89421d347e7352261b408d2d00d19ee08e2e39ec Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 6 Jul 2024 19:20:02 +0530 Subject: [PATCH 1255/1399] Attach NOTES - LeetHub From bd7e605ebc367a4c0b707bf3e3dc3cd617c88c28 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 6 Jul 2024 19:20:06 +0530 Subject: [PATCH 1256/1399] Time: 0 ms (100.00%), Space: 7.1 MB (30.58%) - LeetHub From 6a32324543fc51b45578bf4adf041c3cb8d831da Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 6 Jul 2024 19:31:07 +0530 Subject: [PATCH 1257/1399] Attach NOTES - LeetHub From f819b816547b99bf111470b1d0d6d0f0b75ebb71 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 6 Jul 2024 19:31:10 +0530 Subject: [PATCH 1258/1399] Time: 2 ms (54.14%), Space: 7 MB (71.18%) - LeetHub --- 2582-pass-the-pillow/2582-pass-the-pillow.cpp | 24 +++---------------- 1 file changed, 3 insertions(+), 21 deletions(-) diff --git a/2582-pass-the-pillow/2582-pass-the-pillow.cpp b/2582-pass-the-pillow/2582-pass-the-pillow.cpp index 4e73b840..52a89766 100644 --- a/2582-pass-the-pillow/2582-pass-the-pillow.cpp +++ b/2582-pass-the-pillow/2582-pass-the-pillow.cpp @@ -2,28 +2,10 @@ class Solution { public: int passThePillow(int n, int time) { - int dir = 1, pos = 1; + int fullChunks = time / (n-1); + int extTime = time % (n-1); - while(time--) - { - if(dir) - { - ++pos; - if(pos == n) - { - dir = 0; - } - } - else - { - --pos; - if(pos == 1) - { - dir = 1; - } - } - } + return (fullChunks & 1 ? n - extTime : extTime + 1); - return pos; } }; \ No newline at end of file From 5b7d0537a18235def977266626f9099bfa2e6813 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 7 Jul 2024 22:46:00 +0530 Subject: [PATCH 1259/1399] Create README - LeetHub --- 1518-water-bottles/README.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 1518-water-bottles/README.md diff --git a/1518-water-bottles/README.md b/1518-water-bottles/README.md new file mode 100644 index 00000000..cb5e9b9e --- /dev/null +++ b/1518-water-bottles/README.md @@ -0,0 +1,31 @@ +

1518. Water Bottles

Easy


There are numBottles water bottles that are initially full of water. You can exchange numExchange empty water bottles from the market with one full water bottle.

+ +

The operation of drinking a full water bottle turns it into an empty bottle.

+ +

Given the two integers numBottles and numExchange, return the maximum number of water bottles you can drink.

+ +

 

+

Example 1:

+ +
Input: numBottles = 9, numExchange = 3
+Output: 13
+Explanation: You can exchange 3 empty bottles to get 1 full water bottle.
+Number of water bottles you can drink: 9 + 3 + 1 = 13.
+
+ +

Example 2:

+ +
Input: numBottles = 15, numExchange = 4
+Output: 19
+Explanation: You can exchange 4 empty bottles to get 1 full water bottle. 
+Number of water bottles you can drink: 15 + 3 + 1 = 19.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= numBottles <= 100
  • +
  • 2 <= numExchange <= 100
  • +
+
\ No newline at end of file From 0bd9b0b4d2302ef5765762eb51d3e77e94b9a911 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 7 Jul 2024 22:46:04 +0530 Subject: [PATCH 1260/1399] Time: 0 ms (100.00%), Space: 7.2 MB (30.43%) - LeetHub --- 1518-water-bottles/1518-water-bottles.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 1518-water-bottles/1518-water-bottles.cpp diff --git a/1518-water-bottles/1518-water-bottles.cpp b/1518-water-bottles/1518-water-bottles.cpp new file mode 100644 index 00000000..d7fba118 --- /dev/null +++ b/1518-water-bottles/1518-water-bottles.cpp @@ -0,0 +1,18 @@ +class Solution { +public: + int numWaterBottles(int numBottles, int numExchange) { + + int ans = numBottles; + + while((numBottles/numExchange) > 0) + { + ans += (numBottles/numExchange); + int rem = (numBottles%numExchange); + numBottles /= numExchange; + numBottles += rem; + } + + return ans; + + } +}; \ No newline at end of file From e50a0453123320a3fc47e805011e050013ef3632 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 7 Jul 2024 22:46:28 +0530 Subject: [PATCH 1261/1399] Attach NOTES - LeetHub --- 1518-water-bottles/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1518-water-bottles/NOTES.md diff --git a/1518-water-bottles/NOTES.md b/1518-water-bottles/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1518-water-bottles/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 7cbde9916fdc99b90860a9fdf86c15ca12a6288a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 7 Jul 2024 22:46:31 +0530 Subject: [PATCH 1262/1399] Time: 0 ms (100.00%), Space: 7.2 MB (30.43%) - LeetHub From 88ba925fc3bfbfae072c0b80d7a058977c4e3c69 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 15 Jul 2024 22:33:12 +0530 Subject: [PATCH 1263/1399] Create README - LeetHub --- .../README.md | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 2196-create-binary-tree-from-descriptions/README.md diff --git a/2196-create-binary-tree-from-descriptions/README.md b/2196-create-binary-tree-from-descriptions/README.md new file mode 100644 index 00000000..3d44829b --- /dev/null +++ b/2196-create-binary-tree-from-descriptions/README.md @@ -0,0 +1,39 @@ +

2196. Create Binary Tree From Descriptions

Medium


You are given a 2D integer array descriptions where descriptions[i] = [parenti, childi, isLefti] indicates that parenti is the parent of childi in a binary tree of unique values. Furthermore,

+ +
    +
  • If isLefti == 1, then childi is the left child of parenti.
  • +
  • If isLefti == 0, then childi is the right child of parenti.
  • +
+ +

Construct the binary tree described by descriptions and return its root.

+ +

The test cases will be generated such that the binary tree is valid.

+ +

 

+

Example 1:

+ +
Input: descriptions = [[20,15,1],[20,17,0],[50,20,1],[50,80,0],[80,19,1]]
+Output: [50,20,80,15,17,19]
+Explanation: The root node is the node with value 50 since it has no parent.
+The resulting binary tree is shown in the diagram.
+
+ +

Example 2:

+ +
Input: descriptions = [[1,2,1],[2,3,0],[3,4,1]]
+Output: [1,2,null,null,3,4]
+Explanation: The root node is the node with value 1 since it has no parent.
+The resulting binary tree is shown in the diagram.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= descriptions.length <= 104
  • +
  • descriptions[i].length == 3
  • +
  • 1 <= parenti, childi <= 105
  • +
  • 0 <= isLefti <= 1
  • +
  • The binary tree described by descriptions is valid.
  • +
+
\ No newline at end of file From 6c87acd8929ffd535b9c19c6cf18f80adb70e6f3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 15 Jul 2024 22:33:15 +0530 Subject: [PATCH 1264/1399] Time: 528 ms (98.04%), Space: 231 MB (98.25%) - LeetHub --- ...6-create-binary-tree-from-descriptions.cpp | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 2196-create-binary-tree-from-descriptions/2196-create-binary-tree-from-descriptions.cpp diff --git a/2196-create-binary-tree-from-descriptions/2196-create-binary-tree-from-descriptions.cpp b/2196-create-binary-tree-from-descriptions/2196-create-binary-tree-from-descriptions.cpp new file mode 100644 index 00000000..ac310fa9 --- /dev/null +++ b/2196-create-binary-tree-from-descriptions/2196-create-binary-tree-from-descriptions.cpp @@ -0,0 +1,32 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + TreeNode* createBinaryTree(vector>& descriptions) { + TreeNode* map[100001] = {}; + bool child[100001] = {}; + for (auto& d : descriptions){ + if (map[d[0]] == nullptr) map[d[0]] = new TreeNode(d[0]); + TreeNode* node = (map[d[1]] == nullptr ? new TreeNode(d[1]) : map[d[1]]); + if (d[2]) + map[d[0]]->left = node; + else + map[d[0]]->right = node; + map[node->val] = node; + child[d[1]] = true; + } + for (auto& d : descriptions) + if (!child[d[0]]) + return map[d[0]]; + return nullptr; + } +}; \ No newline at end of file From 4795846e0b6ffeba95298f314ff8a6e242fc197c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 16 Jul 2024 21:16:27 +0530 Subject: [PATCH 1265/1399] Create README - LeetHub --- .../README.md | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 0026-remove-duplicates-from-sorted-array/README.md diff --git a/0026-remove-duplicates-from-sorted-array/README.md b/0026-remove-duplicates-from-sorted-array/README.md new file mode 100644 index 00000000..ec027591 --- /dev/null +++ b/0026-remove-duplicates-from-sorted-array/README.md @@ -0,0 +1,52 @@ +

26. Remove Duplicates from Sorted Array

Easy


Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same. Then return the number of unique elements in nums.

+ +

Consider the number of unique elements of nums to be k, to get accepted, you need to do the following things:

+ +
    +
  • Change the array nums such that the first k elements of nums contain the unique elements in the order they were present in nums initially. The remaining elements of nums are not important as well as the size of nums.
  • +
  • Return k.
  • +
+ +

Custom Judge:

+ +

The judge will test your solution with the following code:

+ +
int[] nums = [...]; // Input array
+int[] expectedNums = [...]; // The expected answer with correct length
+
+int k = removeDuplicates(nums); // Calls your implementation
+
+assert k == expectedNums.length;
+for (int i = 0; i < k; i++) {
+    assert nums[i] == expectedNums[i];
+}
+
+ +

If all assertions pass, then your solution will be accepted.

+ +

 

+

Example 1:

+ +
Input: nums = [1,1,2]
+Output: 2, nums = [1,2,_]
+Explanation: Your function should return k = 2, with the first two elements of nums being 1 and 2 respectively.
+It does not matter what you leave beyond the returned k (hence they are underscores).
+
+ +

Example 2:

+ +
Input: nums = [0,0,1,1,1,2,2,3,3,4]
+Output: 5, nums = [0,1,2,3,4,_,_,_,_,_]
+Explanation: Your function should return k = 5, with the first five elements of nums being 0, 1, 2, 3, and 4 respectively.
+It does not matter what you leave beyond the returned k (hence they are underscores).
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 3 * 104
  • +
  • -100 <= nums[i] <= 100
  • +
  • nums is sorted in non-decreasing order.
  • +
+
\ No newline at end of file From 0423d04bb087735cd02be948820a9a3be0e81034 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 16 Jul 2024 22:43:58 +0530 Subject: [PATCH 1266/1399] Create README - LeetHub --- .../README.md | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 2096-step-by-step-directions-from-a-binary-tree-node-to-another/README.md diff --git a/2096-step-by-step-directions-from-a-binary-tree-node-to-another/README.md b/2096-step-by-step-directions-from-a-binary-tree-node-to-another/README.md new file mode 100644 index 00000000..26236aaf --- /dev/null +++ b/2096-step-by-step-directions-from-a-binary-tree-node-to-another/README.md @@ -0,0 +1,39 @@ +

2096. Step-By-Step Directions From a Binary Tree Node to Another

Medium


You are given the root of a binary tree with n nodes. Each node is uniquely assigned a value from 1 to n. You are also given an integer startValue representing the value of the start node s, and a different integer destValue representing the value of the destination node t.

+ +

Find the shortest path starting from node s and ending at node t. Generate step-by-step directions of such path as a string consisting of only the uppercase letters 'L', 'R', and 'U'. Each letter indicates a specific direction:

+ +
    +
  • 'L' means to go from a node to its left child node.
  • +
  • 'R' means to go from a node to its right child node.
  • +
  • 'U' means to go from a node to its parent node.
  • +
+ +

Return the step-by-step directions of the shortest path from node s to node t.

+ +

 

+

Example 1:

+ +
Input: root = [5,1,2,3,null,6,4], startValue = 3, destValue = 6
+Output: "UURL"
+Explanation: The shortest path is: 3 → 1 → 5 → 2 → 6.
+
+ +

Example 2:

+ +
Input: root = [2,1], startValue = 2, destValue = 1
+Output: "L"
+Explanation: The shortest path is: 2 → 1.
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is n.
  • +
  • 2 <= n <= 105
  • +
  • 1 <= Node.val <= n
  • +
  • All the values in the tree are unique.
  • +
  • 1 <= startValue, destValue <= n
  • +
  • startValue != destValue
  • +
+
\ No newline at end of file From f32d95b36dca2f0cfa341754aabb7bc8337a1a66 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 16 Jul 2024 22:44:04 +0530 Subject: [PATCH 1267/1399] Time: 156 ms (55.35%), Space: 121.8 MB (27.68%) - LeetHub --- ...ons-from-a-binary-tree-node-to-another.cpp | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 2096-step-by-step-directions-from-a-binary-tree-node-to-another/2096-step-by-step-directions-from-a-binary-tree-node-to-another.cpp diff --git a/2096-step-by-step-directions-from-a-binary-tree-node-to-another/2096-step-by-step-directions-from-a-binary-tree-node-to-another.cpp b/2096-step-by-step-directions-from-a-binary-tree-node-to-another/2096-step-by-step-directions-from-a-binary-tree-node-to-another.cpp new file mode 100644 index 00000000..54cbc44b --- /dev/null +++ b/2096-step-by-step-directions-from-a-binary-tree-node-to-another/2096-step-by-step-directions-from-a-binary-tree-node-to-another.cpp @@ -0,0 +1,56 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { + +private: + void rootToNode(TreeNode* root,int &n,int &m,string &temp,string &s,string &d){ + if(!root) return; + + if(root->val == n) s=temp; + if(root->val == m) d=temp; + + temp.push_back('L'); + rootToNode(root->left, n, m, temp, s, d); + temp.pop_back(); + + temp.push_back('R'); + rootToNode(root->right, n, m, temp, s, d); + temp.pop_back(); + } + + +public: + string getDirections(TreeNode* root, int n, int m) { + + string s,d,temp; + rootToNode(root,n,m,temp,s,d); + + int ind = 0; + + for(int i=0;i Date: Wed, 17 Jul 2024 08:27:08 +0530 Subject: [PATCH 1268/1399] Create README - LeetHub --- 0073-set-matrix-zeroes/README.md | 36 ++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 0073-set-matrix-zeroes/README.md diff --git a/0073-set-matrix-zeroes/README.md b/0073-set-matrix-zeroes/README.md new file mode 100644 index 00000000..574d6249 --- /dev/null +++ b/0073-set-matrix-zeroes/README.md @@ -0,0 +1,36 @@ +

73. Set Matrix Zeroes

Medium


Given an m x n integer matrix matrix, if an element is 0, set its entire row and column to 0's.

+ +

You must do it in place.

+ +

 

+

Example 1:

+ +
Input: matrix = [[1,1,1],[1,0,1],[1,1,1]]
+Output: [[1,0,1],[0,0,0],[1,0,1]]
+
+ +

Example 2:

+ +
Input: matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]
+Output: [[0,0,0,0],[0,4,5,0],[0,3,1,0]]
+
+ +

 

+

Constraints:

+ +
    +
  • m == matrix.length
  • +
  • n == matrix[0].length
  • +
  • 1 <= m, n <= 200
  • +
  • -231 <= matrix[i][j] <= 231 - 1
  • +
+ +

 

+

Follow up:

+ +
    +
  • A straightforward solution using O(mn) space is probably a bad idea.
  • +
  • A simple improvement uses O(m + n) space, but still not the best solution.
  • +
  • Could you devise a constant space solution?
  • +
+
\ No newline at end of file From d7cf27a2bb818ec34b6f60bd58dde17decfecf98 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 17 Jul 2024 12:23:13 +0530 Subject: [PATCH 1269/1399] Attach NOTES - LeetHub From e4df025ecb98ea1c699d610d02fe6e9d39e14d30 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 17 Jul 2024 21:58:34 +0530 Subject: [PATCH 1270/1399] Create README - LeetHub --- 1110-delete-nodes-and-return-forest/README.md | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 1110-delete-nodes-and-return-forest/README.md diff --git a/1110-delete-nodes-and-return-forest/README.md b/1110-delete-nodes-and-return-forest/README.md new file mode 100644 index 00000000..d613487a --- /dev/null +++ b/1110-delete-nodes-and-return-forest/README.md @@ -0,0 +1,29 @@ +

1110. Delete Nodes And Return Forest

Medium


Given the root of a binary tree, each node in the tree has a distinct value.

+ +

After deleting all nodes with a value in to_delete, we are left with a forest (a disjoint union of trees).

+ +

Return the roots of the trees in the remaining forest. You may return the result in any order.

+ +

 

+

Example 1:

+ +
Input: root = [1,2,3,4,5,6,7], to_delete = [3,5]
+Output: [[1,2,null,4],[6],[7]]
+
+ +

Example 2:

+ +
Input: root = [1,2,4,null,3], to_delete = [3]
+Output: [[1,2,4]]
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the given tree is at most 1000.
  • +
  • Each node has a distinct value between 1 and 1000.
  • +
  • to_delete.length <= 1000
  • +
  • to_delete contains distinct values between 1 and 1000.
  • +
+
\ No newline at end of file From e53989c8adb23228a82a76d04cb90b77ce8c357a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 17 Jul 2024 21:58:35 +0530 Subject: [PATCH 1271/1399] Attach NOTES - LeetHub --- 1110-delete-nodes-and-return-forest/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1110-delete-nodes-and-return-forest/NOTES.md diff --git a/1110-delete-nodes-and-return-forest/NOTES.md b/1110-delete-nodes-and-return-forest/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1110-delete-nodes-and-return-forest/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From abdc21442e3b42146cad966530235136f3ae16f3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 17 Jul 2024 21:58:38 +0530 Subject: [PATCH 1272/1399] Time: 17 ms (45.72%), Space: 27 MB (25.05%) - LeetHub --- .../1110-delete-nodes-and-return-forest.cpp | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 1110-delete-nodes-and-return-forest/1110-delete-nodes-and-return-forest.cpp diff --git a/1110-delete-nodes-and-return-forest/1110-delete-nodes-and-return-forest.cpp b/1110-delete-nodes-and-return-forest/1110-delete-nodes-and-return-forest.cpp new file mode 100644 index 00000000..b0d40593 --- /dev/null +++ b/1110-delete-nodes-and-return-forest/1110-delete-nodes-and-return-forest.cpp @@ -0,0 +1,31 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + bool set[1001] = {}; + void dfs(TreeNode* &root, bool flag, vector& res){ + if (root == nullptr) return; + dfs(root->left, set[root->val], res); + dfs(root->right, set[root->val], res); + if (!set[root->val] && flag) res.push_back(root); + if (set[root->val]) root = nullptr; + } +public: + vector delNodes(TreeNode* root, vector& to_delete) { + vector res; + for (int num : to_delete) + set[num] = true; + dfs(root, true, res); + return res; + } + +}; \ No newline at end of file From c28e0b3195eab9aa556d7b0eb2288cf5b31e8891 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 18 Jul 2024 23:07:54 +0530 Subject: [PATCH 1273/1399] Attach NOTES - LeetHub --- 1530-number-of-good-leaf-nodes-pairs/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1530-number-of-good-leaf-nodes-pairs/NOTES.md diff --git a/1530-number-of-good-leaf-nodes-pairs/NOTES.md b/1530-number-of-good-leaf-nodes-pairs/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1530-number-of-good-leaf-nodes-pairs/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From cc3c2993e68bd2af99c83c29c7da1a7831f6736e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 19 Jul 2024 19:29:19 +0530 Subject: [PATCH 1274/1399] Create README - LeetHub --- .../README.md | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 1423-maximum-points-you-can-obtain-from-cards/README.md diff --git a/1423-maximum-points-you-can-obtain-from-cards/README.md b/1423-maximum-points-you-can-obtain-from-cards/README.md new file mode 100644 index 00000000..89a65089 --- /dev/null +++ b/1423-maximum-points-you-can-obtain-from-cards/README.md @@ -0,0 +1,39 @@ +

1423. Maximum Points You Can Obtain from Cards

Medium


There are several cards arranged in a row, and each card has an associated number of points. The points are given in the integer array cardPoints.

+ +

In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards.

+ +

Your score is the sum of the points of the cards you have taken.

+ +

Given the integer array cardPoints and the integer k, return the maximum score you can obtain.

+ +

 

+

Example 1:

+ +
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
+Output: 12
+Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12.
+
+ +

Example 2:

+ +
Input: cardPoints = [2,2,2], k = 2
+Output: 4
+Explanation: Regardless of which two cards you take, your score will always be 4.
+
+ +

Example 3:

+ +
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
+Output: 55
+Explanation: You have to take all the cards. Your score is the sum of points of all cards.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= cardPoints.length <= 105
  • +
  • 1 <= cardPoints[i] <= 104
  • +
  • 1 <= k <= cardPoints.length
  • +
+
\ No newline at end of file From 695b806677e9329559863bbb271af0adff0641a9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 19 Jul 2024 19:29:22 +0530 Subject: [PATCH 1275/1399] Time: 48 ms (19.65%), Space: 48.3 MB (5.58%) - LeetHub --- ...ximum-points-you-can-obtain-from-cards.cpp | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 1423-maximum-points-you-can-obtain-from-cards/1423-maximum-points-you-can-obtain-from-cards.cpp diff --git a/1423-maximum-points-you-can-obtain-from-cards/1423-maximum-points-you-can-obtain-from-cards.cpp b/1423-maximum-points-you-can-obtain-from-cards/1423-maximum-points-you-can-obtain-from-cards.cpp new file mode 100644 index 00000000..ef988336 --- /dev/null +++ b/1423-maximum-points-you-can-obtain-from-cards/1423-maximum-points-you-can-obtain-from-cards.cpp @@ -0,0 +1,39 @@ +class Solution { +public: + + void solve(int k, int& ans, vector& pref, vector& suff) + { + int n = pref.size(); + for(int i = 0; i < k; ++i) + { + int end = n-(k-(i+1)); + int endSum = end < n ? suff[end] : 0; + ans = max(ans, pref[i] + endSum); + } + } + + int maxScore(vector& cardPoints, int k) { + + int n = cardPoints.size(); + vector pref(n, 0), suff(n, 0); + + pref[0] = cardPoints[0]; + suff[n-1] = cardPoints[n-1]; + + for(int i = 1; i < n; ++i) + pref[i] = (pref[i-1] + cardPoints[i]); + + for(int i = n-2; i >= 0; --i) + suff[i] = (suff[i+1] + cardPoints[i]); + + int ans = 0; + + solve(k, ans, pref, suff); + swap(pref, suff); + reverse(pref.begin(), pref.end()); + reverse(suff.begin(), suff.end()); + solve(k, ans, pref, suff); + + return ans; + } +}; \ No newline at end of file From f3c41ce35e799621fefd250149012b001d7e0b10 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 19 Jul 2024 19:33:57 +0530 Subject: [PATCH 1276/1399] Attach NOTES - LeetHub --- 1423-maximum-points-you-can-obtain-from-cards/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1423-maximum-points-you-can-obtain-from-cards/NOTES.md diff --git a/1423-maximum-points-you-can-obtain-from-cards/NOTES.md b/1423-maximum-points-you-can-obtain-from-cards/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1423-maximum-points-you-can-obtain-from-cards/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 368bd2146a1ef3997f168e39181667a02a41bf10 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 19 Jul 2024 19:34:00 +0530 Subject: [PATCH 1277/1399] Time: 48 ms (19.65%), Space: 48.3 MB (5.58%) - LeetHub From e63e306f4b0b1649c58de4c39676ec6101d1c57b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 19 Jul 2024 21:18:31 +0530 Subject: [PATCH 1278/1399] Create README - LeetHub --- 0054-spiral-matrix/README.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 0054-spiral-matrix/README.md diff --git a/0054-spiral-matrix/README.md b/0054-spiral-matrix/README.md new file mode 100644 index 00000000..afec7a07 --- /dev/null +++ b/0054-spiral-matrix/README.md @@ -0,0 +1,25 @@ +

54. Spiral Matrix

Medium


Given an m x n matrix, return all elements of the matrix in spiral order.

+ +

 

+

Example 1:

+ +
Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
+Output: [1,2,3,6,9,8,7,4,5]
+
+ +

Example 2:

+ +
Input: matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
+Output: [1,2,3,4,8,12,11,10,9,5,6,7]
+
+ +

 

+

Constraints:

+ +
    +
  • m == matrix.length
  • +
  • n == matrix[i].length
  • +
  • 1 <= m, n <= 10
  • +
  • -100 <= matrix[i][j] <= 100
  • +
+
\ No newline at end of file From 676893348b06e32765c9673452ed9007a98dfb63 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 19 Jul 2024 21:18:32 +0530 Subject: [PATCH 1279/1399] Attach NOTES - LeetHub --- 0054-spiral-matrix/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0054-spiral-matrix/NOTES.md diff --git a/0054-spiral-matrix/NOTES.md b/0054-spiral-matrix/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0054-spiral-matrix/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 15fb02c9f214ff00fda717ec1fbc30c57df4c49f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 19 Jul 2024 21:18:36 +0530 Subject: [PATCH 1280/1399] Time: 0 ms (100.00%), Space: 8.4 MB (9.65%) - LeetHub --- 0054-spiral-matrix/0054-spiral-matrix.cpp | 46 +++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 0054-spiral-matrix/0054-spiral-matrix.cpp diff --git a/0054-spiral-matrix/0054-spiral-matrix.cpp b/0054-spiral-matrix/0054-spiral-matrix.cpp new file mode 100644 index 00000000..3397eda9 --- /dev/null +++ b/0054-spiral-matrix/0054-spiral-matrix.cpp @@ -0,0 +1,46 @@ +class Solution { +public: + vector spiralOrder(vector>& matrix) { + + int n = matrix.size(); + int m = matrix[0].size(); + + int top = 0, left = 0, right = m-1, bottom = n-1, dir = 0; + vector ans; + + while(top <= bottom and left <= right) + { + if(dir == 0) + { + for(int i = left; i <= right; ++i) + ans.push_back(matrix[top][i]); + ++top; + } + + else if (dir == 1) + { + for(int i = top; i <= bottom; ++i) + ans.push_back(matrix[i][right]); + --right; + } + + else if(dir == 2) + { + for(int i = right; i >= left; --i) + ans.push_back(matrix[bottom][i]); + --bottom; + } + + else if(dir == 3) + { + for(int i = bottom; i >= top; --i) + ans.push_back(matrix[i][left]); + ++left; + } + + dir = (dir + 1) % 4; + } + + return ans; + } +}; \ No newline at end of file From a94cd345db832dde0ff16476f16ec88881074d09 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 19 Jul 2024 21:19:15 +0530 Subject: [PATCH 1281/1399] Attach NOTES - LeetHub From b76ab9c35a7e06def6774c7712f557fd86d50c7e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 19 Jul 2024 21:19:18 +0530 Subject: [PATCH 1282/1399] Time: 0 ms (100.00%), Space: 8.4 MB (9.65%) - LeetHub From 4e9b0210f45b50e7add58c61a28065f7a0f37de9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 19 Jul 2024 22:32:30 +0530 Subject: [PATCH 1283/1399] Create README - LeetHub --- 1380-lucky-numbers-in-a-matrix/README.md | 37 ++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 1380-lucky-numbers-in-a-matrix/README.md diff --git a/1380-lucky-numbers-in-a-matrix/README.md b/1380-lucky-numbers-in-a-matrix/README.md new file mode 100644 index 00000000..5cfd13f5 --- /dev/null +++ b/1380-lucky-numbers-in-a-matrix/README.md @@ -0,0 +1,37 @@ +

1380. Lucky Numbers in a Matrix

Easy


Given an m x n matrix of distinct numbers, return all lucky numbers in the matrix in any order.

+ +

A lucky number is an element of the matrix such that it is the minimum element in its row and maximum in its column.

+ +

 

+

Example 1:

+ +
Input: matrix = [[3,7,8],[9,11,13],[15,16,17]]
+Output: [15]
+Explanation: 15 is the only lucky number since it is the minimum in its row and the maximum in its column.
+
+ +

Example 2:

+ +
Input: matrix = [[1,10,4,2],[9,3,8,7],[15,16,17,12]]
+Output: [12]
+Explanation: 12 is the only lucky number since it is the minimum in its row and the maximum in its column.
+
+ +

Example 3:

+ +
Input: matrix = [[7,8],[1,2]]
+Output: [7]
+Explanation: 7 is the only lucky number since it is the minimum in its row and the maximum in its column.
+
+ +

 

+

Constraints:

+ +
    +
  • m == mat.length
  • +
  • n == mat[i].length
  • +
  • 1 <= n, m <= 50
  • +
  • 1 <= matrix[i][j] <= 105.
  • +
  • All elements in the matrix are distinct.
  • +
+
\ No newline at end of file From 05768037181f87a0a5bd9d731c3e57b6bbbb66ba Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 19 Jul 2024 22:32:33 +0530 Subject: [PATCH 1284/1399] Time: 12 ms (84.97%), Space: 14.5 MB (16.09%) - LeetHub --- .../1380-lucky-numbers-in-a-matrix.cpp | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 1380-lucky-numbers-in-a-matrix/1380-lucky-numbers-in-a-matrix.cpp diff --git a/1380-lucky-numbers-in-a-matrix/1380-lucky-numbers-in-a-matrix.cpp b/1380-lucky-numbers-in-a-matrix/1380-lucky-numbers-in-a-matrix.cpp new file mode 100644 index 00000000..3d76be27 --- /dev/null +++ b/1380-lucky-numbers-in-a-matrix/1380-lucky-numbers-in-a-matrix.cpp @@ -0,0 +1,30 @@ +class Solution { +public: + vector luckyNumbers (vector>& matrix) { + vector ans; + int r = matrix.size(); + int c = matrix[0].size(); + + vector rmin(r,INT_MAX); + vector cmax(c,INT_MIN); + + for(int i = 0; i Date: Sat, 20 Jul 2024 22:13:21 +0530 Subject: [PATCH 1285/1399] Attach NOTES - LeetHub --- 1605-find-valid-matrix-given-row-and-column-sums/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1605-find-valid-matrix-given-row-and-column-sums/NOTES.md diff --git a/1605-find-valid-matrix-given-row-and-column-sums/NOTES.md b/1605-find-valid-matrix-given-row-and-column-sums/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1605-find-valid-matrix-given-row-and-column-sums/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 6f10557408ac6752647169f71906b204117189a7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 20 Jul 2024 22:13:24 +0530 Subject: [PATCH 1286/1399] Time: 31 ms (88.26%), Space: 36.1 MB (11.51%) - LeetHub --- ...valid-matrix-given-row-and-column-sums.cpp | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 1605-find-valid-matrix-given-row-and-column-sums/1605-find-valid-matrix-given-row-and-column-sums.cpp diff --git a/1605-find-valid-matrix-given-row-and-column-sums/1605-find-valid-matrix-given-row-and-column-sums.cpp b/1605-find-valid-matrix-given-row-and-column-sums/1605-find-valid-matrix-given-row-and-column-sums.cpp new file mode 100644 index 00000000..cab3c516 --- /dev/null +++ b/1605-find-valid-matrix-given-row-and-column-sums/1605-find-valid-matrix-given-row-and-column-sums.cpp @@ -0,0 +1,23 @@ +class Solution { +public: + vector> restoreMatrix(vector& rowSum, vector& colSum) { + + int m = rowSum.size(); + int n = colSum.size(); + vector> ans(m, vector(n, 0)); + + for(int i = 0; i < m; i++) { + for(int j = 0; j < n; j++) { + if (rowSum[i] == 0 || colSum[j] == 0) { + ans[i][j] = 0; + } else { + ans[i][j] = min(rowSum[i], colSum[j]); + rowSum[i] -= ans[i][j]; + colSum[j] -= ans[i][j]; + } + } + } + + return ans; + } +}; \ No newline at end of file From ed7b6da749a0244a107f53007e0e17cfef917f3d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 21 Jul 2024 10:04:07 +0530 Subject: [PATCH 1287/1399] Create README - LeetHub From 7c178341ae280bcc2135d2b60f958b972dc12bf6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 21 Jul 2024 10:12:25 +0530 Subject: [PATCH 1288/1399] Create README - LeetHub --- 0088-merge-sorted-array/README.md | 46 +++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 0088-merge-sorted-array/README.md diff --git a/0088-merge-sorted-array/README.md b/0088-merge-sorted-array/README.md new file mode 100644 index 00000000..edd52f0d --- /dev/null +++ b/0088-merge-sorted-array/README.md @@ -0,0 +1,46 @@ +

88. Merge Sorted Array

Easy


You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and two integers m and n, representing the number of elements in nums1 and nums2 respectively.

+ +

Merge nums1 and nums2 into a single array sorted in non-decreasing order.

+ +

The final sorted array should not be returned by the function, but instead be stored inside the array nums1. To accommodate this, nums1 has a length of m + n, where the first m elements denote the elements that should be merged, and the last n elements are set to 0 and should be ignored. nums2 has a length of n.

+ +

 

+

Example 1:

+ +
Input: nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3
+Output: [1,2,2,3,5,6]
+Explanation: The arrays we are merging are [1,2,3] and [2,5,6].
+The result of the merge is [1,2,2,3,5,6] with the underlined elements coming from nums1.
+
+ +

Example 2:

+ +
Input: nums1 = [1], m = 1, nums2 = [], n = 0
+Output: [1]
+Explanation: The arrays we are merging are [1] and [].
+The result of the merge is [1].
+
+ +

Example 3:

+ +
Input: nums1 = [0], m = 0, nums2 = [1], n = 1
+Output: [1]
+Explanation: The arrays we are merging are [] and [1].
+The result of the merge is [1].
+Note that because m = 0, there are no elements in nums1. The 0 is only there to ensure the merge result can fit in nums1.
+
+ +

 

+

Constraints:

+ +
    +
  • nums1.length == m + n
  • +
  • nums2.length == n
  • +
  • 0 <= m, n <= 200
  • +
  • 1 <= m + n <= 200
  • +
  • -109 <= nums1[i], nums2[j] <= 109
  • +
+ +

 

+

Follow up: Can you come up with an algorithm that runs in O(m + n) time?

+
\ No newline at end of file From b19557d329652f75c696dc7e132d299795d27046 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 21 Jul 2024 10:12:28 +0530 Subject: [PATCH 1289/1399] Time: 5 ms (28.10%), Space: 11.4 MB (11.74%) - LeetHub --- .../0088-merge-sorted-array.cpp | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 0088-merge-sorted-array/0088-merge-sorted-array.cpp diff --git a/0088-merge-sorted-array/0088-merge-sorted-array.cpp b/0088-merge-sorted-array/0088-merge-sorted-array.cpp new file mode 100644 index 00000000..22bbedc5 --- /dev/null +++ b/0088-merge-sorted-array/0088-merge-sorted-array.cpp @@ -0,0 +1,24 @@ +class Solution { +public: + void merge(vector& nums1, int m, vector& nums2, int n) { + + vector ans; + int i = 0, j = 0; + + while(i < m and j < n) + { + if(nums1[i] < nums2[j]) + ans.push_back(nums1[i++]); + else + ans.push_back(nums2[j++]); + } + + while(i < m) + ans.push_back(nums1[i++]); + while(j < n) + ans.push_back(nums2[j++]); + + nums1 = ans; + + } +}; \ No newline at end of file From 49e76635d9af5d065f6e5412790cae0e1627a6e9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 21 Jul 2024 10:16:26 +0530 Subject: [PATCH 1290/1399] Attach NOTES - LeetHub --- 0088-merge-sorted-array/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0088-merge-sorted-array/NOTES.md diff --git a/0088-merge-sorted-array/NOTES.md b/0088-merge-sorted-array/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0088-merge-sorted-array/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From bae62f3e80439d875129b2775fb100b7fc2e5db1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 21 Jul 2024 10:16:28 +0530 Subject: [PATCH 1291/1399] Time: 2 ms (49.63%), Space: 11.1 MB (19.11%) - LeetHub --- .../0088-merge-sorted-array.cpp | 23 ++++++++----------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/0088-merge-sorted-array/0088-merge-sorted-array.cpp b/0088-merge-sorted-array/0088-merge-sorted-array.cpp index 22bbedc5..f5bd7797 100644 --- a/0088-merge-sorted-array/0088-merge-sorted-array.cpp +++ b/0088-merge-sorted-array/0088-merge-sorted-array.cpp @@ -1,24 +1,21 @@ class Solution { public: void merge(vector& nums1, int m, vector& nums2, int n) { + + int i = m-1, j = n-1, k = n+m-1; - vector ans; - int i = 0, j = 0; - - while(i < m and j < n) + while(i >= 0 and j >= 0) { - if(nums1[i] < nums2[j]) - ans.push_back(nums1[i++]); + if(nums1[i] > nums2[j]) + nums1[k--] = nums1[i--]; else - ans.push_back(nums2[j++]); + nums1[k--] = nums2[j--]; } - while(i < m) - ans.push_back(nums1[i++]); - while(j < n) - ans.push_back(nums2[j++]); - - nums1 = ans; + while(i >= 0) + nums1[k--] = nums1[i--]; + while(j >= 0) + nums1[k--] = nums2[j--]; } }; \ No newline at end of file From 6449d2c81c4ad3c3f9e41c21652a3d5c6dd829f9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 21 Jul 2024 10:45:02 +0530 Subject: [PATCH 1292/1399] Attach NOTES - LeetHub From 3bf5d938419dded5d5d0703df2579abe2010adae Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 21 Jul 2024 10:45:06 +0530 Subject: [PATCH 1293/1399] Time: 11 ms (93.77%), Space: 27 MB (42.59%) - LeetHub --- 0169-majority-element/0169-majority-element.cpp | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/0169-majority-element/0169-majority-element.cpp b/0169-majority-element/0169-majority-element.cpp index 9b85e7db..b2296cda 100644 --- a/0169-majority-element/0169-majority-element.cpp +++ b/0169-majority-element/0169-majority-element.cpp @@ -2,21 +2,22 @@ class Solution { public: int majorityElement(vector& nums) { - int cnt = 0, num = -1; + int cnt = 1, num1 = nums[0], n = nums.size(); - for(auto& ele : nums) + for(int i = 1; i < n; ++i) { + if(num1 != nums[i]) + --cnt; + else + ++cnt; + if(cnt == 0) { - num = ele; cnt = 1; + num1 = nums[i]; } - else if(ele == num) - ++cnt; - else - --cnt; } - return num; + return num1; } }; \ No newline at end of file From 899b4b2f60fb83388a7cfa2da1e56387ba4431df Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 21 Jul 2024 11:33:03 +0530 Subject: [PATCH 1294/1399] Attach NOTES - LeetHub --- 0493-reverse-pairs/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0493-reverse-pairs/NOTES.md diff --git a/0493-reverse-pairs/NOTES.md b/0493-reverse-pairs/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0493-reverse-pairs/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From add44adf338212f5e3144ebb0d095a79e97b94f1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 21 Jul 2024 21:41:37 +0530 Subject: [PATCH 1295/1399] Create README - LeetHub --- 0289-game-of-life/README.md | 44 +++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 0289-game-of-life/README.md diff --git a/0289-game-of-life/README.md b/0289-game-of-life/README.md new file mode 100644 index 00000000..4d231fac --- /dev/null +++ b/0289-game-of-life/README.md @@ -0,0 +1,44 @@ +

289. Game of Life

Medium


According to Wikipedia's article: "The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970."

+ +

The board is made up of an m x n grid of cells, where each cell has an initial state: live (represented by a 1) or dead (represented by a 0). Each cell interacts with its eight neighbors (horizontal, vertical, diagonal) using the following four rules (taken from the above Wikipedia article):

+ +
    +
  1. Any live cell with fewer than two live neighbors dies as if caused by under-population.
  2. +
  3. Any live cell with two or three live neighbors lives on to the next generation.
  4. +
  5. Any live cell with more than three live neighbors dies, as if by over-population.
  6. +
  7. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.
  8. +
+ +

The next state is created by applying the above rules simultaneously to every cell in the current state, where births and deaths occur simultaneously. Given the current state of the m x n grid board, return the next state.

+ +

 

+

Example 1:

+ +
Input: board = [[0,1,0],[0,0,1],[1,1,1],[0,0,0]]
+Output: [[0,0,0],[1,0,1],[0,1,1],[0,1,0]]
+
+ +

Example 2:

+ +
Input: board = [[1,1],[1,0]]
+Output: [[1,1],[1,1]]
+
+ +

 

+

Constraints:

+ +
    +
  • m == board.length
  • +
  • n == board[i].length
  • +
  • 1 <= m, n <= 25
  • +
  • board[i][j] is 0 or 1.
  • +
+ +

 

+

Follow up:

+ +
    +
  • Could you solve it in-place? Remember that the board needs to be updated simultaneously: You cannot update some cells first and then use their updated values to update other cells.
  • +
  • In this question, we represent the board using a 2D array. In principle, the board is infinite, which would cause problems when the active area encroaches upon the border of the array (i.e., live cells reach the border). How would you address these problems?
  • +
+
\ No newline at end of file From d7601eca087cfed84735eb31922f23b62bfc4abd Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 21 Jul 2024 21:41:40 +0530 Subject: [PATCH 1296/1399] Time: 0 ms (100.00%), Space: 8.7 MB (8.42%) - LeetHub --- 0289-game-of-life/0289-game-of-life.cpp | 58 +++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 0289-game-of-life/0289-game-of-life.cpp diff --git a/0289-game-of-life/0289-game-of-life.cpp b/0289-game-of-life/0289-game-of-life.cpp new file mode 100644 index 00000000..bef875c7 --- /dev/null +++ b/0289-game-of-life/0289-game-of-life.cpp @@ -0,0 +1,58 @@ +class Solution { +public: + void gameOfLife(vector>& board) { + + vector dx = {-1, -1, -1, 0, 0, +1, +1, +1}; + vector dy = {-1, 0, +1, -1, +1, -1, 0, +1}; + + int n = board.size(); + int m = board[0].size(); + + set> st; + + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < m; ++j) + { + if(board[i][j]) + st.insert({i, j}); + } + } + + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < m; ++j) + { + int alive = 0; + int dead = 0; + + for(int k = 0; k < 8; ++k) + { + int newx = i + dx[k]; + int newy = j + dy[k]; + + if(newx >= 0 and newy >= 0 and newx < n and newy < m) + { + // cout< 3) + board[i][j] = 0; + } + else + { + // cout< Date: Sun, 21 Jul 2024 21:42:56 +0530 Subject: [PATCH 1297/1399] Attach NOTES - LeetHub --- 0289-game-of-life/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0289-game-of-life/NOTES.md diff --git a/0289-game-of-life/NOTES.md b/0289-game-of-life/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0289-game-of-life/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 1b1a4d0392448a894bc80d8c3b82451078678dab Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 21 Jul 2024 21:42:59 +0530 Subject: [PATCH 1298/1399] Time: 4 ms (20.95%), Space: 8.6 MB (8.42%) - LeetHub --- 0289-game-of-life/0289-game-of-life.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/0289-game-of-life/0289-game-of-life.cpp b/0289-game-of-life/0289-game-of-life.cpp index bef875c7..eec57c7c 100644 --- a/0289-game-of-life/0289-game-of-life.cpp +++ b/0289-game-of-life/0289-game-of-life.cpp @@ -33,7 +33,6 @@ class Solution { if(newx >= 0 and newy >= 0 and newx < n and newy < m) { - // cout< Date: Mon, 22 Jul 2024 01:04:49 +0530 Subject: [PATCH 1299/1399] Create README - LeetHub --- 2392-build-a-matrix-with-conditions/README.md | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 2392-build-a-matrix-with-conditions/README.md diff --git a/2392-build-a-matrix-with-conditions/README.md b/2392-build-a-matrix-with-conditions/README.md new file mode 100644 index 00000000..83a8ca6c --- /dev/null +++ b/2392-build-a-matrix-with-conditions/README.md @@ -0,0 +1,55 @@ +

2392. Build a Matrix With Conditions

Hard


You are given a positive integer k. You are also given:

+ +
    +
  • a 2D integer array rowConditions of size n where rowConditions[i] = [abovei, belowi], and
  • +
  • a 2D integer array colConditions of size m where colConditions[i] = [lefti, righti].
  • +
+ +

The two arrays contain integers from 1 to k.

+ +

You have to build a k x k matrix that contains each of the numbers from 1 to k exactly once. The remaining cells should have the value 0.

+ +

The matrix should also satisfy the following conditions:

+ +
    +
  • The number abovei should appear in a row that is strictly above the row at which the number belowi appears for all i from 0 to n - 1.
  • +
  • The number lefti should appear in a column that is strictly left of the column at which the number righti appears for all i from 0 to m - 1.
  • +
+ +

Return any matrix that satisfies the conditions. If no answer exists, return an empty matrix.

+ +

 

+

Example 1:

+ +
Input: k = 3, rowConditions = [[1,2],[3,2]], colConditions = [[2,1],[3,2]]
+Output: [[3,0,0],[0,0,1],[0,2,0]]
+Explanation: The diagram above shows a valid example of a matrix that satisfies all the conditions.
+The row conditions are the following:
+- Number 1 is in row 1, and number 2 is in row 2, so 1 is above 2 in the matrix.
+- Number 3 is in row 0, and number 2 is in row 2, so 3 is above 2 in the matrix.
+The column conditions are the following:
+- Number 2 is in column 1, and number 1 is in column 2, so 2 is left of 1 in the matrix.
+- Number 3 is in column 0, and number 2 is in column 1, so 3 is left of 2 in the matrix.
+Note that there may be multiple correct answers.
+
+ +

Example 2:

+ +
Input: k = 3, rowConditions = [[1,2],[2,3],[3,1],[2,3]], colConditions = [[2,1]]
+Output: []
+Explanation: From the first two conditions, 3 has to be below 1 but the third conditions needs 3 to be above 1 to be satisfied.
+No matrix can satisfy all the conditions, so we return the empty matrix.
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= k <= 400
  • +
  • 1 <= rowConditions.length, colConditions.length <= 104
  • +
  • rowConditions[i].length == colConditions[i].length == 2
  • +
  • 1 <= abovei, belowi, lefti, righti <= k
  • +
  • abovei != belowi
  • +
  • lefti != righti
  • +
+
\ No newline at end of file From 1a924181ed95deb66a5ac282905d72f8eb79a0df Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 22 Jul 2024 01:04:53 +0530 Subject: [PATCH 1300/1399] Time: 108 ms (44.44%), Space: 65.2 MB (34.65%) - LeetHub --- .../2392-build-a-matrix-with-conditions.cpp | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 2392-build-a-matrix-with-conditions/2392-build-a-matrix-with-conditions.cpp diff --git a/2392-build-a-matrix-with-conditions/2392-build-a-matrix-with-conditions.cpp b/2392-build-a-matrix-with-conditions/2392-build-a-matrix-with-conditions.cpp new file mode 100644 index 00000000..356083ec --- /dev/null +++ b/2392-build-a-matrix-with-conditions/2392-build-a-matrix-with-conditions.cpp @@ -0,0 +1,65 @@ +class Solution { +public: + vector findOrder(int k, vector>& dependencies) { + + vector> adj(k + 1); + vector indegree(k + 1); + for(auto dependency: dependencies) { + adj[dependency[0]].push_back(dependency[1]); + indegree[dependency[1]]++; + } + + queue q; + for(int i = 1; i <= k; i++) { + if(indegree[i] == 0) { + q.push(i); + } + } + + int cnt = 0; + vector order; + while(!q.empty()) { + + int cur = q.front(); + q.pop(); + + cnt++; + order.push_back(cur); + + for(int nbr: adj[cur]) { + indegree[nbr]--; + + if(indegree[nbr] == 0) { + q.push(nbr); + } + } + } + + if(cnt == k) return order; + return {}; + } + + vector> buildMatrix(int k, vector>& rowConditions, vector>& colConditions) { + vector rowArray = findOrder(k, rowConditions); + vector colArray = findOrder(k, colConditions); + + + if(rowArray.size() == 0 || colArray.size() == 0) { + return {}; + } + + vector> ind(k); + + for(int i = 0; i < k; i++) { + ind[rowArray[i] - 1].first = i; + ind[colArray[i] - 1].second = i; + } + + vector> result(k, vector(k, 0)); + + for(int i = 0; i < k; i++) { + result[ind[i].first][ind[i].second] = i + 1; + } + return result; + } +}; \ No newline at end of file From 29dd485e1cf61264e773af0e86c664d066598776 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 22 Jul 2024 20:51:15 +0530 Subject: [PATCH 1301/1399] Create README - LeetHub --- 2418-sort-the-people/README.md | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 2418-sort-the-people/README.md diff --git a/2418-sort-the-people/README.md b/2418-sort-the-people/README.md new file mode 100644 index 00000000..2f291942 --- /dev/null +++ b/2418-sort-the-people/README.md @@ -0,0 +1,33 @@ +

2418. Sort the People

Easy


You are given an array of strings names, and an array heights that consists of distinct positive integers. Both arrays are of length n.

+ +

For each index i, names[i] and heights[i] denote the name and height of the ith person.

+ +

Return names sorted in descending order by the people's heights.

+ +

 

+

Example 1:

+ +
Input: names = ["Mary","John","Emma"], heights = [180,165,170]
+Output: ["Mary","Emma","John"]
+Explanation: Mary is the tallest, followed by Emma and John.
+
+ +

Example 2:

+ +
Input: names = ["Alice","Bob","Bob"], heights = [155,185,150]
+Output: ["Bob","Alice","Bob"]
+Explanation: The first Bob is the tallest, followed by Alice and the second Bob.
+
+ +

 

+

Constraints:

+ +
    +
  • n == names.length == heights.length
  • +
  • 1 <= n <= 103
  • +
  • 1 <= names[i].length <= 20
  • +
  • 1 <= heights[i] <= 105
  • +
  • names[i] consists of lower and upper case English letters.
  • +
  • All the values of heights are distinct.
  • +
+
\ No newline at end of file From f2c81c48f5373f14b60473b97ddeced1a97d6179 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 22 Jul 2024 20:51:16 +0530 Subject: [PATCH 1302/1399] Attach NOTES - LeetHub --- 2418-sort-the-people/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2418-sort-the-people/NOTES.md diff --git a/2418-sort-the-people/NOTES.md b/2418-sort-the-people/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2418-sort-the-people/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 14ae23618e460efe135013b94445bf5a17c3ba71 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 22 Jul 2024 20:51:20 +0530 Subject: [PATCH 1303/1399] Time: 31 ms (50.96%), Space: 27.9 MB (24.89%) - LeetHub --- 2418-sort-the-people/2418-sort-the-people.cpp | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 2418-sort-the-people/2418-sort-the-people.cpp diff --git a/2418-sort-the-people/2418-sort-the-people.cpp b/2418-sort-the-people/2418-sort-the-people.cpp new file mode 100644 index 00000000..eb04256d --- /dev/null +++ b/2418-sort-the-people/2418-sort-the-people.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + vector sortPeople(vector& names, vector& heights) { + + vector> here; + + for(int i = 0; i < heights.size(); ++i) + here.push_back({heights[i], i}); + + sort(here.rbegin(), here.rend()); + + vector ans; + + for(int i = 0; i < names.size(); ++i) + ans.push_back(names[here[i].second]); + + return ans; + + } +}; \ No newline at end of file From 8b8a2513acccd6cea7e7ae095152b5b6ecc172dc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 23 Jul 2024 19:25:01 +0530 Subject: [PATCH 1304/1399] Create README - LeetHub --- .../README.md | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 1636-sort-array-by-increasing-frequency/README.md diff --git a/1636-sort-array-by-increasing-frequency/README.md b/1636-sort-array-by-increasing-frequency/README.md new file mode 100644 index 00000000..cfe5e97a --- /dev/null +++ b/1636-sort-array-by-increasing-frequency/README.md @@ -0,0 +1,32 @@ +

1636. Sort Array by Increasing Frequency

Easy


Given an array of integers nums, sort the array in increasing order based on the frequency of the values. If multiple values have the same frequency, sort them in decreasing order.

+ +

Return the sorted array.

+ +

 

+

Example 1:

+ +
Input: nums = [1,1,2,2,2,3]
+Output: [3,1,1,2,2,2]
+Explanation: '3' has a frequency of 1, '1' has a frequency of 2, and '2' has a frequency of 3.
+
+ +

Example 2:

+ +
Input: nums = [2,3,1,3,2]
+Output: [1,3,3,2,2]
+Explanation: '2' and '3' both have a frequency of 2, so they are sorted in decreasing order.
+
+ +

Example 3:

+ +
Input: nums = [-1,1,-6,4,5,-6,1,4,1]
+Output: [5,-1,4,4,-6,-6,1,1,1]
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 100
  • +
  • -100 <= nums[i] <= 100
  • +
+
\ No newline at end of file From de73e81bb69d837470a1cffa8fb47c6b22873153 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 23 Jul 2024 22:24:11 +0530 Subject: [PATCH 1305/1399] Create README - LeetHub --- .../README.md | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 3224-minimum-array-changes-to-make-differences-equal/README.md diff --git a/3224-minimum-array-changes-to-make-differences-equal/README.md b/3224-minimum-array-changes-to-make-differences-equal/README.md new file mode 100644 index 00000000..a40cf925 --- /dev/null +++ b/3224-minimum-array-changes-to-make-differences-equal/README.md @@ -0,0 +1,58 @@ +

3224. Minimum Array Changes to Make Differences Equal

Medium


You are given an integer array nums of size n where n is even, and an integer k.

+ +

You can perform some changes on the array, where in one change you can replace any element in the array with any integer in the range from 0 to k.

+ +

You need to perform some changes (possibly none) such that the final array satisfies the following condition:

+ +
    +
  • There exists an integer X such that abs(a[i] - a[n - i - 1]) = X for all (0 <= i < n).
  • +
+ +

Return the minimum number of changes required to satisfy the above condition.

+ +

 

+

Example 1:

+ +
+

Input: nums = [1,0,1,2,4,3], k = 4

+ +

Output: 2

+ +

Explanation:
+We can perform the following changes:

+ +
    +
  • Replace nums[1] by 2. The resulting array is nums = [1,2,1,2,4,3].
  • +
  • Replace nums[3] by 3. The resulting array is nums = [1,2,1,3,4,3].
  • +
+ +

The integer X will be 2.

+
+ +

Example 2:

+ +
+

Input: nums = [0,1,2,3,3,6,5,4], k = 6

+ +

Output: 2

+ +

Explanation:
+We can perform the following operations:

+ +
    +
  • Replace nums[3] by 0. The resulting array is nums = [0,1,2,0,3,6,5,4].
  • +
  • Replace nums[4] by 4. The resulting array is nums = [0,1,2,0,4,6,5,4].
  • +
+ +

The integer X will be 4.

+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= n == nums.length <= 105
  • +
  • n is even.
  • +
  • 0 <= nums[i] <= k <= 105
  • +
+
\ No newline at end of file From fd92e8e913a3aa87bcf359f4fb0748b42bbc4f87 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 23 Jul 2024 22:24:15 +0530 Subject: [PATCH 1306/1399] Time: 394 ms (12.47%), Space: 136.4 MB (15.71%) - LeetHub --- ...rray-changes-to-make-differences-equal.cpp | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 3224-minimum-array-changes-to-make-differences-equal/3224-minimum-array-changes-to-make-differences-equal.cpp diff --git a/3224-minimum-array-changes-to-make-differences-equal/3224-minimum-array-changes-to-make-differences-equal.cpp b/3224-minimum-array-changes-to-make-differences-equal/3224-minimum-array-changes-to-make-differences-equal.cpp new file mode 100644 index 00000000..edd38009 --- /dev/null +++ b/3224-minimum-array-changes-to-make-differences-equal/3224-minimum-array-changes-to-make-differences-equal.cpp @@ -0,0 +1,29 @@ +class Solution { +public: + int minChanges(vector& nums, int k) { + + int n = nums.size(); + + map mp; + + for(int i = 0; i < n/2; ++i) + { + int diff = abs(nums[i] - nums[n-i-1]); + int maxDiff = max(k - min(nums[i], nums[n-i-1]), max(nums[i], nums[n-i-1])); + ++mp[0]; + --mp[diff]; + ++mp[diff+1]; + ++mp[maxDiff + 1]; + } + + int ans = INT_MAX, sum = 0; + + for(auto&[f, e] : mp) + { + sum += e; + ans = min(ans, sum); + } + + return ans; + } +}; \ No newline at end of file From 8c249b24fc6d0876fce5a51333fe799d263828f3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 25 Jul 2024 00:41:08 +0530 Subject: [PATCH 1307/1399] Create README - LeetHub --- 2191-sort-the-jumbled-numbers/README.md | 47 +++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 2191-sort-the-jumbled-numbers/README.md diff --git a/2191-sort-the-jumbled-numbers/README.md b/2191-sort-the-jumbled-numbers/README.md new file mode 100644 index 00000000..c230b1a8 --- /dev/null +++ b/2191-sort-the-jumbled-numbers/README.md @@ -0,0 +1,47 @@ +

2191. Sort the Jumbled Numbers

Medium


You are given a 0-indexed integer array mapping which represents the mapping rule of a shuffled decimal system. mapping[i] = j means digit i should be mapped to digit j in this system.

+ +

The mapped value of an integer is the new integer obtained by replacing each occurrence of digit i in the integer with mapping[i] for all 0 <= i <= 9.

+ +

You are also given another integer array nums. Return the array nums sorted in non-decreasing order based on the mapped values of its elements.

+ +

Notes:

+ +
    +
  • Elements with the same mapped values should appear in the same relative order as in the input.
  • +
  • The elements of nums should only be sorted based on their mapped values and not be replaced by them.
  • +
+ +

 

+

Example 1:

+ +
Input: mapping = [8,9,4,0,2,1,3,5,7,6], nums = [991,338,38]
+Output: [338,38,991]
+Explanation: 
+Map the number 991 as follows:
+1. mapping[9] = 6, so all occurrences of the digit 9 will become 6.
+2. mapping[1] = 9, so all occurrences of the digit 1 will become 9.
+Therefore, the mapped value of 991 is 669.
+338 maps to 007, or 7 after removing the leading zeros.
+38 maps to 07, which is also 7 after removing leading zeros.
+Since 338 and 38 share the same mapped value, they should remain in the same relative order, so 338 comes before 38.
+Thus, the sorted array is [338,38,991].
+
+ +

Example 2:

+ +
Input: mapping = [0,1,2,3,4,5,6,7,8,9], nums = [789,456,123]
+Output: [123,456,789]
+Explanation: 789 maps to 789, 456 maps to 456, and 123 maps to 123. Thus, the sorted array is [123,456,789].
+
+ +

 

+

Constraints:

+ +
    +
  • mapping.length == 10
  • +
  • 0 <= mapping[i] <= 9
  • +
  • All the values of mapping[i] are unique.
  • +
  • 1 <= nums.length <= 3 * 104
  • +
  • 0 <= nums[i] < 109
  • +
+
\ No newline at end of file From 1807a8aa06a39791dd0e0b7998ace64f65b9aba2 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 25 Jul 2024 00:41:11 +0530 Subject: [PATCH 1308/1399] Time: 296 ms (57.58%), Space: 88.8 MB (55.84%) - LeetHub --- .../2191-sort-the-jumbled-numbers.cpp | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 2191-sort-the-jumbled-numbers/2191-sort-the-jumbled-numbers.cpp diff --git a/2191-sort-the-jumbled-numbers/2191-sort-the-jumbled-numbers.cpp b/2191-sort-the-jumbled-numbers/2191-sort-the-jumbled-numbers.cpp new file mode 100644 index 00000000..1cc4d93d --- /dev/null +++ b/2191-sort-the-jumbled-numbers/2191-sort-the-jumbled-numbers.cpp @@ -0,0 +1,32 @@ +class Solution { +public: + vector sortJumbled(vector& mapping, vector& nums) { + + map mp; + + for(int i = 0; i < mapping.size(); ++i) + mp[i+'0'] = mapping[i]+'0'; + + vector> vp; + + for(int i = 0; i < nums.size(); ++i) + { + string str = to_string(nums[i]); + for(int i = 0; i < str.size(); ++i) + str[i] = mp[str[i]]; + vp.push_back({stoi(str), i}); + } + + sort(vp.begin(), vp.end()); + + vector ans; + + for(auto&[f,e] : vp) + { + ans.push_back(nums[e]); + } + + return ans; + + } +}; \ No newline at end of file From 1c70444081baaad8f421340e85a846538b8a0e43 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 25 Jul 2024 21:10:37 +0530 Subject: [PATCH 1309/1399] Create README - LeetHub From 177b8e7157e57d0897c5077883d95d5d4fa1809d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 25 Jul 2024 21:54:49 +0530 Subject: [PATCH 1310/1399] Create README - LeetHub From 3d8391919d48f0f4d14c2beaa4cb0fec7e304301 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 26 Jul 2024 23:17:05 +0530 Subject: [PATCH 1311/1399] Attach NOTES - LeetHub --- .../NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1334-find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance/NOTES.md diff --git a/1334-find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance/NOTES.md b/1334-find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1334-find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 0f827f90e6309af38b20d647558188b3961258d5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 27 Jul 2024 22:48:41 +0530 Subject: [PATCH 1312/1399] Create README - LeetHub --- .../README.md | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 2976-minimum-cost-to-convert-string-i/README.md diff --git a/2976-minimum-cost-to-convert-string-i/README.md b/2976-minimum-cost-to-convert-string-i/README.md new file mode 100644 index 00000000..cc8d73c0 --- /dev/null +++ b/2976-minimum-cost-to-convert-string-i/README.md @@ -0,0 +1,48 @@ +

2976. Minimum Cost to Convert String I

Medium


You are given two 0-indexed strings source and target, both of length n and consisting of lowercase English letters. You are also given two 0-indexed character arrays original and changed, and an integer array cost, where cost[i] represents the cost of changing the character original[i] to the character changed[i].

+ +

You start with the string source. In one operation, you can pick a character x from the string and change it to the character y at a cost of z if there exists any index j such that cost[j] == z, original[j] == x, and changed[j] == y.

+ +

Return the minimum cost to convert the string source to the string target using any number of operations. If it is impossible to convert source to target, return -1.

+ +

Note that there may exist indices i, j such that original[j] == original[i] and changed[j] == changed[i].

+ +

 

+

Example 1:

+ +
Input: source = "abcd", target = "acbe", original = ["a","b","c","c","e","d"], changed = ["b","c","b","e","b","e"], cost = [2,5,5,1,2,20]
+Output: 28
+Explanation: To convert the string "abcd" to string "acbe":
+- Change value at index 1 from 'b' to 'c' at a cost of 5.
+- Change value at index 2 from 'c' to 'e' at a cost of 1.
+- Change value at index 2 from 'e' to 'b' at a cost of 2.
+- Change value at index 3 from 'd' to 'e' at a cost of 20.
+The total cost incurred is 5 + 1 + 2 + 20 = 28.
+It can be shown that this is the minimum possible cost.
+
+ +

Example 2:

+ +
Input: source = "aaaa", target = "bbbb", original = ["a","c"], changed = ["c","b"], cost = [1,2]
+Output: 12
+Explanation: To change the character 'a' to 'b' change the character 'a' to 'c' at a cost of 1, followed by changing the character 'c' to 'b' at a cost of 2, for a total cost of 1 + 2 = 3. To change all occurrences of 'a' to 'b', a total cost of 3 * 4 = 12 is incurred.
+
+ +

Example 3:

+ +
Input: source = "abcd", target = "abce", original = ["a"], changed = ["e"], cost = [10000]
+Output: -1
+Explanation: It is impossible to convert source to target because the value at index 3 cannot be changed from 'd' to 'e'.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= source.length == target.length <= 105
  • +
  • source, target consist of lowercase English letters.
  • +
  • 1 <= cost.length == original.length == changed.length <= 2000
  • +
  • original[i], changed[i] are lowercase English letters.
  • +
  • 1 <= cost[i] <= 106
  • +
  • original[i] != changed[i]
  • +
+
\ No newline at end of file From 205e5de390c2e7d6c9419f4d97abbcfe51b96493 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 27 Jul 2024 22:48:44 +0530 Subject: [PATCH 1313/1399] Time: 168 ms (70.36%), Space: 99.1 MB (47.68%) - LeetHub --- .../2976-minimum-cost-to-convert-string-i.cpp | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 2976-minimum-cost-to-convert-string-i/2976-minimum-cost-to-convert-string-i.cpp diff --git a/2976-minimum-cost-to-convert-string-i/2976-minimum-cost-to-convert-string-i.cpp b/2976-minimum-cost-to-convert-string-i/2976-minimum-cost-to-convert-string-i.cpp new file mode 100644 index 00000000..31ba790f --- /dev/null +++ b/2976-minimum-cost-to-convert-string-i/2976-minimum-cost-to-convert-string-i.cpp @@ -0,0 +1,41 @@ +using ll = long long; + +class Solution { +public: + long long minimumCost(string source, string target, vector& original, vector& changed, vector& cost) { + + vector> mat(26, vector(26, INT_MAX)); + + for(int i = 0; i < original.size(); ++i) + { + int a = original[i] - 'a'; + int b = changed[i] - 'a'; + mat[a][b] = min(mat[a][b], (ll)cost[i]); + } + + for(int k = 0; k < 26; ++k) + { + for(int i = 0; i < 26; ++i) + { + for(int j = 0; j < 26; ++j) + { + mat[i][j] = min(mat[i][j], mat[i][k] + mat[k][j]); + } + } + } + + ll ans = 0; + + for(int i = 0; i < source.size(); ++i) + { + if(source[i] != target[i]) + { + ll val = mat[source[i]-'a'][target[i]-'a']; + if(val >= INT_MAX) return -1; + ans += val; + } + } + + return ans; + } +}; \ No newline at end of file From 55834ef31833804abc038a7bda0c0c2739e338a1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 28 Jul 2024 23:38:52 +0530 Subject: [PATCH 1314/1399] Attach NOTES - LeetHub --- 2045-second-minimum-time-to-reach-destination/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2045-second-minimum-time-to-reach-destination/NOTES.md diff --git a/2045-second-minimum-time-to-reach-destination/NOTES.md b/2045-second-minimum-time-to-reach-destination/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2045-second-minimum-time-to-reach-destination/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 38a222db6e32690360f02f8739bd242262a19e79 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 28 Jul 2024 23:38:56 +0530 Subject: [PATCH 1315/1399] Time: 666 ms (8.94%), Space: 223.2 MB (12.16%) - LeetHub --- ...cond-minimum-time-to-reach-destination.cpp | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 2045-second-minimum-time-to-reach-destination/2045-second-minimum-time-to-reach-destination.cpp diff --git a/2045-second-minimum-time-to-reach-destination/2045-second-minimum-time-to-reach-destination.cpp b/2045-second-minimum-time-to-reach-destination/2045-second-minimum-time-to-reach-destination.cpp new file mode 100644 index 00000000..37b28c23 --- /dev/null +++ b/2045-second-minimum-time-to-reach-destination/2045-second-minimum-time-to-reach-destination.cpp @@ -0,0 +1,56 @@ +class Solution { +public: + int secondMinimum(int n, vector>& edges, int time, int change) { + + vector> dis (n); + for (int i=0;i adj[n]; + for (auto it:edges){ + adj[it[0]-1].push_back(it[1]-1); + adj[it[1]-1].push_back(it[0]-1); + } + + priority_queue , vector>, greater>> pq; + pq.push({0,0}); // time=0, node=0 + dis[0].pop(); + dis[0].push(0); + + while (pq.empty()==false){ + int dist=pq.top().first; + int node=pq.top().second; + pq.pop(); + int k=dist/change; + if (k%2==1) { + dist=dist+(change-dist%change); + } + + for (auto adjnode:adj[node]){ + int adjdis=dist+time; + + if (dis[adjnode].top()==1e8){ + dis[adjnode].pop(); + dis[adjnode].push(adjdis); + pq.push({adjdis,adjnode}); + } + else if (dis[adjnode].size()<2 && dis[adjnode].top()!= adjdis){ + dis[adjnode].push(adjdis); + pq.push({adjdis,adjnode}); + } + else { + if (dis[adjnode].top() > adjdis){ + dis[adjnode].pop(); + dis[adjnode].push(adjdis); + pq.push({adjdis,adjnode}); + } + } + } + } + + return dis[n-1].top(); + } +}; \ No newline at end of file From c0b66b698dab6f68a95750d57c33191d008ebbad Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 29 Jul 2024 22:27:38 +0530 Subject: [PATCH 1316/1399] Create README - LeetHub --- 1395-count-number-of-teams/README.md | 42 ++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 1395-count-number-of-teams/README.md diff --git a/1395-count-number-of-teams/README.md b/1395-count-number-of-teams/README.md new file mode 100644 index 00000000..e0ebb7f2 --- /dev/null +++ b/1395-count-number-of-teams/README.md @@ -0,0 +1,42 @@ +

1395. Count Number of Teams

Medium


There are n soldiers standing in a line. Each soldier is assigned a unique rating value.

+ +

You have to form a team of 3 soldiers amongst them under the following rules:

+ +
    +
  • Choose 3 soldiers with index (i, j, k) with rating (rating[i], rating[j], rating[k]).
  • +
  • A team is valid if: (rating[i] < rating[j] < rating[k]) or (rating[i] > rating[j] > rating[k]) where (0 <= i < j < k < n).
  • +
+ +

Return the number of teams you can form given the conditions. (soldiers can be part of multiple teams).

+ +

 

+

Example 1:

+ +
Input: rating = [2,5,3,4,1]
+Output: 3
+Explanation: We can form three teams given the conditions. (2,3,4), (5,4,1), (5,3,1). 
+
+ +

Example 2:

+ +
Input: rating = [2,1,3]
+Output: 0
+Explanation: We can't form any team given the conditions.
+
+ +

Example 3:

+ +
Input: rating = [1,2,3,4]
+Output: 4
+
+ +

 

+

Constraints:

+ +
    +
  • n == rating.length
  • +
  • 3 <= n <= 1000
  • +
  • 1 <= rating[i] <= 105
  • +
  • All the integers in rating are unique.
  • +
+
\ No newline at end of file From 5a1cfca04ce1eb768bec9f3e2a2508a21448a688 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 31 Jul 2024 00:07:53 +0530 Subject: [PATCH 1317/1399] Create README - LeetHub --- .../README.md | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 1653-minimum-deletions-to-make-string-balanced/README.md diff --git a/1653-minimum-deletions-to-make-string-balanced/README.md b/1653-minimum-deletions-to-make-string-balanced/README.md new file mode 100644 index 00000000..8dc82bd2 --- /dev/null +++ b/1653-minimum-deletions-to-make-string-balanced/README.md @@ -0,0 +1,31 @@ +

1653. Minimum Deletions to Make String Balanced

Medium


You are given a string s consisting only of characters 'a' and 'b'​​​​.

+ +

You can delete any number of characters in s to make s balanced. s is balanced if there is no pair of indices (i,j) such that i < j and s[i] = 'b' and s[j]= 'a'.

+ +

Return the minimum number of deletions needed to make s balanced.

+ +

 

+

Example 1:

+ +
Input: s = "aababbab"
+Output: 2
+Explanation: You can either:
+Delete the characters at 0-indexed positions 2 and 6 ("aababbab" -> "aaabbb"), or
+Delete the characters at 0-indexed positions 3 and 6 ("aababbab" -> "aabbbb").
+
+ +

Example 2:

+ +
Input: s = "bbaaaaabb"
+Output: 2
+Explanation: The only solution is to delete the first two characters.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 105
  • +
  • s[i] is 'a' or 'b'​​.
  • +
+
\ No newline at end of file From a8fb4e24c344398d2317254d73def9766425c5c6 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 1 Aug 2024 00:11:25 +0530 Subject: [PATCH 1318/1399] Attach NOTES - LeetHub --- 1105-filling-bookcase-shelves/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1105-filling-bookcase-shelves/NOTES.md diff --git a/1105-filling-bookcase-shelves/NOTES.md b/1105-filling-bookcase-shelves/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1105-filling-bookcase-shelves/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 98be600bac267bbe3409942435f26fd16bafe0c0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 1 Aug 2024 00:11:28 +0530 Subject: [PATCH 1319/1399] Time: 3 ms (87.31%), Space: 31.5 MB (5.37%) - LeetHub --- .../1105-filling-bookcase-shelves.cpp | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 1105-filling-bookcase-shelves/1105-filling-bookcase-shelves.cpp diff --git a/1105-filling-bookcase-shelves/1105-filling-bookcase-shelves.cpp b/1105-filling-bookcase-shelves/1105-filling-bookcase-shelves.cpp new file mode 100644 index 00000000..1c7ef1c7 --- /dev/null +++ b/1105-filling-bookcase-shelves/1105-filling-bookcase-shelves.cpp @@ -0,0 +1,33 @@ +class Solution { +private: + int solve(vector> &books, int i, int remShelfWidth, int maxShelfHeight, + int shelfWidth, vector> &dp) { + + if(i == books.size()) { + return maxShelfHeight; + } + + if(dp[i][remShelfWidth] != -1) + return dp[i][remShelfWidth]; + + int currShelf = INT_MAX, nextShelf = INT_MAX; + + int bookWidth = books[i][0]; + int bookHeight = books[i][1]; + + if(bookWidth <= remShelfWidth) { + currShelf = solve(books, i + 1, remShelfWidth - bookWidth, + max(maxShelfHeight, bookHeight), shelfWidth, dp); + } + + nextShelf = maxShelfHeight + solve(books, i + 1, shelfWidth - bookWidth, + bookHeight, shelfWidth, dp); + + return dp[i][remShelfWidth] = min(currShelf, nextShelf); + } +public: + int minHeightShelves(vector>& books, int shelfWidth) { + vector> dp(1001, vector (shelfWidth + 1, -1)); + return solve(books, 0, shelfWidth, 0, shelfWidth, dp); + } +}; \ No newline at end of file From 57210c0f0c4b3216c7e5fca81737750c3debbc93 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 1 Aug 2024 20:19:51 +0530 Subject: [PATCH 1320/1399] Create README - LeetHub --- 2678-number-of-senior-citizens/README.md | 37 ++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 2678-number-of-senior-citizens/README.md diff --git a/2678-number-of-senior-citizens/README.md b/2678-number-of-senior-citizens/README.md new file mode 100644 index 00000000..f48784b3 --- /dev/null +++ b/2678-number-of-senior-citizens/README.md @@ -0,0 +1,37 @@ +

2678. Number of Senior Citizens

Easy


You are given a 0-indexed array of strings details. Each element of details provides information about a given passenger compressed into a string of length 15. The system is such that:

+ +
    +
  • The first ten characters consist of the phone number of passengers.
  • +
  • The next character denotes the gender of the person.
  • +
  • The following two characters are used to indicate the age of the person.
  • +
  • The last two characters determine the seat allotted to that person.
  • +
+ +

Return the number of passengers who are strictly more than 60 years old.

+ +

 

+

Example 1:

+ +
Input: details = ["7868190130M7522","5303914400F9211","9273338290F4010"]
+Output: 2
+Explanation: The passengers at indices 0, 1, and 2 have ages 75, 92, and 40. Thus, there are 2 people who are over 60 years old.
+
+ +

Example 2:

+ +
Input: details = ["1313579440F2036","2921522980M5644"]
+Output: 0
+Explanation: None of the passengers are older than 60.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= details.length <= 100
  • +
  • details[i].length == 15
  • +
  • details[i] consists of digits from '0' to '9'.
  • +
  • details[i][10] is either 'M' or 'F' or 'O'.
  • +
  • The phone numbers and seat numbers of the passengers are distinct.
  • +
+
\ No newline at end of file From 63cb914baf463acf1fdcb1fb93d9809f2e7f708b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 1 Aug 2024 20:19:54 +0530 Subject: [PATCH 1321/1399] Time: 8 ms (47.01%), Space: 17.6 MB (16.71%) - LeetHub --- .../2678-number-of-senior-citizens.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 2678-number-of-senior-citizens/2678-number-of-senior-citizens.cpp diff --git a/2678-number-of-senior-citizens/2678-number-of-senior-citizens.cpp b/2678-number-of-senior-citizens/2678-number-of-senior-citizens.cpp new file mode 100644 index 00000000..d02e9832 --- /dev/null +++ b/2678-number-of-senior-citizens/2678-number-of-senior-citizens.cpp @@ -0,0 +1,14 @@ +class Solution { +public: + int countSeniors(vector& details) { + + int cnt = 0; + + for(auto& str : details) + { + cnt += (stoi(str.substr(11, 2)) > 60); + } + + return cnt; + } +}; \ No newline at end of file From 1c4b70fcf8e232ffb5a8ca3e906d21e1b609032a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 2 Aug 2024 18:47:39 +0530 Subject: [PATCH 1322/1399] Create README - LeetHub From 74e61184918fe372f2b2e9846d11898894baea3b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 2 Aug 2024 22:07:06 +0530 Subject: [PATCH 1323/1399] Create README - LeetHub --- .../README.md | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 2134-minimum-swaps-to-group-all-1s-together-ii/README.md diff --git a/2134-minimum-swaps-to-group-all-1s-together-ii/README.md b/2134-minimum-swaps-to-group-all-1s-together-ii/README.md new file mode 100644 index 00000000..2c4dc53e --- /dev/null +++ b/2134-minimum-swaps-to-group-all-1s-together-ii/README.md @@ -0,0 +1,46 @@ +

2134. Minimum Swaps to Group All 1's Together II

Medium


A swap is defined as taking two distinct positions in an array and swapping the values in them.

+ +

A circular array is defined as an array where we consider the first element and the last element to be adjacent.

+ +

Given a binary circular array nums, return the minimum number of swaps required to group all 1's present in the array together at any location.

+ +

 

+

Example 1:

+ +
Input: nums = [0,1,0,1,1,0,0]
+Output: 1
+Explanation: Here are a few of the ways to group all the 1's together:
+[0,0,1,1,1,0,0] using 1 swap.
+[0,1,1,1,0,0,0] using 1 swap.
+[1,1,0,0,0,0,1] using 2 swaps (using the circular property of the array).
+There is no way to group all 1's together with 0 swaps.
+Thus, the minimum number of swaps required is 1.
+
+ +

Example 2:

+ +
Input: nums = [0,1,1,1,0,0,1,1,0]
+Output: 2
+Explanation: Here are a few of the ways to group all the 1's together:
+[1,1,1,0,0,0,0,1,1] using 2 swaps (using the circular property of the array).
+[1,1,1,1,1,0,0,0,0] using 2 swaps.
+There is no way to group all 1's together with 0 or 1 swaps.
+Thus, the minimum number of swaps required is 2.
+
+ +

Example 3:

+ +
Input: nums = [1,1,0,0,1]
+Output: 0
+Explanation: All the 1's are already grouped together due to the circular property of the array.
+Thus, the minimum number of swaps required is 0.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 105
  • +
  • nums[i] is either 0 or 1.
  • +
+
\ No newline at end of file From 5c9b070ffaa696396be920ec091b343fe5bdc141 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 2 Aug 2024 22:07:09 +0530 Subject: [PATCH 1324/1399] Time: 80 ms (16.50%), Space: 91.1 MB (13.75%) - LeetHub --- ...imum-swaps-to-group-all-1s-together-ii.cpp | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 2134-minimum-swaps-to-group-all-1s-together-ii/2134-minimum-swaps-to-group-all-1s-together-ii.cpp diff --git a/2134-minimum-swaps-to-group-all-1s-together-ii/2134-minimum-swaps-to-group-all-1s-together-ii.cpp b/2134-minimum-swaps-to-group-all-1s-together-ii/2134-minimum-swaps-to-group-all-1s-together-ii.cpp new file mode 100644 index 00000000..276805cd --- /dev/null +++ b/2134-minimum-swaps-to-group-all-1s-together-ii/2134-minimum-swaps-to-group-all-1s-together-ii.cpp @@ -0,0 +1,30 @@ +class Solution { +public: + int minSwaps(vector& nums) { + + int n = nums.size(); + int cnt = 0; + + for(auto& ele : nums) + cnt += ele; + + for(int i = 0; i < n; ++i) + nums.push_back(nums[i]); + + int ans = INT_MAX, curCnt = 0, j = 0, i = 0; + + while(j < 2*n) + { + curCnt += nums[j]; + + if(j - i + 1 == cnt) + { + ans = min(ans, cnt - curCnt); + curCnt -= nums[i++]; + } + ++j; + } + + return ans == INT_MAX ? 0 : ans; + } +}; \ No newline at end of file From 89c0f32c851c41af667d130ad29968688cd9f7d4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 2 Aug 2024 22:07:12 +0530 Subject: [PATCH 1325/1399] Attach NOTES - LeetHub --- 2134-minimum-swaps-to-group-all-1s-together-ii/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2134-minimum-swaps-to-group-all-1s-together-ii/NOTES.md diff --git a/2134-minimum-swaps-to-group-all-1s-together-ii/NOTES.md b/2134-minimum-swaps-to-group-all-1s-together-ii/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2134-minimum-swaps-to-group-all-1s-together-ii/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 639f7b9204ce138cba9a9818719de86c6a8ff29d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 2 Aug 2024 22:07:15 +0530 Subject: [PATCH 1326/1399] Time: 80 ms (16.50%), Space: 91.1 MB (13.75%) - LeetHub From a8089845aabb71ef006654f54072515cb4a6598d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 3 Aug 2024 21:52:46 +0530 Subject: [PATCH 1327/1399] Attach NOTES - LeetHub --- 1460-make-two-arrays-equal-by-reversing-subarrays/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1460-make-two-arrays-equal-by-reversing-subarrays/NOTES.md diff --git a/1460-make-two-arrays-equal-by-reversing-subarrays/NOTES.md b/1460-make-two-arrays-equal-by-reversing-subarrays/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1460-make-two-arrays-equal-by-reversing-subarrays/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 1f69fc485453037ca326e4e700919c2b2cbd1b2d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 3 Aug 2024 21:52:49 +0530 Subject: [PATCH 1328/1399] Time: 7 ms (84.20%), Space: 17.5 MB (57.40%) - LeetHub --- ...0-make-two-arrays-equal-by-reversing-subarrays.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 1460-make-two-arrays-equal-by-reversing-subarrays/1460-make-two-arrays-equal-by-reversing-subarrays.cpp diff --git a/1460-make-two-arrays-equal-by-reversing-subarrays/1460-make-two-arrays-equal-by-reversing-subarrays.cpp b/1460-make-two-arrays-equal-by-reversing-subarrays/1460-make-two-arrays-equal-by-reversing-subarrays.cpp new file mode 100644 index 00000000..7b717361 --- /dev/null +++ b/1460-make-two-arrays-equal-by-reversing-subarrays/1460-make-two-arrays-equal-by-reversing-subarrays.cpp @@ -0,0 +1,11 @@ +class Solution { +public: + bool canBeEqual(vector& target, vector& arr) { + + sort(arr.begin(), arr.end()); + sort(target.begin(), target.end()); + + return arr == target; + + } +}; \ No newline at end of file From 745e8d482e0c416a02990899ed7fc9d5e77307b9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 3 Aug 2024 21:53:05 +0530 Subject: [PATCH 1329/1399] Attach NOTES - LeetHub From 74dc8f8f6957168b1b79235bff77edb35f7fe9ee Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 3 Aug 2024 21:53:08 +0530 Subject: [PATCH 1330/1399] Time: 7 ms (84.20%), Space: 17.5 MB (57.40%) - LeetHub From 162ddcc6defc886293edf653e5f3012e3e672621 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 4 Aug 2024 20:34:09 +0530 Subject: [PATCH 1331/1399] Create README - LeetHub --- .../README.md | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 1508-range-sum-of-sorted-subarray-sums/README.md diff --git a/1508-range-sum-of-sorted-subarray-sums/README.md b/1508-range-sum-of-sorted-subarray-sums/README.md new file mode 100644 index 00000000..ac175361 --- /dev/null +++ b/1508-range-sum-of-sorted-subarray-sums/README.md @@ -0,0 +1,35 @@ +

1508. Range Sum of Sorted Subarray Sums

Medium


You are given the array nums consisting of n positive integers. You computed the sum of all non-empty continuous subarrays from the array and then sorted them in non-decreasing order, creating a new array of n * (n + 1) / 2 numbers.

+ +

Return the sum of the numbers from index left to index right (indexed from 1), inclusive, in the new array. Since the answer can be a huge number return it modulo 109 + 7.

+ +

 

+

Example 1:

+ +
Input: nums = [1,2,3,4], n = 4, left = 1, right = 5
+Output: 13 
+Explanation: All subarray sums are 1, 3, 6, 10, 2, 5, 9, 3, 7, 4. After sorting them in non-decreasing order we have the new array [1, 2, 3, 3, 4, 5, 6, 7, 9, 10]. The sum of the numbers from index le = 1 to ri = 5 is 1 + 2 + 3 + 3 + 4 = 13. 
+
+ +

Example 2:

+ +
Input: nums = [1,2,3,4], n = 4, left = 3, right = 4
+Output: 6
+Explanation: The given array is the same as example 1. We have the new array [1, 2, 3, 3, 4, 5, 6, 7, 9, 10]. The sum of the numbers from index le = 3 to ri = 4 is 3 + 3 = 6.
+
+ +

Example 3:

+ +
Input: nums = [1,2,3,4], n = 4, left = 1, right = 10
+Output: 50
+
+ +

 

+

Constraints:

+ +
    +
  • n == nums.length
  • +
  • 1 <= nums.length <= 1000
  • +
  • 1 <= nums[i] <= 100
  • +
  • 1 <= left <= right <= n * (n + 1) / 2
  • +
+
\ No newline at end of file From 2d4d0823eaed5dedafc6429c26e2e7397b4b12ce Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 4 Aug 2024 20:34:13 +0530 Subject: [PATCH 1332/1399] Time: 100 ms (22.70%), Space: 35 MB (15.96%) - LeetHub --- ...1508-range-sum-of-sorted-subarray-sums.cpp | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 1508-range-sum-of-sorted-subarray-sums/1508-range-sum-of-sorted-subarray-sums.cpp diff --git a/1508-range-sum-of-sorted-subarray-sums/1508-range-sum-of-sorted-subarray-sums.cpp b/1508-range-sum-of-sorted-subarray-sums/1508-range-sum-of-sorted-subarray-sums.cpp new file mode 100644 index 00000000..a4ebe508 --- /dev/null +++ b/1508-range-sum-of-sorted-subarray-sums/1508-range-sum-of-sorted-subarray-sums.cpp @@ -0,0 +1,32 @@ +class Solution { +public: + int rangeSum(vector& nums, int n, int left, int right) { + + vector sums; + + for(int i = 0; i < n; ++i) + { + int sum = 0; + for(int j = i; j < n; ++j) + { + sum += nums[j]; + sums.push_back(sum); + } + } + + sort(sums.begin(), sums.end()); + + vector pref(sums.size()); + + pref[0] = sums[0]; + + const int mod = 1e9+7; + int ans = 0; + + for(int i = left-1; i < right; ++i) + ans = (ans + sums[i]) % mod; + + return ans; + + } +}; \ No newline at end of file From 064cdde07322794c2ee2dc10d66548902dde8c3a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 4 Aug 2024 20:35:32 +0530 Subject: [PATCH 1333/1399] Attach NOTES - LeetHub --- 1508-range-sum-of-sorted-subarray-sums/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1508-range-sum-of-sorted-subarray-sums/NOTES.md diff --git a/1508-range-sum-of-sorted-subarray-sums/NOTES.md b/1508-range-sum-of-sorted-subarray-sums/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1508-range-sum-of-sorted-subarray-sums/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 3878a2c37771170841205c371c757bc50a187d35 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 4 Aug 2024 21:52:16 +0530 Subject: [PATCH 1334/1399] Create README - LeetHub From 6e3c5f8d3753d5bd544ab3c63b65944cc05f964a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 4 Aug 2024 22:00:03 +0530 Subject: [PATCH 1335/1399] Create README - LeetHub --- .../README.md | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 0462-minimum-moves-to-equal-array-elements-ii/README.md diff --git a/0462-minimum-moves-to-equal-array-elements-ii/README.md b/0462-minimum-moves-to-equal-array-elements-ii/README.md new file mode 100644 index 00000000..9c851770 --- /dev/null +++ b/0462-minimum-moves-to-equal-array-elements-ii/README.md @@ -0,0 +1,31 @@ +

462. Minimum Moves to Equal Array Elements II

Medium


Given an integer array nums of size n, return the minimum number of moves required to make all array elements equal.

+ +

In one move, you can increment or decrement an element of the array by 1.

+ +

Test cases are designed so that the answer will fit in a 32-bit integer.

+ +

 

+

Example 1:

+ +
Input: nums = [1,2,3]
+Output: 2
+Explanation:
+Only two moves are needed (remember each move increments or decrements one element):
+[1,2,3]  =>  [2,2,3]  =>  [2,2,2]
+
+ +

Example 2:

+ +
Input: nums = [1,10,2,9]
+Output: 16
+
+ +

 

+

Constraints:

+ +
    +
  • n == nums.length
  • +
  • 1 <= nums.length <= 105
  • +
  • -109 <= nums[i] <= 109
  • +
+
\ No newline at end of file From 33756b13abd1686b58c35f1469511f155ecf2b24 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 4 Aug 2024 22:00:06 +0530 Subject: [PATCH 1336/1399] Time: 9 ms (59.93%), Space: 14.8 MB (48.57%) - LeetHub --- ...nimum-moves-to-equal-array-elements-ii.cpp | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 0462-minimum-moves-to-equal-array-elements-ii/0462-minimum-moves-to-equal-array-elements-ii.cpp diff --git a/0462-minimum-moves-to-equal-array-elements-ii/0462-minimum-moves-to-equal-array-elements-ii.cpp b/0462-minimum-moves-to-equal-array-elements-ii/0462-minimum-moves-to-equal-array-elements-ii.cpp new file mode 100644 index 00000000..4e5db170 --- /dev/null +++ b/0462-minimum-moves-to-equal-array-elements-ii/0462-minimum-moves-to-equal-array-elements-ii.cpp @@ -0,0 +1,24 @@ +class Solution { +public: + int minMoves2(vector& nums) { + + int n = nums.size(); + + sort(nums.begin(), nums.end()); + + int med; + if(n&1) + med = nums[n/2]; + else + med = (nums[n/2] + nums[(n/2) - 1])/2; + + int ans = 0; + + for(int i = 0; i < n; ++i) + { + ans += abs(med - nums[i]); + } + + return ans; + } +}; \ No newline at end of file From 1860c57bf3af6fe75f3e828fead2dc97e5852f51 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 4 Aug 2024 22:01:23 +0530 Subject: [PATCH 1337/1399] Attach NOTES - LeetHub --- 0462-minimum-moves-to-equal-array-elements-ii/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0462-minimum-moves-to-equal-array-elements-ii/NOTES.md diff --git a/0462-minimum-moves-to-equal-array-elements-ii/NOTES.md b/0462-minimum-moves-to-equal-array-elements-ii/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0462-minimum-moves-to-equal-array-elements-ii/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 03c725d99defe08d2cf8cf62b6141c567bbabc74 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 4 Aug 2024 22:01:26 +0530 Subject: [PATCH 1338/1399] Time: 9 ms (59.93%), Space: 14.8 MB (48.57%) - LeetHub From 78c8bd492a4251e90de2bba4864e48751b667554 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 4 Aug 2024 22:14:26 +0530 Subject: [PATCH 1339/1399] Attach NOTES - LeetHub --- 0067-add-binary/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0067-add-binary/NOTES.md diff --git a/0067-add-binary/NOTES.md b/0067-add-binary/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0067-add-binary/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 6d8ef1a18cd2fd81ab18d563f3f68b39ae6ca641 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 4 Aug 2024 22:17:19 +0530 Subject: [PATCH 1340/1399] Attach NOTES - LeetHub From beb8ad4e07e3f7a85c1be941fd3f040b55ec5a3a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 4 Aug 2024 22:20:25 +0530 Subject: [PATCH 1341/1399] Create README - LeetHub --- .../README.md | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 0628-maximum-product-of-three-numbers/README.md diff --git a/0628-maximum-product-of-three-numbers/README.md b/0628-maximum-product-of-three-numbers/README.md new file mode 100644 index 00000000..ecd0b816 --- /dev/null +++ b/0628-maximum-product-of-three-numbers/README.md @@ -0,0 +1,21 @@ +

628. Maximum Product of Three Numbers

Easy


Given an integer array nums, find three numbers whose product is maximum and return the maximum product.

+ +

 

+

Example 1:

+
Input: nums = [1,2,3]
+Output: 6
+

Example 2:

+
Input: nums = [1,2,3,4]
+Output: 24
+

Example 3:

+
Input: nums = [-1,-2,-3]
+Output: -6
+
+

 

+

Constraints:

+ +
    +
  • 3 <= nums.length <= 104
  • +
  • -1000 <= nums[i] <= 1000
  • +
+
\ No newline at end of file From 193b8a8a3a9210483b63ac17f1379db1107e7e4a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 4 Aug 2024 22:20:28 +0530 Subject: [PATCH 1342/1399] Time: 37 ms (38.52%), Space: 31.5 MB (27.86%) - LeetHub --- .../0628-maximum-product-of-three-numbers.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 0628-maximum-product-of-three-numbers/0628-maximum-product-of-three-numbers.cpp diff --git a/0628-maximum-product-of-three-numbers/0628-maximum-product-of-three-numbers.cpp b/0628-maximum-product-of-three-numbers/0628-maximum-product-of-three-numbers.cpp new file mode 100644 index 00000000..75508a34 --- /dev/null +++ b/0628-maximum-product-of-three-numbers/0628-maximum-product-of-three-numbers.cpp @@ -0,0 +1,12 @@ +class Solution { +public: + int maximumProduct(vector& nums) { + + int n = nums.size(); + + sort(nums.begin(), nums.end()); + + return max({nums[0] * nums[1] * nums[2], nums[0] * nums[1] * nums[n-1], nums[n-3] * nums[n-2] * nums[n-1]}); + + } +}; \ No newline at end of file From 403a8c1af9c40124bdc03fc8d8c3b987969a404b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 4 Aug 2024 22:21:30 +0530 Subject: [PATCH 1343/1399] Attach NOTES - LeetHub --- 0628-maximum-product-of-three-numbers/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0628-maximum-product-of-three-numbers/NOTES.md diff --git a/0628-maximum-product-of-three-numbers/NOTES.md b/0628-maximum-product-of-three-numbers/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0628-maximum-product-of-three-numbers/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 7b8ec3179e96b8ac5702b5e8df461ace6fbe8520 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 5 Aug 2024 07:09:53 +0530 Subject: [PATCH 1344/1399] Create README - LeetHub --- .../README.md | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 2053-kth-distinct-string-in-an-array/README.md diff --git a/2053-kth-distinct-string-in-an-array/README.md b/2053-kth-distinct-string-in-an-array/README.md new file mode 100644 index 00000000..30e7edf5 --- /dev/null +++ b/2053-kth-distinct-string-in-an-array/README.md @@ -0,0 +1,43 @@ +

2053. Kth Distinct String in an Array

Easy


A distinct string is a string that is present only once in an array.

+ +

Given an array of strings arr, and an integer k, return the kth distinct string present in arr. If there are fewer than k distinct strings, return an empty string "".

+ +

Note that the strings are considered in the order in which they appear in the array.

+ +

 

+

Example 1:

+ +
Input: arr = ["d","b","c","b","c","a"], k = 2
+Output: "a"
+Explanation:
+The only distinct strings in arr are "d" and "a".
+"d" appears 1st, so it is the 1st distinct string.
+"a" appears 2nd, so it is the 2nd distinct string.
+Since k == 2, "a" is returned. 
+
+ +

Example 2:

+ +
Input: arr = ["aaa","aa","a"], k = 1
+Output: "aaa"
+Explanation:
+All strings in arr are distinct, so the 1st string "aaa" is returned.
+
+ +

Example 3:

+ +
Input: arr = ["a","b","a"], k = 3
+Output: ""
+Explanation:
+The only distinct string is "b". Since there are fewer than 3 distinct strings, we return an empty string "".
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= k <= arr.length <= 1000
  • +
  • 1 <= arr[i].length <= 5
  • +
  • arr[i] consists of lowercase English letters.
  • +
+
\ No newline at end of file From 771b82c2884177aab637da815704a41ea000491c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 5 Aug 2024 07:09:54 +0530 Subject: [PATCH 1345/1399] Attach NOTES - LeetHub --- 2053-kth-distinct-string-in-an-array/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 2053-kth-distinct-string-in-an-array/NOTES.md diff --git a/2053-kth-distinct-string-in-an-array/NOTES.md b/2053-kth-distinct-string-in-an-array/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/2053-kth-distinct-string-in-an-array/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 7a69d3f7a113f7865da53ab976669c3f1e77f2aa Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 5 Aug 2024 07:09:58 +0530 Subject: [PATCH 1346/1399] Time: 33 ms (9.15%), Space: 20.2 MB (5.44%) - LeetHub --- .../2053-kth-distinct-string-in-an-array.cpp | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 2053-kth-distinct-string-in-an-array/2053-kth-distinct-string-in-an-array.cpp diff --git a/2053-kth-distinct-string-in-an-array/2053-kth-distinct-string-in-an-array.cpp b/2053-kth-distinct-string-in-an-array/2053-kth-distinct-string-in-an-array.cpp new file mode 100644 index 00000000..b1a31e15 --- /dev/null +++ b/2053-kth-distinct-string-in-an-array/2053-kth-distinct-string-in-an-array.cpp @@ -0,0 +1,23 @@ +class Solution { +public: + string kthDistinct(vector& arr, int k) { + + map mp; + vector vec; + + for(auto& str : arr) + ++mp[str]; + + for(auto& str : arr) + { + if(mp[str] == 1) + { + vec.push_back(str); + ++mp[str]; + } + } + + return (vec.size() < k ? "" : vec[k-1]); + + } +}; \ No newline at end of file From f75a51f5bc6357acd90ce9c128abb0c61c71cede Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 5 Aug 2024 07:10:43 +0530 Subject: [PATCH 1347/1399] Attach NOTES - LeetHub From ae4bea2c9523f1f530225920a6b62dbb68509295 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 5 Aug 2024 07:10:46 +0530 Subject: [PATCH 1348/1399] Time: 22 ms (27.19%), Space: 20.2 MB (5.97%) - LeetHub --- .../2053-kth-distinct-string-in-an-array.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/2053-kth-distinct-string-in-an-array/2053-kth-distinct-string-in-an-array.cpp b/2053-kth-distinct-string-in-an-array/2053-kth-distinct-string-in-an-array.cpp index b1a31e15..fa609201 100644 --- a/2053-kth-distinct-string-in-an-array/2053-kth-distinct-string-in-an-array.cpp +++ b/2053-kth-distinct-string-in-an-array/2053-kth-distinct-string-in-an-array.cpp @@ -13,7 +13,6 @@ class Solution { if(mp[str] == 1) { vec.push_back(str); - ++mp[str]; } } From 6b27bda96149e8ceb221b7bcb58d19199364f453 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 5 Aug 2024 07:11:15 +0530 Subject: [PATCH 1349/1399] Attach NOTES - LeetHub From 94fdc7426ede891c4469637fd58c80f437f5480c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 7 Aug 2024 23:37:14 +0530 Subject: [PATCH 1350/1399] Create README - LeetHub --- 0273-integer-to-english-words/README.md | 28 +++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 0273-integer-to-english-words/README.md diff --git a/0273-integer-to-english-words/README.md b/0273-integer-to-english-words/README.md new file mode 100644 index 00000000..708515e3 --- /dev/null +++ b/0273-integer-to-english-words/README.md @@ -0,0 +1,28 @@ +

273. Integer to English Words

Hard


Convert a non-negative integer num to its English words representation.

+ +

 

+

Example 1:

+ +
Input: num = 123
+Output: "One Hundred Twenty Three"
+
+ +

Example 2:

+ +
Input: num = 12345
+Output: "Twelve Thousand Three Hundred Forty Five"
+
+ +

Example 3:

+ +
Input: num = 1234567
+Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
+
+ +

 

+

Constraints:

+ +
    +
  • 0 <= num <= 231 - 1
  • +
+
\ No newline at end of file From cd6b587d8e1c461dd3662b387681882eb3a01bdf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 7 Aug 2024 23:37:18 +0530 Subject: [PATCH 1351/1399] Time: 4 ms (49.19%), Space: 12.4 MB (28.62%) - LeetHub --- .../0273-integer-to-english-words.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 0273-integer-to-english-words/0273-integer-to-english-words.cpp diff --git a/0273-integer-to-english-words/0273-integer-to-english-words.cpp b/0273-integer-to-english-words/0273-integer-to-english-words.cpp new file mode 100644 index 00000000..f6db280d --- /dev/null +++ b/0273-integer-to-english-words/0273-integer-to-english-words.cpp @@ -0,0 +1,19 @@ +class Solution { +public: + vector> nums ={{1000000000, "Billion"}, {1000000, "Million"}, + {1000, "Thousand"}, {100, "Hundred"}, {90, "Ninety"}, {80, "Eighty"}, {70, "Seventy"}, + {60, "Sixty"}, {50, "Fifty"}, {40, "Forty"}, {30, "Thirty"}, {20, "Twenty"}, {19, "Nineteen"}, + {18, "Eighteen"}, {17, "Seventeen"}, {16, "Sixteen"}, {15, "Fifteen"}, {14, "Fourteen"}, + {13, "Thirteen"}, {12, "Twelve"}, {11, "Eleven"}, {10, "Ten"}, {9, "Nine"}, {8, "Eight"}, + {7, "Seven"}, {6, "Six"}, {5, "Five"}, {4, "Four"}, {3, "Three"}, {2, "Two"}, {1, "One"}}; + + string numberToWords(int num) { + if(num == 0) return "Zero"; + + for(auto it: nums) + if(num >= it.first) + return (num >= 100 ? numberToWords(num/it.first)+" " : "") + it.second + (num%it.first == 0 ? "" : " "+numberToWords(num%it.first)); + + return ""; + } +}; \ No newline at end of file From eb89a74ab3a07af55f501ba26b4be740aa9e673f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 8 Aug 2024 23:45:05 +0530 Subject: [PATCH 1352/1399] Create README - LeetHub --- 0885-spiral-matrix-iii/README.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 0885-spiral-matrix-iii/README.md diff --git a/0885-spiral-matrix-iii/README.md b/0885-spiral-matrix-iii/README.md new file mode 100644 index 00000000..721d4ed3 --- /dev/null +++ b/0885-spiral-matrix-iii/README.md @@ -0,0 +1,28 @@ +

885. Spiral Matrix III

Medium


You start at the cell (rStart, cStart) of an rows x cols grid facing east. The northwest corner is at the first row and column in the grid, and the southeast corner is at the last row and column.

+ +

You will walk in a clockwise spiral shape to visit every position in this grid. Whenever you move outside the grid's boundary, we continue our walk outside the grid (but may return to the grid boundary later.). Eventually, we reach all rows * cols spaces of the grid.

+ +

Return an array of coordinates representing the positions of the grid in the order you visited them.

+ +

 

+

Example 1:

+ +
Input: rows = 1, cols = 4, rStart = 0, cStart = 0
+Output: [[0,0],[0,1],[0,2],[0,3]]
+
+ +

Example 2:

+ +
Input: rows = 5, cols = 6, rStart = 1, cStart = 4
+Output: [[1,4],[1,5],[2,5],[2,4],[2,3],[1,3],[0,3],[0,4],[0,5],[3,5],[3,4],[3,3],[3,2],[2,2],[1,2],[0,2],[4,5],[4,4],[4,3],[4,2],[4,1],[3,1],[2,1],[1,1],[0,1],[4,0],[3,0],[2,0],[1,0],[0,0]]
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= rows, cols <= 100
  • +
  • 0 <= rStart < rows
  • +
  • 0 <= cStart < cols
  • +
+
\ No newline at end of file From 8a4f254c7adde2f794ae29d09e184eef96b79dd7 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 8 Aug 2024 23:45:08 +0530 Subject: [PATCH 1353/1399] Time: 11 ms (42.57%), Space: 13.9 MB (23.17%) - LeetHub --- .../0885-spiral-matrix-iii.cpp | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 0885-spiral-matrix-iii/0885-spiral-matrix-iii.cpp diff --git a/0885-spiral-matrix-iii/0885-spiral-matrix-iii.cpp b/0885-spiral-matrix-iii/0885-spiral-matrix-iii.cpp new file mode 100644 index 00000000..78e13cb1 --- /dev/null +++ b/0885-spiral-matrix-iii/0885-spiral-matrix-iii.cpp @@ -0,0 +1,50 @@ +class Solution { +public: + vector> spiralMatrixIII(int rows, int cols, int rStart, int cStart) { + + vector> res; + int step = 1, ctr = 0; + + auto isValid = [&]() + { + return (rStart >= 0 and rStart < rows and cStart >= 0 and cStart < cols); + }; + + while(ctr < rows * cols) + { + for(int i = 0; i < step; ++i) + { + if(isValid()) + res.push_back({rStart, cStart}), ++ctr; + ++cStart; + } + + for(int i = 0; i < step; ++i) + { + if(isValid()) + res.push_back({rStart, cStart}), ++ctr; + ++rStart; + } + + ++step; + + for(int i = 0; i < step; ++i) + { + if(isValid()) + res.push_back({rStart, cStart}), ++ctr; + --cStart; + } + + for(int i = 0; i < step; ++i) + { + if(isValid()) + res.push_back({rStart, cStart}), ++ctr; + --rStart; + } + + ++step; + } + + return res; + } +}; \ No newline at end of file From fc928e4faa0dbcf01ed14e56ad757eff1018408b Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 9 Aug 2024 08:36:12 +0530 Subject: [PATCH 1354/1399] Create README - LeetHub --- 0840-magic-squares-in-grid/README.md | 35 ++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 0840-magic-squares-in-grid/README.md diff --git a/0840-magic-squares-in-grid/README.md b/0840-magic-squares-in-grid/README.md new file mode 100644 index 00000000..06ddc8d2 --- /dev/null +++ b/0840-magic-squares-in-grid/README.md @@ -0,0 +1,35 @@ +

840. Magic Squares In Grid

Medium


A 3 x 3 magic square is a 3 x 3 grid filled with distinct numbers from 1 to 9 such that each row, column, and both diagonals all have the same sum.

+ +

Given a row x col grid of integers, how many 3 x 3 contiguous magic square subgrids are there?

+ +

Note: while a magic square can only contain numbers from 1 to 9, grid may contain numbers up to 15.

+ +

 

+

Example 1:

+ +
Input: grid = [[4,3,8,4],[9,5,1,9],[2,7,6,2]]
+Output: 1
+Explanation: 
+The following subgrid is a 3 x 3 magic square:
+
+while this one is not:
+
+In total, there is only one magic square inside the given grid.
+
+ +

Example 2:

+ +
Input: grid = [[8]]
+Output: 0
+
+ +

 

+

Constraints:

+ +
    +
  • row == grid.length
  • +
  • col == grid[i].length
  • +
  • 1 <= row, col <= 10
  • +
  • 0 <= grid[i][j] <= 15
  • +
+
\ No newline at end of file From 33fe7b6e326749685b019c87e200487971519290 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 9 Aug 2024 08:36:16 +0530 Subject: [PATCH 1355/1399] Time: 11 ms (5.88%), Space: 15.3 MB (7.14%) - LeetHub --- .../0840-magic-squares-in-grid.cpp | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 0840-magic-squares-in-grid/0840-magic-squares-in-grid.cpp diff --git a/0840-magic-squares-in-grid/0840-magic-squares-in-grid.cpp b/0840-magic-squares-in-grid/0840-magic-squares-in-grid.cpp new file mode 100644 index 00000000..3cfd5c6a --- /dev/null +++ b/0840-magic-squares-in-grid/0840-magic-squares-in-grid.cpp @@ -0,0 +1,81 @@ +class Solution { +public: + + int checkMatrix(vector>& mat) + { + + int diagSumA = 0, diagSumB = 0, end = 2; + map mp; + vector freq(10, 0); + + for(int i = 0; i < mat.size(); ++i) + { + int sum = 0; + for(int j = 0; j < mat[0].size(); ++j) + { + if(mat[i][j] >= 0 and mat[i][j] <= 9) + ++freq[mat[i][j]]; + sum += mat[i][j]; + if(i == j) + diagSumA += mat[i][j]; + if(j == end) + { + diagSumB += mat[i][j]; + --end; + } + } + ++mp[sum]; + } + + for(int j = 0; j < mat[0].size(); ++j) + { + int sum = 0; + for(int i = 0; i < mat.size(); ++i) + sum += mat[i][j]; + ++mp[sum]; + } + + for(int i = 1; i <= 9; ++i) + { + if(freq[i] == 0) + return 0; + } + + ++mp[diagSumA]; + ++mp[diagSumB]; + + return mp.size() == 1; + } + + int numMagicSquaresInside(vector>& grid) { + + int n = grid.size(); + int m = grid[0].size(); + + int ans = 0; + + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < m; ++j) + { + vector> mat; + + if(i + 3 > n or j + 3 > m) + break; + for(int k = i; k < i + 3; ++k) + { + vector row; + for(int l = j; l < j + 3; ++l) + row.push_back(grid[k][l]); + mat.push_back(row); + } + + if(checkMatrix(mat)) + ++ans; + } + } + + return ans; + + } +}; \ No newline at end of file From 37078e2d5df16db4669f559c0c0f1c44fffff8b1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 9 Aug 2024 08:37:05 +0530 Subject: [PATCH 1356/1399] Attach NOTES - LeetHub --- 0840-magic-squares-in-grid/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0840-magic-squares-in-grid/NOTES.md diff --git a/0840-magic-squares-in-grid/NOTES.md b/0840-magic-squares-in-grid/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0840-magic-squares-in-grid/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From e16569c4e008ef4e314cb3f0e5d374ee3630466d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 9 Aug 2024 08:37:08 +0530 Subject: [PATCH 1357/1399] Time: 11 ms (5.88%), Space: 15.3 MB (7.14%) - LeetHub From ae53d915e5e3281c3330124d63af6fa2f556a00f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 10 Aug 2024 23:30:16 +0530 Subject: [PATCH 1358/1399] Create README - LeetHub --- 0959-regions-cut-by-slashes/README.md | 35 +++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 0959-regions-cut-by-slashes/README.md diff --git a/0959-regions-cut-by-slashes/README.md b/0959-regions-cut-by-slashes/README.md new file mode 100644 index 00000000..fc2c5d3b --- /dev/null +++ b/0959-regions-cut-by-slashes/README.md @@ -0,0 +1,35 @@ +

959. Regions Cut By Slashes

Medium


An n x n grid is composed of 1 x 1 squares where each 1 x 1 square consists of a '/', '\', or blank space ' '. These characters divide the square into contiguous regions.

+ +

Given the grid grid represented as a string array, return the number of regions.

+ +

Note that backslash characters are escaped, so a '\' is represented as '\\'.

+ +

 

+

Example 1:

+ +
Input: grid = [" /","/ "]
+Output: 2
+
+ +

Example 2:

+ +
Input: grid = [" /","  "]
+Output: 1
+
+ +

Example 3:

+ +
Input: grid = ["/\\","\\/"]
+Output: 5
+Explanation: Recall that because \ characters are escaped, "\\/" refers to \/, and "/\\" refers to /\.
+
+ +

 

+

Constraints:

+ +
    +
  • n == grid.length == grid[i].length
  • +
  • 1 <= n <= 30
  • +
  • grid[i][j] is either '/', '\', or ' '.
  • +
+
\ No newline at end of file From bf8019f434c26995745e6ab1741f6163d53311d5 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 10 Aug 2024 23:30:20 +0530 Subject: [PATCH 1359/1399] Time: 33 ms (10.57%), Space: 16.8 MB (13.01%) - LeetHub --- .../0959-regions-cut-by-slashes.cpp | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 0959-regions-cut-by-slashes/0959-regions-cut-by-slashes.cpp diff --git a/0959-regions-cut-by-slashes/0959-regions-cut-by-slashes.cpp b/0959-regions-cut-by-slashes/0959-regions-cut-by-slashes.cpp new file mode 100644 index 00000000..614e2c88 --- /dev/null +++ b/0959-regions-cut-by-slashes/0959-regions-cut-by-slashes.cpp @@ -0,0 +1,56 @@ +class Solution { +public: + int regionsBySlashes(vector& grid) { + + int n = grid.size(); + int m = grid[0].size(); + int cnt = 0; + + vector> mat(n*3, vector(m*3, 0)); + vector> visited = mat; + + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < m; ++j) + { + if(grid[i][j] == '/') + { + mat[i*3][j*3+2] = 1; + mat[i*3+1][j*3+1] = 1; + mat[i*3+2][j*3] = 1; + } + else if(grid[i][j] == '\\') + { + mat[i*3][j*3] = 1; + mat[i*3+1][j*3+1] = 1; + mat[i*3+2][j*3+2] = 1; + } + } + } + + function dfs = [&](int i, int j) + { + if(i < 0 or j < 0 or i >= n*3 or j >= m*3 or visited[i][j] or mat[i][j] == 1) + return; + visited[i][j] = 1; + dfs(i-1, j); + dfs(i, j+1); + dfs(i+1, j); + dfs(i, j-1); + }; + + for(int i = 0; i < n*3; ++i) + { + for(int j = 0; j < m*3; ++j) + { + if(!visited[i][j] and mat[i][j] == 0) + { + ++cnt; + dfs(i, j); + } + } + } + + return cnt; + } +}; \ No newline at end of file From f088809c12db10d62cd1fee27dd60b306df28086 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 10 Aug 2024 23:30:47 +0530 Subject: [PATCH 1360/1399] Attach NOTES - LeetHub --- 0959-regions-cut-by-slashes/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0959-regions-cut-by-slashes/NOTES.md diff --git a/0959-regions-cut-by-slashes/NOTES.md b/0959-regions-cut-by-slashes/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0959-regions-cut-by-slashes/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From fdb7c9c966839ecf75603036931dfd369a7ef8c4 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 10 Aug 2024 23:30:50 +0530 Subject: [PATCH 1361/1399] Time: 33 ms (10.57%), Space: 16.8 MB (13.01%) - LeetHub From 371d31850ebb828de31d8dac780f0b64363c2924 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 11 Aug 2024 22:11:37 +0530 Subject: [PATCH 1362/1399] Create README - LeetHub --- .../README.md | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 1568-minimum-number-of-days-to-disconnect-island/README.md diff --git a/1568-minimum-number-of-days-to-disconnect-island/README.md b/1568-minimum-number-of-days-to-disconnect-island/README.md new file mode 100644 index 00000000..652dcb78 --- /dev/null +++ b/1568-minimum-number-of-days-to-disconnect-island/README.md @@ -0,0 +1,35 @@ +

1568. Minimum Number of Days to Disconnect Island

Hard


You are given an m x n binary grid grid where 1 represents land and 0 represents water. An island is a maximal 4-directionally (horizontal or vertical) connected group of 1's.

+ +

The grid is said to be connected if we have exactly one island, otherwise is said disconnected.

+ +

In one day, we are allowed to change any single land cell (1) into a water cell (0).

+ +

Return the minimum number of days to disconnect the grid.

+ +

 

+

Example 1:

+ +
Input: grid = [[0,1,1,0],[0,1,1,0],[0,0,0,0]]
+
+Output: 2
+Explanation: We need at least 2 days to get a disconnected grid.
+Change land grid[1][1] and grid[0][2] to water and get 2 disconnected island.
+
+ +

Example 2:

+ +
Input: grid = [[1,1]]
+Output: 2
+Explanation: Grid of full water is also disconnected ([[1,1]] -> [[0,0]]), 0 islands.
+
+ +

 

+

Constraints:

+ +
    +
  • m == grid.length
  • +
  • n == grid[i].length
  • +
  • 1 <= m, n <= 30
  • +
  • grid[i][j] is either 0 or 1.
  • +
+
\ No newline at end of file From 66aef824a5dfaebbff473b4e9d60934064f6b074 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 11 Aug 2024 22:11:42 +0530 Subject: [PATCH 1363/1399] Time: 84 ms (52.59%), Space: 25.5 MB (49.71%) - LeetHub --- ...um-number-of-days-to-disconnect-island.cpp | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 1568-minimum-number-of-days-to-disconnect-island/1568-minimum-number-of-days-to-disconnect-island.cpp diff --git a/1568-minimum-number-of-days-to-disconnect-island/1568-minimum-number-of-days-to-disconnect-island.cpp b/1568-minimum-number-of-days-to-disconnect-island/1568-minimum-number-of-days-to-disconnect-island.cpp new file mode 100644 index 00000000..7c93a1bf --- /dev/null +++ b/1568-minimum-number-of-days-to-disconnect-island/1568-minimum-number-of-days-to-disconnect-island.cpp @@ -0,0 +1,82 @@ +class Solution { +public: + + vector dx = {-1,0,0,+1}; + vector dy = {0,-1,+1,0}; + + void dfs(int i, int j, vector>& grid, vector>& visited) + { + visited[i][j] = 1; + + for(int k = 0; k < 4; ++k) + { + int newx = dx[k] + i; + int newy = dy[k] + j; + + if(newx >= 0 and newy >= 0 and newx < grid.size() and newy < grid[0].size() and grid[newx][newy] == 1 and !visited[newx][newy]) + { + dfs(newx,newy, grid, visited); + } + } + } + + int countNumberofIslands(vector>& grid) + { + int n = grid.size(); + int m = grid[0].size(); + int count = 0; + + vector> visited(n,vector(m,0)); + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < m; ++j) + { + if(grid[i][j] == 1 and !visited[i][j]) + { + ++count; + dfs(i,j,grid,visited); + } + } + } + + return count; + } + + int minDays(vector>& grid) { + + int n = grid.size(), m = grid[0].size(); + + int island = countNumberofIslands(grid); + + if(island > 1 or island == 0) + return 0; + + int oneCount = 0; + + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < m; ++j) + { + if(grid[i][j] == 1) + { + grid[i][j] = 0; + + island = countNumberofIslands(grid); + + if(island > 1) + return 1; + + grid[i][j] = 1; + + ++oneCount; + } + } + } + + if(oneCount == 1) + return 1; + + return 2; + + } +}; \ No newline at end of file From e6763ba9014b2f3f27e201fbdbb9505ca7f55134 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 13 Aug 2024 08:51:31 +0530 Subject: [PATCH 1364/1399] Create README - LeetHub --- 0040-combination-sum-ii/README.md | 38 +++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 0040-combination-sum-ii/README.md diff --git a/0040-combination-sum-ii/README.md b/0040-combination-sum-ii/README.md new file mode 100644 index 00000000..41ac5070 --- /dev/null +++ b/0040-combination-sum-ii/README.md @@ -0,0 +1,38 @@ +

40. Combination Sum II

Medium


Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sum to target.

+ +

Each number in candidates may only be used once in the combination.

+ +

Note: The solution set must not contain duplicate combinations.

+ +

 

+

Example 1:

+ +
Input: candidates = [10,1,2,7,6,1,5], target = 8
+Output: 
+[
+[1,1,6],
+[1,2,5],
+[1,7],
+[2,6]
+]
+
+ +

Example 2:

+ +
Input: candidates = [2,5,2,1,2], target = 5
+Output: 
+[
+[1,2,2],
+[5]
+]
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= candidates.length <= 100
  • +
  • 1 <= candidates[i] <= 50
  • +
  • 1 <= target <= 30
  • +
+
\ No newline at end of file From 58fd0d0b5b8ce03d8053436e5781cf8421681560 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 13 Aug 2024 08:51:35 +0530 Subject: [PATCH 1365/1399] Time: 0 ms (100.00%), Space: 14.2 MB (21.77%) - LeetHub --- .../0040-combination-sum-ii.cpp | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 0040-combination-sum-ii/0040-combination-sum-ii.cpp diff --git a/0040-combination-sum-ii/0040-combination-sum-ii.cpp b/0040-combination-sum-ii/0040-combination-sum-ii.cpp new file mode 100644 index 00000000..0e49d197 --- /dev/null +++ b/0040-combination-sum-ii/0040-combination-sum-ii.cpp @@ -0,0 +1,37 @@ +class Solution { +public: + + void helper(int idx, int target, vector ds, vector>& res, vector& candidates) + { + if(target == 0) + { + res.push_back(ds); + return; + } + + for(int i = idx; i < candidates.size(); ++i) + { + if(i != idx and candidates[i] == candidates[i-1]) + continue; + if(target - candidates[i] < 0) + break; + ds.push_back(candidates[i]); + helper(i+1, target-candidates[i], ds, res, candidates); + ds.pop_back(); + } + + } + + vector> combinationSum2(vector& candidates, int target) { + + vector ds; + vector> res; + + sort(candidates.begin(), candidates.end()); + + helper(0, target, ds, res, candidates); + + return res; + + } +}; \ No newline at end of file From 5866cd6b03499fd697140607661233ae1ad877b0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 14 Aug 2024 22:18:07 +0530 Subject: [PATCH 1366/1399] Create README - LeetHub --- .../README.md | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 0719-find-k-th-smallest-pair-distance/README.md diff --git a/0719-find-k-th-smallest-pair-distance/README.md b/0719-find-k-th-smallest-pair-distance/README.md new file mode 100644 index 00000000..cdd3d60c --- /dev/null +++ b/0719-find-k-th-smallest-pair-distance/README.md @@ -0,0 +1,38 @@ +

719. Find K-th Smallest Pair Distance

Hard


The distance of a pair of integers a and b is defined as the absolute difference between a and b.

+ +

Given an integer array nums and an integer k, return the kth smallest distance among all the pairs nums[i] and nums[j] where 0 <= i < j < nums.length.

+ +

 

+

Example 1:

+ +
Input: nums = [1,3,1], k = 1
+Output: 0
+Explanation: Here are all the pairs:
+(1,3) -> 2
+(1,1) -> 0
+(3,1) -> 2
+Then the 1st smallest distance pair is (1,1), and its distance is 0.
+
+ +

Example 2:

+ +
Input: nums = [1,1,1], k = 2
+Output: 0
+
+ +

Example 3:

+ +
Input: nums = [1,6,1], k = 3
+Output: 5
+
+ +

 

+

Constraints:

+ +
    +
  • n == nums.length
  • +
  • 2 <= n <= 104
  • +
  • 0 <= nums[i] <= 106
  • +
  • 1 <= k <= n * (n - 1) / 2
  • +
+
\ No newline at end of file From 620684b453d05ba1bfdce3ed92748d91c403140e Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 14 Aug 2024 22:18:07 +0530 Subject: [PATCH 1367/1399] Attach NOTES - LeetHub --- 0719-find-k-th-smallest-pair-distance/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0719-find-k-th-smallest-pair-distance/NOTES.md diff --git a/0719-find-k-th-smallest-pair-distance/NOTES.md b/0719-find-k-th-smallest-pair-distance/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0719-find-k-th-smallest-pair-distance/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 35434784dee0edbfc89ec99f6ae558025dc8bbe9 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 14 Aug 2024 22:18:10 +0530 Subject: [PATCH 1368/1399] Time: 522 ms (8.41%), Space: 96.5 MB (5.05%) - LeetHub --- .../0719-find-k-th-smallest-pair-distance.cpp | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 0719-find-k-th-smallest-pair-distance/0719-find-k-th-smallest-pair-distance.cpp diff --git a/0719-find-k-th-smallest-pair-distance/0719-find-k-th-smallest-pair-distance.cpp b/0719-find-k-th-smallest-pair-distance/0719-find-k-th-smallest-pair-distance.cpp new file mode 100644 index 00000000..af0b24dc --- /dev/null +++ b/0719-find-k-th-smallest-pair-distance/0719-find-k-th-smallest-pair-distance.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + int smallestDistancePair(vector& nums, int k) { + + int n = nums.size(), N = 1000000; + vector cnt(N, 0); + + for (int i = 0; i < n; i++) { + for (int j = i+1; j < n; j++) + cnt[abs(nums[i]-nums[j])]++; + } + + for (int i = 0; i < N; i++) { + if (cnt[i] >= k) return i; + k -= cnt[i]; + } + + return 0; + } +}; \ No newline at end of file From 445d8b61df4a2c00297d0d4052c112b9cc3b6d4f Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 15 Aug 2024 22:17:58 +0530 Subject: [PATCH 1369/1399] Create README - LeetHub --- 0860-lemonade-change/README.md | 37 ++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 0860-lemonade-change/README.md diff --git a/0860-lemonade-change/README.md b/0860-lemonade-change/README.md new file mode 100644 index 00000000..d3a40735 --- /dev/null +++ b/0860-lemonade-change/README.md @@ -0,0 +1,37 @@ +

860. Lemonade Change

Easy


At a lemonade stand, each lemonade costs $5. Customers are standing in a queue to buy from you and order one at a time (in the order specified by bills). Each customer will only buy one lemonade and pay with either a $5, $10, or $20 bill. You must provide the correct change to each customer so that the net transaction is that the customer pays $5.

+ +

Note that you do not have any change in hand at first.

+ +

Given an integer array bills where bills[i] is the bill the ith customer pays, return true if you can provide every customer with the correct change, or false otherwise.

+ +

 

+

Example 1:

+ +
Input: bills = [5,5,5,10,20]
+Output: true
+Explanation: 
+From the first 3 customers, we collect three $5 bills in order.
+From the fourth customer, we collect a $10 bill and give back a $5.
+From the fifth customer, we give a $10 bill and a $5 bill.
+Since all customers got correct change, we output true.
+
+ +

Example 2:

+ +
Input: bills = [5,5,10,10,20]
+Output: false
+Explanation: 
+From the first two customers in order, we collect two $5 bills.
+For the next two customers in order, we collect a $10 bill and give back a $5 bill.
+For the last customer, we can not give the change of $15 back because we only have two $10 bills.
+Since not every customer received the correct change, the answer is false.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= bills.length <= 105
  • +
  • bills[i] is either 5, 10, or 20.
  • +
+
\ No newline at end of file From b80851320d42ee2e4cd8aea755b5470f714f278c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 15 Aug 2024 22:18:01 +0530 Subject: [PATCH 1370/1399] Time: 67 ms (77.92%), Space: 86.1 MB (32.71%) - LeetHub --- 0860-lemonade-change/0860-lemonade-change.cpp | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 0860-lemonade-change/0860-lemonade-change.cpp diff --git a/0860-lemonade-change/0860-lemonade-change.cpp b/0860-lemonade-change/0860-lemonade-change.cpp new file mode 100644 index 00000000..630ace19 --- /dev/null +++ b/0860-lemonade-change/0860-lemonade-change.cpp @@ -0,0 +1,35 @@ +class Solution { +public: + bool lemonadeChange(vector& bills) { + + map mp; + + for(auto& ele : bills) + { + int rem = ele - 5; + + if(rem) + { + while(mp[10] and rem >= 10) + { + --mp[10]; + rem -= 10; + } + + while(mp[5] and rem) + { + --mp[5]; + rem -= 5; + } + } + + if(rem > 0) + return false; + + ++mp[ele]; + } + + return true; + + } +}; \ No newline at end of file From 3d210b8fe1b52245f04b4d87aa61e93ba026246d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 15 Aug 2024 22:18:41 +0530 Subject: [PATCH 1371/1399] Attach NOTES - LeetHub --- 0860-lemonade-change/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0860-lemonade-change/NOTES.md diff --git a/0860-lemonade-change/NOTES.md b/0860-lemonade-change/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0860-lemonade-change/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 9588e1485a96c7ca6f83683303a827cb3569fac1 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 15 Aug 2024 22:18:44 +0530 Subject: [PATCH 1372/1399] Time: 67 ms (77.92%), Space: 86.1 MB (32.71%) - LeetHub From 4602a5a9d5974444c761416382ca12d7a3753172 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 16 Aug 2024 22:16:53 +0530 Subject: [PATCH 1373/1399] Create README - LeetHub --- 0624-maximum-distance-in-arrays/README.md | 32 +++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 0624-maximum-distance-in-arrays/README.md diff --git a/0624-maximum-distance-in-arrays/README.md b/0624-maximum-distance-in-arrays/README.md new file mode 100644 index 00000000..77b7a756 --- /dev/null +++ b/0624-maximum-distance-in-arrays/README.md @@ -0,0 +1,32 @@ +

624. Maximum Distance in Arrays

Medium


You are given m arrays, where each array is sorted in ascending order.

+ +

You can pick up two integers from two different arrays (each array picks one) and calculate the distance. We define the distance between two integers a and b to be their absolute difference |a - b|.

+ +

Return the maximum distance.

+ +

 

+

Example 1:

+ +
Input: arrays = [[1,2,3],[4,5],[1,2,3]]
+Output: 4
+Explanation: One way to reach the maximum distance 4 is to pick 1 in the first or third array and pick 5 in the second array.
+
+ +

Example 2:

+ +
Input: arrays = [[1],[1]]
+Output: 0
+
+ +

 

+

Constraints:

+ +
    +
  • m == arrays.length
  • +
  • 2 <= m <= 105
  • +
  • 1 <= arrays[i].length <= 500
  • +
  • -104 <= arrays[i][j] <= 104
  • +
  • arrays[i] is sorted in ascending order.
  • +
  • There will be at most 105 integers in all the arrays.
  • +
+
\ No newline at end of file From b9ebf93aeddf1cfa59531075dbff8f9775fdc6a3 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 16 Aug 2024 22:16:56 +0530 Subject: [PATCH 1374/1399] Time: 235 ms (49.06%), Space: 107.8 MB (69.81%) - LeetHub --- .../0624-maximum-distance-in-arrays.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 0624-maximum-distance-in-arrays/0624-maximum-distance-in-arrays.cpp diff --git a/0624-maximum-distance-in-arrays/0624-maximum-distance-in-arrays.cpp b/0624-maximum-distance-in-arrays/0624-maximum-distance-in-arrays.cpp new file mode 100644 index 00000000..5ceb9190 --- /dev/null +++ b/0624-maximum-distance-in-arrays/0624-maximum-distance-in-arrays.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + int maxDistance(vector>& arrays) { + + int left = arrays[0][0], right = arrays[0].back(), ans = 0; + + for(int i = 1; i < arrays.size(); i++) + { + ans = max(ans, max(abs(arrays[i][0] - right), abs(arrays[i].back() - left))); + left = min(left, arrays[i][0]); + right = max(right, arrays[i].back()); + } + + + return ans; + } +}; \ No newline at end of file From 86180dace4573f9490192c6dabdacfd36c241d3a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 17 Aug 2024 23:57:37 +0530 Subject: [PATCH 1375/1399] Create README - LeetHub --- .../README.md | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 1937-maximum-number-of-points-with-cost/README.md diff --git a/1937-maximum-number-of-points-with-cost/README.md b/1937-maximum-number-of-points-with-cost/README.md new file mode 100644 index 00000000..d0709862 --- /dev/null +++ b/1937-maximum-number-of-points-with-cost/README.md @@ -0,0 +1,49 @@ +

1937. Maximum Number of Points with Cost

Medium


You are given an m x n integer matrix points (0-indexed). Starting with 0 points, you want to maximize the number of points you can get from the matrix.

+ +

To gain points, you must pick one cell in each row. Picking the cell at coordinates (r, c) will add points[r][c] to your score.

+ +

However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows r and r + 1 (where 0 <= r < m - 1), picking cells at coordinates (r, c1) and (r + 1, c2) will subtract abs(c1 - c2) from your score.

+ +

Return the maximum number of points you can achieve.

+ +

abs(x) is defined as:

+ +
    +
  • x for x >= 0.
  • +
  • -x for x < 0.
  • +
+ +

 

+

Example 1:

+ +
Input: points = [[1,2,3],[1,5,1],[3,1,1]]
+Output: 9
+Explanation:
+The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0).
+You add 3 + 5 + 3 = 11 to your score.
+However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score.
+Your final score is 11 - 2 = 9.
+
+ +

Example 2:

+ +
Input: points = [[1,5],[2,3],[4,2]]
+Output: 11
+Explanation:
+The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0).
+You add 5 + 3 + 4 = 12 to your score.
+However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score.
+Your final score is 12 - 1 = 11.
+
+ +

 

+

Constraints:

+ +
    +
  • m == points.length
  • +
  • n == points[r].length
  • +
  • 1 <= m, n <= 105
  • +
  • 1 <= m * n <= 105
  • +
  • 0 <= points[r][c] <= 105
  • +
+
\ No newline at end of file From ad62b6816981b853f858badf064e40716a0cccdf Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 18 Aug 2024 20:55:27 +0530 Subject: [PATCH 1376/1399] Create README - LeetHub --- 0264-ugly-number-ii/README.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 0264-ugly-number-ii/README.md diff --git a/0264-ugly-number-ii/README.md b/0264-ugly-number-ii/README.md new file mode 100644 index 00000000..296532e1 --- /dev/null +++ b/0264-ugly-number-ii/README.md @@ -0,0 +1,26 @@ +

264. Ugly Number II

Medium


An ugly number is a positive integer whose prime factors are limited to 2, 3, and 5.

+ +

Given an integer n, return the nth ugly number.

+ +

 

+

Example 1:

+ +
Input: n = 10
+Output: 12
+Explanation: [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
+
+ +

Example 2:

+ +
Input: n = 1
+Output: 1
+Explanation: 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= 1690
  • +
+
\ No newline at end of file From fa838d608420553d6b3535595064781a144b9153 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sun, 18 Aug 2024 20:55:30 +0530 Subject: [PATCH 1377/1399] Time: 3 ms (76.61%), Space: 9.7 MB (72.38%) - LeetHub --- 0264-ugly-number-ii/0264-ugly-number-ii.cpp | 23 +++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 0264-ugly-number-ii/0264-ugly-number-ii.cpp diff --git a/0264-ugly-number-ii/0264-ugly-number-ii.cpp b/0264-ugly-number-ii/0264-ugly-number-ii.cpp new file mode 100644 index 00000000..27655d96 --- /dev/null +++ b/0264-ugly-number-ii/0264-ugly-number-ii.cpp @@ -0,0 +1,23 @@ +class Solution { +public: + int nthUglyNumber(int n) { + + vector v(n+1, 1); + + int i2 = 1, i3 = 1, i5 = 1; + + for(int i = 2; i <= n; ++i) + { + int ithUgly = min({v[i2] * 2, v[i3] * 3, v[i5] * 5}); + + v[i] = ithUgly; + + if(v[i] == v[i2] * 2) ++i2; + if(v[i] == v[i3] * 3) ++i3; + if(v[i] == v[i5] * 5) ++i5; + } + + return v[n]; + + } +}; \ No newline at end of file From 75d0be16f04fb7f0e46422472f33d81629680145 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 19 Aug 2024 20:22:48 +0530 Subject: [PATCH 1378/1399] Create README - LeetHub --- 0650-2-keys-keyboard/README.md | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 0650-2-keys-keyboard/README.md diff --git a/0650-2-keys-keyboard/README.md b/0650-2-keys-keyboard/README.md new file mode 100644 index 00000000..4d778549 --- /dev/null +++ b/0650-2-keys-keyboard/README.md @@ -0,0 +1,33 @@ +

650. 2 Keys Keyboard

Medium


There is only one character 'A' on the screen of a notepad. You can perform one of two operations on this notepad for each step:

+ +
    +
  • Copy All: You can copy all the characters present on the screen (a partial copy is not allowed).
  • +
  • Paste: You can paste the characters which are copied last time.
  • +
+ +

Given an integer n, return the minimum number of operations to get the character 'A' exactly n times on the screen.

+ +

 

+

Example 1:

+ +
Input: n = 3
+Output: 3
+Explanation: Initially, we have one character 'A'.
+In step 1, we use Copy All operation.
+In step 2, we use Paste operation to get 'AA'.
+In step 3, we use Paste operation to get 'AAA'.
+
+ +

Example 2:

+ +
Input: n = 1
+Output: 0
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= 1000
  • +
+
\ No newline at end of file From 98787384617c52655f96a1dce799678cfeada80a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 19 Aug 2024 20:22:51 +0530 Subject: [PATCH 1379/1399] Time: 26 ms (19.81%), Space: 157.3 MB (8.56%) - LeetHub --- 0650-2-keys-keyboard/0650-2-keys-keyboard.cpp | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 0650-2-keys-keyboard/0650-2-keys-keyboard.cpp diff --git a/0650-2-keys-keyboard/0650-2-keys-keyboard.cpp b/0650-2-keys-keyboard/0650-2-keys-keyboard.cpp new file mode 100644 index 00000000..bf53ed7a --- /dev/null +++ b/0650-2-keys-keyboard/0650-2-keys-keyboard.cpp @@ -0,0 +1,35 @@ +class Solution { +public: + + int helper(int n, int last, int tot, vector>& dp) + { + if(n < 0) + return 1e9; + + if(n == 0) + return 0; + + if(dp[n][last] != -1) + return dp[n][last]; + + int previousCopy = 1 + helper(n - last, last, tot, dp); + + int alreadyTaken = tot - n; + + int newCopy = 2 + helper(n - alreadyTaken, alreadyTaken, tot, dp); + + return dp[n][last] = min(newCopy, previousCopy); + + } + + int minSteps(int n) { + + if(n == 1) + return 0; + + vector> dp(n+1,vector(n+1, -1)); + + return helper(n-1, 1, n, dp) + 1; + + } +}; \ No newline at end of file From 2d5b4ac0d42400b0406cfaf08c0be08fd9d30f70 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 19 Aug 2024 20:24:03 +0530 Subject: [PATCH 1380/1399] Attach NOTES - LeetHub --- 0650-2-keys-keyboard/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0650-2-keys-keyboard/NOTES.md diff --git a/0650-2-keys-keyboard/NOTES.md b/0650-2-keys-keyboard/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0650-2-keys-keyboard/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 6f155c8206492b0c0232e62ef8a348a5678df152 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 19 Aug 2024 20:24:06 +0530 Subject: [PATCH 1381/1399] Time: 26 ms (19.81%), Space: 157.3 MB (8.56%) - LeetHub From 60459ff6ef7e4ec148b98f72193c890ff6691b8c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Tue, 20 Aug 2024 22:45:08 +0530 Subject: [PATCH 1382/1399] Attach NOTES - LeetHub --- 1140-stone-game-ii/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1140-stone-game-ii/NOTES.md diff --git a/1140-stone-game-ii/NOTES.md b/1140-stone-game-ii/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1140-stone-game-ii/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 1f9efafcad18ab57895a69e112175363ec0e2cb8 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 21 Aug 2024 22:32:50 +0530 Subject: [PATCH 1383/1399] Attach NOTES - LeetHub --- 0664-strange-printer/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0664-strange-printer/NOTES.md diff --git a/0664-strange-printer/NOTES.md b/0664-strange-printer/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0664-strange-printer/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 87114d097ecb6cce68b6f2a15defecfd95a27e37 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 22 Aug 2024 20:45:45 +0530 Subject: [PATCH 1384/1399] Create README - LeetHub --- 0476-number-complement/README.md | 33 ++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 0476-number-complement/README.md diff --git a/0476-number-complement/README.md b/0476-number-complement/README.md new file mode 100644 index 00000000..b46eec80 --- /dev/null +++ b/0476-number-complement/README.md @@ -0,0 +1,33 @@ +

476. Number Complement

Easy


The complement of an integer is the integer you get when you flip all the 0's to 1's and all the 1's to 0's in its binary representation.

+ +
    +
  • For example, The integer 5 is "101" in binary and its complement is "010" which is the integer 2.
  • +
+ +

Given an integer num, return its complement.

+ +

 

+

Example 1:

+ +
Input: num = 5
+Output: 2
+Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2.
+
+ +

Example 2:

+ +
Input: num = 1
+Output: 0
+Explanation: The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= num < 231
  • +
+ +

 

+

Note: This question is the same as 1009: https://leetcode.com/problems/complement-of-base-10-integer/

+
\ No newline at end of file From a596f65bed1faf880defdede492f3a10bad8e30a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Thu, 22 Aug 2024 20:45:49 +0530 Subject: [PATCH 1385/1399] Time: 3 ms (41.07%), Space: 7.6 MB (33.08%) - LeetHub --- .../0476-number-complement.cpp | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 0476-number-complement/0476-number-complement.cpp diff --git a/0476-number-complement/0476-number-complement.cpp b/0476-number-complement/0476-number-complement.cpp new file mode 100644 index 00000000..1670a955 --- /dev/null +++ b/0476-number-complement/0476-number-complement.cpp @@ -0,0 +1,23 @@ +class Solution { +public: + int findComplement(int num) { + + int ans = 0; + bool ok = false; + + for(int i = 31; i >= 0; --i) + { + if(!(num & (1 << i)) and ok) + { + ans += (1 << i); + } + + if(num & (1 << i)) + { + ok = true; + } + } + + return ans; + } +}; \ No newline at end of file From a3a5265fa8ab2f8aaf81ff442b7369573fa8977a Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 23 Aug 2024 22:28:18 +0530 Subject: [PATCH 1386/1399] Create README - LeetHub --- .../README.md | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 0592-fraction-addition-and-subtraction/README.md diff --git a/0592-fraction-addition-and-subtraction/README.md b/0592-fraction-addition-and-subtraction/README.md new file mode 100644 index 00000000..5d1558c5 --- /dev/null +++ b/0592-fraction-addition-and-subtraction/README.md @@ -0,0 +1,34 @@ +

592. Fraction Addition and Subtraction

Medium


Given a string expression representing an expression of fraction addition and subtraction, return the calculation result in string format.

+ +

The final result should be an irreducible fraction. If your final result is an integer, change it to the format of a fraction that has a denominator 1. So in this case, 2 should be converted to 2/1.

+ +

 

+

Example 1:

+ +
Input: expression = "-1/2+1/2"
+Output: "0/1"
+
+ +

Example 2:

+ +
Input: expression = "-1/2+1/2+1/3"
+Output: "1/3"
+
+ +

Example 3:

+ +
Input: expression = "1/3-1/2"
+Output: "-1/6"
+
+ +

 

+

Constraints:

+ +
    +
  • The input string only contains '0' to '9', '/', '+' and '-'. So does the output.
  • +
  • Each fraction (input and output) has the format ±numerator/denominator. If the first input fraction or the output is positive, then '+' will be omitted.
  • +
  • The input only contains valid irreducible fractions, where the numerator and denominator of each fraction will always be in the range [1, 10]. If the denominator is 1, it means this fraction is actually an integer in a fraction format defined above.
  • +
  • The number of given fractions will be in the range [1, 10].
  • +
  • The numerator and denominator of the final result are guaranteed to be valid and in the range of 32-bit int.
  • +
+
\ No newline at end of file From 9d0a5ea92df9b7d020ebcc2cc4040cf6a4a71833 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Fri, 23 Aug 2024 22:28:21 +0530 Subject: [PATCH 1387/1399] Time: 3 ms (50.39%), Space: 7.7 MB (58.91%) - LeetHub --- ...0592-fraction-addition-and-subtraction.cpp | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 0592-fraction-addition-and-subtraction/0592-fraction-addition-and-subtraction.cpp diff --git a/0592-fraction-addition-and-subtraction/0592-fraction-addition-and-subtraction.cpp b/0592-fraction-addition-and-subtraction/0592-fraction-addition-and-subtraction.cpp new file mode 100644 index 00000000..532239f0 --- /dev/null +++ b/0592-fraction-addition-and-subtraction/0592-fraction-addition-and-subtraction.cpp @@ -0,0 +1,47 @@ +class Solution { +public: + string fractionAddition(string expression) { + + int num = 0; + int den = 1; + + int i = 0, n = expression.size(); + + while(i < n) + { + int curNum = 0; + int curDen = 0; + + bool isNeg = (expression[i] == '-'); + + if(expression[i] == '+' or expression[i] == '-') + ++i; + + while(i < n && isdigit(expression[i])) + { + int val = expression[i] - '0'; + curNum = (curNum * 10) + val; + ++i; + } + + ++i; + if(isNeg) curNum *= -1; + + while(i < n && isdigit(expression[i])) + { + int val = expression[i] - '0'; + curDen = (curDen * 10) + val; + ++i; + } + + num = (num * curDen) + (curNum * den); + den = den * curDen; + } + + int gcd = abs(__gcd(num, den)); + num /= gcd; + den /= gcd; + + return to_string(num) + "/" + to_string(den); + } +}; \ No newline at end of file From ebabe8276ba152993921a703151f1f842170e21d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 24 Aug 2024 21:53:25 +0530 Subject: [PATCH 1388/1399] Create README - LeetHub --- 0564-find-the-closest-palindrome/README.md | 28 ++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 0564-find-the-closest-palindrome/README.md diff --git a/0564-find-the-closest-palindrome/README.md b/0564-find-the-closest-palindrome/README.md new file mode 100644 index 00000000..a5b4ec14 --- /dev/null +++ b/0564-find-the-closest-palindrome/README.md @@ -0,0 +1,28 @@ +

564. Find the Closest Palindrome

Hard


Given a string n representing an integer, return the closest integer (not including itself), which is a palindrome. If there is a tie, return the smaller one.

+ +

The closest is defined as the absolute difference minimized between two integers.

+ +

 

+

Example 1:

+ +
Input: n = "123"
+Output: "121"
+
+ +

Example 2:

+ +
Input: n = "1"
+Output: "0"
+Explanation: 0 and 2 are the closest palindromes but we return the smallest which is 0.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n.length <= 18
  • +
  • n consists of only digits.
  • +
  • n does not have leading zeros.
  • +
  • n is representing an integer in the range [1, 1018 - 1].
  • +
+
\ No newline at end of file From 3e226f1b9b29fe00fa2068e510a42e6d15d6679d Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 24 Aug 2024 21:53:28 +0530 Subject: [PATCH 1389/1399] Time: 0 ms (100.00%), Space: 8.3 MB (77.93%) - LeetHub --- .../0564-find-the-closest-palindrome.cpp | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 0564-find-the-closest-palindrome/0564-find-the-closest-palindrome.cpp diff --git a/0564-find-the-closest-palindrome/0564-find-the-closest-palindrome.cpp b/0564-find-the-closest-palindrome/0564-find-the-closest-palindrome.cpp new file mode 100644 index 00000000..0d79df33 --- /dev/null +++ b/0564-find-the-closest-palindrome/0564-find-the-closest-palindrome.cpp @@ -0,0 +1,61 @@ +using ll = long long; + +class Solution { +public: + + ll palindrome(ll firstHalf, bool isEven) + { + ll resultNum = firstHalf; + + if(!isEven) + firstHalf /= 10; + + while(firstHalf > 0) + { + resultNum = (resultNum * 10) + (firstHalf%10); + firstHalf /= 10; + } + + return resultNum; + } + + string nearestPalindromic(string n) { + + int sz = n.size(); + int mid = sz/2; + + ll firstHalfLen = (sz % 2 == 0 ? mid : mid+1); + + ll firstHalf = stoll(n.substr(0, firstHalfLen)); + + vector pos; + + pos.push_back(palindrome(firstHalf, sz % 2 == 0)); + pos.push_back(palindrome(firstHalf+1, sz % 2 == 0)); + pos.push_back(palindrome(firstHalf-1, sz % 2 == 0)); + pos.push_back(pow(10, sz-1) - 1); + pos.push_back(pow(10, sz) + 1); + + ll diff = LONG_MAX; + ll res = LONG_MAX; + ll orgNum = stoll(n); + + for(auto& num : pos) + { + if(num == orgNum) continue; + + if(abs(num - orgNum) < diff) + { + diff = abs(num - orgNum); + res = num; + } + else if(abs(num - orgNum) == diff) + { + res = min(res, num); + } + } + + return to_string(res); + + } +}; \ No newline at end of file From 54589be19d702fb408316e1d0fb2495d33710458 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 24 Aug 2024 21:53:38 +0530 Subject: [PATCH 1390/1399] Attach NOTES - LeetHub --- 0564-find-the-closest-palindrome/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0564-find-the-closest-palindrome/NOTES.md diff --git a/0564-find-the-closest-palindrome/NOTES.md b/0564-find-the-closest-palindrome/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0564-find-the-closest-palindrome/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 902a5917c19585f82df1931bf5ea0bd270e6d7bc Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Sat, 24 Aug 2024 21:53:41 +0530 Subject: [PATCH 1391/1399] Time: 0 ms (100.00%), Space: 8.3 MB (77.93%) - LeetHub From aab27573fd0c12dce1ba52bcb2effc0c7a5f1014 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 26 Aug 2024 20:59:47 +0530 Subject: [PATCH 1392/1399] Create README - LeetHub --- 0590-n-ary-tree-postorder-traversal/README.md | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 0590-n-ary-tree-postorder-traversal/README.md diff --git a/0590-n-ary-tree-postorder-traversal/README.md b/0590-n-ary-tree-postorder-traversal/README.md new file mode 100644 index 00000000..cee6f44d --- /dev/null +++ b/0590-n-ary-tree-postorder-traversal/README.md @@ -0,0 +1,29 @@ +

590. N-ary Tree Postorder Traversal

Easy


Given the root of an n-ary tree, return the postorder traversal of its nodes' values.

+ +

Nary-Tree input serialization is represented in their level order traversal. Each group of children is separated by the null value (See examples)

+ +

 

+

Example 1:

+ +
Input: root = [1,null,3,2,4,null,5,6]
+Output: [5,6,3,2,4,1]
+
+ +

Example 2:

+ +
Input: root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]
+Output: [2,6,14,11,7,3,12,8,4,13,9,10,5,1]
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is in the range [0, 104].
  • +
  • 0 <= Node.val <= 104
  • +
  • The height of the n-ary tree is less than or equal to 1000.
  • +
+ +

 

+

Follow up: Recursive solution is trivial, could you do it iteratively?

+
\ No newline at end of file From 627eef7c7f9de705355ece74b8b06575fa3d23af Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 26 Aug 2024 20:59:51 +0530 Subject: [PATCH 1393/1399] Time: 18 ms (22.62%), Space: 15.3 MB (49.47%) - LeetHub --- .../0590-n-ary-tree-postorder-traversal.cpp | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 0590-n-ary-tree-postorder-traversal/0590-n-ary-tree-postorder-traversal.cpp diff --git a/0590-n-ary-tree-postorder-traversal/0590-n-ary-tree-postorder-traversal.cpp b/0590-n-ary-tree-postorder-traversal/0590-n-ary-tree-postorder-traversal.cpp new file mode 100644 index 00000000..8267c9b4 --- /dev/null +++ b/0590-n-ary-tree-postorder-traversal/0590-n-ary-tree-postorder-traversal.cpp @@ -0,0 +1,47 @@ +/* +// Definition for a Node. +class Node { +public: + int val; + vector children; + + Node() {} + + Node(int _val) { + val = _val; + } + + Node(int _val, vector _children) { + val = _val; + children = _children; + } +}; +*/ + +class Solution { +public: + + void postorder(Node* root, vector& ans) + { + if(root) + { + for(auto& chlNode : root->children) + { + postorder(chlNode, ans); + ans.push_back(chlNode->val); + } + } + } + + vector postorder(Node* root) { + + vector ans; + + postorder(root, ans); + if(root) + ans.push_back(root->val); + + return ans; + + } +}; \ No newline at end of file From ff5e9be3885d29529448831df81c16863eb85fa0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 26 Aug 2024 21:00:34 +0530 Subject: [PATCH 1394/1399] Attach NOTES - LeetHub --- 0590-n-ary-tree-postorder-traversal/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0590-n-ary-tree-postorder-traversal/NOTES.md diff --git a/0590-n-ary-tree-postorder-traversal/NOTES.md b/0590-n-ary-tree-postorder-traversal/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/0590-n-ary-tree-postorder-traversal/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From ec0bd5381cecced2b68baf36c83c5a41875ada97 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Mon, 26 Aug 2024 21:00:37 +0530 Subject: [PATCH 1395/1399] Time: 14 ms (51.70%), Space: 15.2 MB (49.47%) - LeetHub --- .../0590-n-ary-tree-postorder-traversal.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/0590-n-ary-tree-postorder-traversal/0590-n-ary-tree-postorder-traversal.cpp b/0590-n-ary-tree-postorder-traversal/0590-n-ary-tree-postorder-traversal.cpp index 8267c9b4..fb8c1b94 100644 --- a/0590-n-ary-tree-postorder-traversal/0590-n-ary-tree-postorder-traversal.cpp +++ b/0590-n-ary-tree-postorder-traversal/0590-n-ary-tree-postorder-traversal.cpp @@ -28,8 +28,8 @@ class Solution { for(auto& chlNode : root->children) { postorder(chlNode, ans); - ans.push_back(chlNode->val); } + ans.push_back(root->val); } } @@ -38,9 +38,7 @@ class Solution { vector ans; postorder(root, ans); - if(root) - ans.push_back(root->val); - + return ans; } From 64b28717a2fb8a1c554fe7ab0872ec4bc0338221 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 28 Aug 2024 20:36:37 +0530 Subject: [PATCH 1396/1399] Create README - LeetHub --- 1905-count-sub-islands/README.md | 33 ++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 1905-count-sub-islands/README.md diff --git a/1905-count-sub-islands/README.md b/1905-count-sub-islands/README.md new file mode 100644 index 00000000..b76cb8e8 --- /dev/null +++ b/1905-count-sub-islands/README.md @@ -0,0 +1,33 @@ +

1905. Count Sub Islands

Medium


You are given two m x n binary matrices grid1 and grid2 containing only 0's (representing water) and 1's (representing land). An island is a group of 1's connected 4-directionally (horizontal or vertical). Any cells outside of the grid are considered water cells.

+ +

An island in grid2 is considered a sub-island if there is an island in grid1 that contains all the cells that make up this island in grid2.

+ +

Return the number of islands in grid2 that are considered sub-islands.

+ +

 

+

Example 1:

+ +
Input: grid1 = [[1,1,1,0,0],[0,1,1,1,1],[0,0,0,0,0],[1,0,0,0,0],[1,1,0,1,1]], grid2 = [[1,1,1,0,0],[0,0,1,1,1],[0,1,0,0,0],[1,0,1,1,0],[0,1,0,1,0]]
+Output: 3
+Explanation: In the picture above, the grid on the left is grid1 and the grid on the right is grid2.
+The 1s colored red in grid2 are those considered to be part of a sub-island. There are three sub-islands.
+
+ +

Example 2:

+ +
Input: grid1 = [[1,0,1,0,1],[1,1,1,1,1],[0,0,0,0,0],[1,1,1,1,1],[1,0,1,0,1]], grid2 = [[0,0,0,0,0],[1,1,1,1,1],[0,1,0,1,0],[0,1,0,1,0],[1,0,0,0,1]]
+Output: 2 
+Explanation: In the picture above, the grid on the left is grid1 and the grid on the right is grid2.
+The 1s colored red in grid2 are those considered to be part of a sub-island. There are two sub-islands.
+
+ +

 

+

Constraints:

+ +
    +
  • m == grid1.length == grid2.length
  • +
  • n == grid1[i].length == grid2[i].length
  • +
  • 1 <= m, n <= 500
  • +
  • grid1[i][j] and grid2[i][j] are either 0 or 1.
  • +
+
\ No newline at end of file From 67abafeb5a9c9f31d41361b6e1860f31e7af827c Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 28 Aug 2024 20:36:41 +0530 Subject: [PATCH 1397/1399] Time: 2756 ms (5.07%), Space: 118.6 MB (26.09%) - LeetHub --- .../1905-count-sub-islands.cpp | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 1905-count-sub-islands/1905-count-sub-islands.cpp diff --git a/1905-count-sub-islands/1905-count-sub-islands.cpp b/1905-count-sub-islands/1905-count-sub-islands.cpp new file mode 100644 index 00000000..20251889 --- /dev/null +++ b/1905-count-sub-islands/1905-count-sub-islands.cpp @@ -0,0 +1,69 @@ +class Solution { +public: + int countSubIslands(vector>& grid1, vector>& grid2) { + + int n = grid1.size(); + int m = grid1[0].size(); + + vector> visited(n, vector(m, false)), visited2(n, vector(m, false)); + + function dfs = [&](int i, int j) + { + if(i < 0 or j < 0 or i >= n or j >= m or visited[i][j] or !grid1[i][j]) + return; + + visited[i][j] = 1; + + dfs(i-1, j); + dfs(i, j+1); + dfs(i+1, j); + dfs(i, j-1); + }; + + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < m; ++j) + { + if(!visited[i][j] and grid1[i][j]) + dfs(i, j); + } + } + + function dfs2 = [&](int i, int j) -> bool{ + if(i < 0 or j < 0 or i >= n or j >= m or visited2[i][j] or !grid2[i][j]) + return true; + + if(!visited[i][j]) return false; + + visited2[i][j] = true; + + bool up, left, right, down; + up = left = right = down = true; + + up = dfs2(i-1, j); + left = dfs2(i, j-1); + down = dfs2(i+1, j); + right = dfs2(i, j+1); + + return up and left and down and right; + }; + + int subIsland = 0; + + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < m; ++j) + { + if(!visited2[i][j] and grid2[i][j]) + { + if(dfs2(i, j)) + { + ++subIsland; + } + } + } + } + + return subIsland; + } +}; \ No newline at end of file From 19280e2de1e68d6485bd939cae84476e037106d0 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 28 Aug 2024 20:38:46 +0530 Subject: [PATCH 1398/1399] Attach NOTES - LeetHub --- 1905-count-sub-islands/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 1905-count-sub-islands/NOTES.md diff --git a/1905-count-sub-islands/NOTES.md b/1905-count-sub-islands/NOTES.md new file mode 100644 index 00000000..38c1374a --- /dev/null +++ b/1905-count-sub-islands/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From 15a212cae2f0d824a4201777e399822daa4ec374 Mon Sep 17 00:00:00 2001 From: VISHAL JOSHI <85362504+knockcat@users.noreply.github.com> Date: Wed, 28 Aug 2024 20:38:50 +0530 Subject: [PATCH 1399/1399] Time: 604 ms (16.65%), Space: 115.6 MB (26.66%) - LeetHub --- .../1905-count-sub-islands.cpp | 44 +++++-------------- 1 file changed, 11 insertions(+), 33 deletions(-) diff --git a/1905-count-sub-islands/1905-count-sub-islands.cpp b/1905-count-sub-islands/1905-count-sub-islands.cpp index 20251889..bd5ccebd 100644 --- a/1905-count-sub-islands/1905-count-sub-islands.cpp +++ b/1905-count-sub-islands/1905-count-sub-islands.cpp @@ -5,45 +5,23 @@ class Solution { int n = grid1.size(); int m = grid1[0].size(); - vector> visited(n, vector(m, false)), visited2(n, vector(m, false)); + vector> visited(n, vector(m, false)); - function dfs = [&](int i, int j) - { - if(i < 0 or j < 0 or i >= n or j >= m or visited[i][j] or !grid1[i][j]) - return; - - visited[i][j] = 1; - - dfs(i-1, j); - dfs(i, j+1); - dfs(i+1, j); - dfs(i, j-1); - }; - - for(int i = 0; i < n; ++i) - { - for(int j = 0; j < m; ++j) - { - if(!visited[i][j] and grid1[i][j]) - dfs(i, j); - } - } - - function dfs2 = [&](int i, int j) -> bool{ - if(i < 0 or j < 0 or i >= n or j >= m or visited2[i][j] or !grid2[i][j]) + function dfs = [&](int i, int j) -> bool{ + if(i < 0 or j < 0 or i >= n or j >= m or visited[i][j] or !grid2[i][j]) return true; - if(!visited[i][j]) return false; + if(!grid1[i][j]) return false; - visited2[i][j] = true; + visited[i][j] = true; bool up, left, right, down; up = left = right = down = true; - up = dfs2(i-1, j); - left = dfs2(i, j-1); - down = dfs2(i+1, j); - right = dfs2(i, j+1); + up = dfs(i-1, j); + left = dfs(i, j-1); + down = dfs(i+1, j); + right = dfs(i, j+1); return up and left and down and right; }; @@ -54,9 +32,9 @@ class Solution { { for(int j = 0; j < m; ++j) { - if(!visited2[i][j] and grid2[i][j]) + if(!visited[i][j] and grid2[i][j]) { - if(dfs2(i, j)) + if(dfs(i, j)) { ++subIsland; }