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; +}