From dc1c8b557d15778b8c9dc6ead7dc566e64205a81 Mon Sep 17 00:00:00 2001 From: Yang Libin Date: Sat, 7 Oct 2023 07:18:40 +0000 Subject: [PATCH] feat: add ts solutions to lcp problems: No.11,12 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * LCP 11.期望个数统计 * LCP 12.小张刷题计划 --- .../README.md" | 9 +++++ .../Solution.ts" | 4 +++ .../README.md" | 33 +++++++++++++++++++ .../Solution.ts" | 28 ++++++++++++++++ 4 files changed, 74 insertions(+) create mode 100644 "lcp/LCP 11. \346\234\237\346\234\233\344\270\252\346\225\260\347\273\237\350\256\241/Solution.ts" create mode 100644 "lcp/LCP 12. \345\260\217\345\274\240\345\210\267\351\242\230\350\256\241\345\210\222/Solution.ts" diff --git "a/lcp/LCP 11. \346\234\237\346\234\233\344\270\252\346\225\260\347\273\237\350\256\241/README.md" "b/lcp/LCP 11. \346\234\237\346\234\233\344\270\252\346\225\260\347\273\237\350\256\241/README.md" index d6625e64781cc..f34e83330a833 100644 --- "a/lcp/LCP 11. \346\234\237\346\234\233\344\270\252\346\225\260\347\273\237\350\256\241/README.md" +++ "b/lcp/LCP 11. \346\234\237\346\234\233\344\270\252\346\225\260\347\273\237\350\256\241/README.md" @@ -111,6 +111,15 @@ func expectNumber(scores []int) int { } ``` +### **TypeScript** + +```ts +function expectNumber(scores: number[]): number { + const s: Set = new Set(scores); + return s.size; +} +``` + ### **...** ``` diff --git "a/lcp/LCP 11. \346\234\237\346\234\233\344\270\252\346\225\260\347\273\237\350\256\241/Solution.ts" "b/lcp/LCP 11. \346\234\237\346\234\233\344\270\252\346\225\260\347\273\237\350\256\241/Solution.ts" new file mode 100644 index 0000000000000..4a9c09c1e986f --- /dev/null +++ "b/lcp/LCP 11. \346\234\237\346\234\233\344\270\252\346\225\260\347\273\237\350\256\241/Solution.ts" @@ -0,0 +1,4 @@ +function expectNumber(scores: number[]): number { + const s: Set = new Set(scores); + return s.size; +} diff --git "a/lcp/LCP 12. \345\260\217\345\274\240\345\210\267\351\242\230\350\256\241\345\210\222/README.md" "b/lcp/LCP 12. \345\260\217\345\274\240\345\210\267\351\242\230\350\256\241\345\210\222/README.md" index 5c9228c26fba9..1e40f9bf64c55 100644 --- "a/lcp/LCP 12. \345\260\217\345\274\240\345\210\267\351\242\230\350\256\241\345\210\222/README.md" +++ "b/lcp/LCP 12. \345\260\217\345\274\240\345\210\267\351\242\230\350\256\241\345\210\222/README.md" @@ -186,6 +186,39 @@ func max(a, b int) int { } ``` +### **TypeScript** + +```ts +function minTime(time: number[], m: number): number { + let left = 0; + let right = time.reduce((a, b) => a + b); + const check = (t: number): boolean => { + let s = 0; + let mx = 0; + let d = 1; + for (const x of time) { + s += x; + mx = Math.max(mx, x); + if (s - mx > t) { + s = x; + mx = x; + d++; + } + } + return d <= m; + }; + while (left < right) { + const mid = (left + right) >> 1; + if (check(mid)) { + right = mid; + } else { + left = mid + 1; + } + } + return left; +} +``` + ### **...** ``` diff --git "a/lcp/LCP 12. \345\260\217\345\274\240\345\210\267\351\242\230\350\256\241\345\210\222/Solution.ts" "b/lcp/LCP 12. \345\260\217\345\274\240\345\210\267\351\242\230\350\256\241\345\210\222/Solution.ts" new file mode 100644 index 0000000000000..b9d54d692ecdf --- /dev/null +++ "b/lcp/LCP 12. \345\260\217\345\274\240\345\210\267\351\242\230\350\256\241\345\210\222/Solution.ts" @@ -0,0 +1,28 @@ +function minTime(time: number[], m: number): number { + let left = 0; + let right = time.reduce((a, b) => a + b); + const check = (t: number): boolean => { + let s = 0; + let mx = 0; + let d = 1; + for (const x of time) { + s += x; + mx = Math.max(mx, x); + if (s - mx > t) { + s = x; + mx = x; + d++; + } + } + return d <= m; + }; + while (left < right) { + const mid = (left + right) >> 1; + if (check(mid)) { + right = mid; + } else { + left = mid + 1; + } + } + return left; +}