From 937771421b4576ad05b7b02392653b9c244c94a0 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Mon, 21 Apr 2025 00:32:06 +0000 Subject: [PATCH] feat: add solutions to lc problem: No.3523 No.3523.Make Array Non-decreasing --- .../3523.Make Array Non-decreasing/README.md | 60 ++++++++++++++++++- .../README_EN.md | 60 ++++++++++++++++++- .../Solution.cpp | 13 ++++ .../Solution.go | 10 ++++ .../Solution.java | 12 ++++ .../Solution.py | 8 +++ .../Solution.ts | 10 ++++ 7 files changed, 167 insertions(+), 6 deletions(-) create mode 100644 solution/3500-3599/3523.Make Array Non-decreasing/Solution.cpp create mode 100644 solution/3500-3599/3523.Make Array Non-decreasing/Solution.go create mode 100644 solution/3500-3599/3523.Make Array Non-decreasing/Solution.java create mode 100644 solution/3500-3599/3523.Make Array Non-decreasing/Solution.py create mode 100644 solution/3500-3599/3523.Make Array Non-decreasing/Solution.ts diff --git a/solution/3500-3599/3523.Make Array Non-decreasing/README.md b/solution/3500-3599/3523.Make Array Non-decreasing/README.md index f5421dac6215f..1fd2bf86d8399 100644 --- a/solution/3500-3599/3523.Make Array Non-decreasing/README.md +++ b/solution/3500-3599/3523.Make Array Non-decreasing/README.md @@ -75,25 +75,79 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3523.Ma #### Python3 ```python - +class Solution: + def maximumPossibleSize(self, nums: List[int]) -> int: + ans = mx = 0 + for x in nums: + if mx <= x: + ans += 1 + mx = x + return ans ``` #### Java ```java - +class Solution { + public int maximumPossibleSize(int[] nums) { + int ans = 0, mx = 0; + for (int x : nums) { + if (mx <= x) { + ++ans; + mx = x; + } + } + return ans; + } +} ``` #### C++ ```cpp - +class Solution { +public: + int maximumPossibleSize(vector& nums) { + int ans = 0, mx = 0; + for (int x : nums) { + if (mx <= x) { + ++ans; + mx = x; + } + } + return ans; + } +}; ``` #### Go ```go +func maximumPossibleSize(nums []int) int { + ans, mx := 0, 0 + for _, x := range nums { + if mx <= x { + ans++ + mx = x + } + } + return ans +} +``` +#### TypeScript + +```ts +function maximumPossibleSize(nums: number[]): number { + let [ans, mx] = [0, 0]; + for (const x of nums) { + if (mx <= x) { + ++ans; + mx = x; + } + } + return ans; +} ``` diff --git a/solution/3500-3599/3523.Make Array Non-decreasing/README_EN.md b/solution/3500-3599/3523.Make Array Non-decreasing/README_EN.md index c95960128c593..edb3315477ba6 100644 --- a/solution/3500-3599/3523.Make Array Non-decreasing/README_EN.md +++ b/solution/3500-3599/3523.Make Array Non-decreasing/README_EN.md @@ -71,25 +71,79 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3523.Ma #### Python3 ```python - +class Solution: + def maximumPossibleSize(self, nums: List[int]) -> int: + ans = mx = 0 + for x in nums: + if mx <= x: + ans += 1 + mx = x + return ans ``` #### Java ```java - +class Solution { + public int maximumPossibleSize(int[] nums) { + int ans = 0, mx = 0; + for (int x : nums) { + if (mx <= x) { + ++ans; + mx = x; + } + } + return ans; + } +} ``` #### C++ ```cpp - +class Solution { +public: + int maximumPossibleSize(vector& nums) { + int ans = 0, mx = 0; + for (int x : nums) { + if (mx <= x) { + ++ans; + mx = x; + } + } + return ans; + } +}; ``` #### Go ```go +func maximumPossibleSize(nums []int) int { + ans, mx := 0, 0 + for _, x := range nums { + if mx <= x { + ans++ + mx = x + } + } + return ans +} +``` +#### TypeScript + +```ts +function maximumPossibleSize(nums: number[]): number { + let [ans, mx] = [0, 0]; + for (const x of nums) { + if (mx <= x) { + ++ans; + mx = x; + } + } + return ans; +} ``` diff --git a/solution/3500-3599/3523.Make Array Non-decreasing/Solution.cpp b/solution/3500-3599/3523.Make Array Non-decreasing/Solution.cpp new file mode 100644 index 0000000000000..344464669c244 --- /dev/null +++ b/solution/3500-3599/3523.Make Array Non-decreasing/Solution.cpp @@ -0,0 +1,13 @@ +class Solution { +public: + int maximumPossibleSize(vector& nums) { + int ans = 0, mx = 0; + for (int x : nums) { + if (mx <= x) { + ++ans; + mx = x; + } + } + return ans; + } +}; \ No newline at end of file diff --git a/solution/3500-3599/3523.Make Array Non-decreasing/Solution.go b/solution/3500-3599/3523.Make Array Non-decreasing/Solution.go new file mode 100644 index 0000000000000..743e0e42f7a80 --- /dev/null +++ b/solution/3500-3599/3523.Make Array Non-decreasing/Solution.go @@ -0,0 +1,10 @@ +func maximumPossibleSize(nums []int) int { + ans, mx := 0, 0 + for _, x := range nums { + if mx <= x { + ans++ + mx = x + } + } + return ans +} \ No newline at end of file diff --git a/solution/3500-3599/3523.Make Array Non-decreasing/Solution.java b/solution/3500-3599/3523.Make Array Non-decreasing/Solution.java new file mode 100644 index 0000000000000..5632dcc86bdd3 --- /dev/null +++ b/solution/3500-3599/3523.Make Array Non-decreasing/Solution.java @@ -0,0 +1,12 @@ +class Solution { + public int maximumPossibleSize(int[] nums) { + int ans = 0, mx = 0; + for (int x : nums) { + if (mx <= x) { + ++ans; + mx = x; + } + } + return ans; + } +} \ No newline at end of file diff --git a/solution/3500-3599/3523.Make Array Non-decreasing/Solution.py b/solution/3500-3599/3523.Make Array Non-decreasing/Solution.py new file mode 100644 index 0000000000000..d09f556678554 --- /dev/null +++ b/solution/3500-3599/3523.Make Array Non-decreasing/Solution.py @@ -0,0 +1,8 @@ +class Solution: + def maximumPossibleSize(self, nums: List[int]) -> int: + ans = mx = 0 + for x in nums: + if mx <= x: + ans += 1 + mx = x + return ans diff --git a/solution/3500-3599/3523.Make Array Non-decreasing/Solution.ts b/solution/3500-3599/3523.Make Array Non-decreasing/Solution.ts new file mode 100644 index 0000000000000..3673391995f32 --- /dev/null +++ b/solution/3500-3599/3523.Make Array Non-decreasing/Solution.ts @@ -0,0 +1,10 @@ +function maximumPossibleSize(nums: number[]): number { + let [ans, mx] = [0, 0]; + for (const x of nums) { + if (mx <= x) { + ++ans; + mx = x; + } + } + return ans; +}