From caba747303c29ff59c6248b998fe1e3469b0c88d Mon Sep 17 00:00:00 2001
From: Cool <747682928@qq.com>
Date: Tue, 29 Oct 2024 19:31:03 +0800
Subject: [PATCH] =?UTF-8?q?2024/10/29=20=E7=81=B5=E8=8C=B6=E9=A2=98?=
 =?UTF-8?q?=E5=8D=95=20=E6=BB=91=E5=8A=A8=E7=AA=97=E5=8F=A3=20=E6=9C=80?=
 =?UTF-8?q?=E7=9F=AD/=E6=9C=80=E5=B0=8F?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../sliding_windows/Num209.java               | 26 +++++
 .../sliding_windows/Num2904.java              | 97 +++++++++++++++++++
 2 files changed, 123 insertions(+)
 create mode 100644 src/main/java/com/cool/ling_cha_mount/sliding_windows/Num209.java
 create mode 100644 src/main/java/com/cool/ling_cha_mount/sliding_windows/Num2904.java

diff --git a/src/main/java/com/cool/ling_cha_mount/sliding_windows/Num209.java b/src/main/java/com/cool/ling_cha_mount/sliding_windows/Num209.java
new file mode 100644
index 0000000..6a16060
--- /dev/null
+++ b/src/main/java/com/cool/ling_cha_mount/sliding_windows/Num209.java
@@ -0,0 +1,26 @@
+package com.cool.ling_cha_mount.sliding_windows;
+
+/**
+ * Created with IntelliJ IDEA.
+ *
+ * @Author: Cool
+ * @Date: 2024/10/29/17:09
+ * @Description: 209. 长度最小的子数组
+ *
+ */
+public class Num209 {
+    public int minSubArrayLen(int target, int[] nums) {
+        int res = nums.length + 1;
+        int left = 0;
+        int sum = 0;
+        for (int i = 0; i < nums.length; i++) {
+            sum += nums[i];
+            while (sum >= target) {
+                res = Math.min(res, i - left + 1);
+                sum -= nums[left];
+                left++;
+            }
+        }
+        return res > nums.length ? 0 : res;
+    }
+}
diff --git a/src/main/java/com/cool/ling_cha_mount/sliding_windows/Num2904.java b/src/main/java/com/cool/ling_cha_mount/sliding_windows/Num2904.java
new file mode 100644
index 0000000..bf586e8
--- /dev/null
+++ b/src/main/java/com/cool/ling_cha_mount/sliding_windows/Num2904.java
@@ -0,0 +1,97 @@
+package com.cool.ling_cha_mount.sliding_windows;
+
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Created with IntelliJ IDEA.
+ *
+ * @Author: Cool
+ * @Date: 2024/10/29/19:19
+ * @Description: 2904. 最短且字典序最小的美丽子字符串
+ */
+public class Num2904 {
+    public String shortestBeautifulSubstring(String s, int k) {
+        int left = 0;
+        int right = 0;
+        String res = "";
+        char[] chars = s.toCharArray();
+        while (left <= right && right < chars.length) {
+            if (chars[right] == '1') {
+                k--;
+            }
+            if (k == 0) {
+                while (chars[left] != '1') {
+                    left++;
+                }
+                if (res.isEmpty()) {
+                    res = s.substring(left, right + 1);
+                } else {
+                    if (res.length() > right - left + 1) {
+                        res = s.substring(left, right + 1);
+                    } else if (res.length() == right - left + 1) {
+                        String ss = s.substring(left, right + 1);
+                        if (res.compareTo(ss) > 0) {
+                            res = ss;
+                        }
+                    }
+                }
+
+                left++;
+                k++;
+            }
+            right++;
+        }
+        return res;
+    }
+    public String shortestBeautifulSubstring1(String s, int k) {
+        int left = 0;
+        int right = 0;
+        List<int[]> list = new ArrayList<>();
+        char[] chars = s.toCharArray();
+        while (left <= right && right < chars.length) {
+            if (chars[right] == '1') {
+                k--;
+            }
+            if (k == 0) {
+                while (chars[left] != '1') {
+                    left++;
+                }
+                if (list.isEmpty()) {
+                    list.add(new int[]{left, right});
+                } else {
+                    int[] arr = list.get(0);
+                    if ((arr[1] - arr[0]) > right - left) {
+                        list.clear();
+                        list.add(new int[]{left, right});
+                    } else if ((arr[1] - arr[0]) == right - left) {
+                        list.add(new int[]{left, right});
+                    }
+                }
+
+                left++;
+                k++;
+            }
+            right++;
+        }
+        if (list.isEmpty()) {
+            return "";
+        }
+        String ans = s.substring(list.get(0)[0], list.get(0)[1] + 1);
+        for (int i = 1; i < list.size(); i++) {
+            int[] arr = list.get(i);
+            String ss = s.substring(arr[0], arr[1] + 1);
+            if (ss.compareTo(ans) < 0) {
+                ans = ss;
+            }
+        }
+        return ans;
+    }
+
+    @Test
+    public void test() {
+        shortestBeautifulSubstring("100011001", 3);
+    }
+}