├── .gitignore ├── LICENSE ├── README.md ├── codes ├── choice100 │ ├── 001_AI-Processor-Combination.py │ ├── 002_task-execute-total-time.py │ ├── 003_count-outstanding-students.py │ ├── 004_PV-site-construction-plan.py │ ├── 005_anonymous-letter.py │ ├── 006_reverse-words.py │ ├── 007_base-station-maintenance-engineer.py │ ├── 008_find-duplicate-codes.py │ ├── 009_decrypt-string.py │ ├── 010_rearrange-string.py │ ├── 011_symmetric-string.py │ ├── 012_find-important-keys.py │ ├── 013_find-key.py │ ├── 014_find-the-middle-node-of-the-linked-list.py │ ├── 015_have-fun.py │ ├── 016_abnormal-attendance-records.py │ ├── 017_integration-testing-of-microservices.py │ ├── 018_print-file.py │ ├── 019_count-vehicle-colors.py │ ├── 020_find-number.py │ ├── 021_shooting-competition.py │ ├── 022_the-middle-position-of-array.py │ ├── 023_adjust-poker.py │ ├── 024_seat-assignment.py │ ├── 025_choose-location-of-the-new-school.py │ ├── 026_log-collection-system.py │ ├── 027_star-basketball-competition.py │ ├── 028_maximum-number-of-substrings-extracted.py │ ├── 029_max-salary.py │ ├── 030_minimum-applicator-energy-efficiency.py │ ├── 031_leftmost-redundant-overlay-substring.py │ ├── 032_min-board-length.py │ ├── 033_find-charging-device-combinations.py │ ├── 034_finds-a-single-entry-free-area.py │ ├── 035_specific-double-ended-que.py │ ├── 036_riddles.py │ ├── 037_block-games-with-the-same-number.py │ ├── 038_knowledge-graph-neologism-mining.py │ ├── 039_car-rental-ride-Green-Island.py │ ├── 040_port-merging.py │ ├── 041_the-boxes-are-arranged-in-a-zigzag-pattern.py │ ├── 042_Online-mall-promotions-1.py │ ├── 043_automatic-exposure.py │ ├── 044_obtain-the-maximum-software-version-number.py │ ├── 045_get-the-perfect-position.py │ ├── 046_currency-unit-conversion.py │ ├── 047_greedy-businessmen-maximum-profit.py │ ├── 048_bit-error-rate.py │ ├── 049_static-scanning-optimal-cost.py │ └── 050_hotel-reservation.py ├── others100 │ ├── 001_AI-recognition-panel.py │ ├── 002_GPU-scheduling.py │ ├── 003_convert-IPv4-addresses-to-integers.py │ ├── 004_n-decimal-subtraction.py │ ├── 005_rsa-encryption-algorithm.py │ ├── 006_tlv-code.py │ ├── 007_vlan-resource-pool.py │ ├── 008_we-are-a-team.py │ ├── 009_inequality.py │ ├── 010_product-maximum-value.py │ ├── 011_nine-cell-key-input.py │ ├── 012_minimum-absolute-sum-of-two-numbers-in-an-out-of-order-integer-sequence.py │ ├── 013_event-push.py │ ├── 014_five-key-keyboard.py │ ├── 015_swap-characters.py │ ├── 016_sites-with-the-most-people.py │ ├── 017_the-most-popular-store.py │ ├── 018_task-scheduling.py │ ├── 019_the-MELON-conundrum.py │ ├── 020_tug-of-war-competition.py │ ├── 021_LISP-like-computation.py │ ├── 022_mode-and-median.py │ ├── 023_find-max-distance.py │ ├── 024_code-editor.py │ ├── 025_push-and-pop.py │ ├── 026_related-string.py │ ├── 027_memory-pool.py │ ├── 028_taxi-fare.py │ ├── 029_faulty-or-gate-circuit.py │ ├── 030_sharing-blocks.py │ ├── 031_sharing-candies.py │ ├── 032_sharing-apples.py │ ├── 033_find-prime-factors.py │ ├── 034_remove-a-specified-directory.py │ ├── 035_remove-minimum-characters.py │ ├── 036_maximum-value-after-deletion.py │ ├── 037_identify-poker.py │ ├── 038_remaining-available-character-set.py │ ├── 039_pythagorean-triple.py │ ├── 040_reverse-words.py │ ├── 041_gomoku-fans.py │ ├── 042_words-chain.py │ ├── 043_max-cards-number.py │ ├── 044_decode-compressed-string.py │ ├── 045_sum-number.py │ ├── 046_double-eleven.py │ ├── 047_rearrange-books.py │ ├── 048_hot-pot.py │ ├── 049_the-boundary-value-of-the-matrix-element.py │ ├── 050_highway-charging-planning.py │ ├── 051_employee-attendance.py │ ├── 052_respond-message-time.py │ ├── 053_take-the-confidential-elevator.py │ ├── 054_shout-seven.py │ ├── 055_good-friend.py │ ├── 056_the-length-of-substring.py │ ├── 057_letter-elimination.py │ ├── 058_letter-combination.py │ ├── 059_letter-counts.py │ ├── 060_string-encryption.py │ ├── 061_string-abstract.py │ ├── 062_string-match.py │ ├── 063_escape-room-game.py │ ├── 064_find-max-value.py │ ├── 065_determine-the-string-subsequence.py │ ├── 066_find-path.py │ ├── 067_search-continuous-scope.py │ ├── 068_shot-competition.py │ ├── 069_children-order.py │ ├── 070_broadcast-server.py │ ├── 071_seat-adjustment.py │ ├── 072_express-truck.py │ ├── 073_quick-construct-station.py │ ├── 074_total-fastest-detection-efficiency.py │ ├── 075_recover-array.py │ ├── 076_discount-fruit.py │ ├── 077_find-co-occurrence-number.py │ ├── 078_find-same-class-children.py │ ├── 079_find-it.py │ ├── 080_find-word.py │ ├── 081_find-city.py │ ├── 082_find-characters.py │ ├── 083_find-number.py │ ├── 084_find-the-minimum-number.py │ ├── 085_find-the-endpoint.py │ ├── 086_sound-off.py │ ├── 087_sound-off-game.py │ ├── 088_recruitment.py │ ├── 089_check-parentheses.py │ ├── 090_stitch-URLs.py │ ├── 091_replace-binary-trees-by-path.py │ ├── 092_pick-the-string.py │ ├── 093_the-priority-queue.py │ ├── 094_sensitive-field-encryption.py │ ├── 095_sequence-restore.py │ ├── 096_count-the-wild-geese.py │ ├── 097_the-numbers-game.py │ ├── 098_the-arrangement-of-numbers.py │ ├── 099_data-classification.py │ ├── 100_queuing-games.py │ ├── 101_the-most-economical-way-to-back-up-data.py │ ├── 102_array-merge.py │ ├── 103_array-sort.py │ ├── 104_array-make-up-minimum-number.py │ ├── 105_integer-factorization.py │ ├── 106_minimum-sum-of-integers.py │ ├── 107_integer-code.py │ ├── 108_file-directory-size.py │ ├── 109_doudizhu-1.py │ ├── 110_doudizhu-2.py │ ├── 111_new-job-number-system.py │ ├── 112_spin-dice.py │ ├── 113_time-format.py │ ├── 114_mingming's-random-number.py │ ├── 115_optimal-scheduling-policy.py │ ├── 116_lowest-order.py │ ├── 117_optimal-plant-distance-for-planting-trees.py │ ├── 118_the-largest-submatrix.py │ ├── 119_maximum-permutation.py │ ├── 119_the-N-permutation.py │ ├── 120_maximum-return-stock-return-problem.py │ ├── 121_maximum-connection-number-of-boys.py │ ├── 122_minimal-delivery-delay.py │ ├── 123_minimum-leaf-node.py │ ├── 124_minimum-number-of-steps.py │ ├── 125_minimum-number-of-exchanges.py │ ├── 126_minimum-number-of-stoped-cars.py │ ├── 127_the-least-time.py │ ├── 128_the-nearest-hospital.py │ ├── 129_the-nearest-point.py │ ├── 130_the-longest-common-suffix.py │ ├── 131_the-longest-legal-expression.py │ ├── 132_the-longest-vowel-string.py │ ├── 133_the-longest-continuous-alternating-square-wave-signal.py │ ├── 134_the-longest-consecutive-substring.py │ ├── 135_valid-substring.py │ ├── 136_service-dependencies.py │ ├── 137_server-power-consumption.py │ ├── 138_robots-walk-mazes.py │ ├── 139_witty-delivery-boy.py │ ├── 140_number-of-squares.py │ ├── 141_tree-structured-queries.py │ ├── 142_dictionary-reference.py │ ├── 143_hot-topics-and-word-sorting.py │ ├── 144_champions-and-runners-up-in-competition.py │ ├── 145_competition-rating.py │ ├── 146_narcissistic-number.py │ ├── 147_narcissistic-number2.py │ ├── 148_minimum-sum-of-integers.py │ ├── 149_k-elements-sum.py │ ├── 150_consecutive-number.py │ ├── 151_caving.py │ ├── 152_pipeline.py │ ├── 153_sliding-window.py │ ├── 154_mars-transformation.py │ ├── 155_martian_compute.py │ ├── 156_CBT-non-leaf-parts-post-order-traversal.py │ ├── 157_hostpot-network-statistics.py │ ├── 158_guess-password.py │ ├── 159_monkey-climbing-the-mountain.py │ ├── 160_card-player.py │ ├── 161_longest-substring-in-circle.py │ ├── 162_copy-the-files-from-the-floppy-disk.py │ ├── 163_virus-infection.py │ ├── 164_max-number-of-consecutive-occurrences-of-the-same-char.py │ ├── 165_relative-open-syllable.py │ ├── 166_the-area-for-intersect-rectangles.py │ ├── 167_matrix-sparse-scan.py │ ├── 168_disk-capacity.py │ ├── 169_weighing-weights.py │ ├── 170_vertical-quad.py │ ├── 171_qualified-substring-length.py │ ├── 172_the-letter-of-the-k-th-min-code-value.py │ ├── 173-the-k-th-longest-string.py │ ├── 174_simple-memory-battery.py │ ├── 175_simple-unzip.py │ ├── 176_basketball-match.py │ ├── 177_screen-saver.py │ ├── 178_draw-machine.py │ ├── 179_meet-rules-combination.py │ ├── 180_statistical-monitoring.py │ ├── 181_surviving-on-a-desert-island.py │ ├── 182_archaeologist.py │ ├── 183_dinner-venue.py │ ├── 184_team-up-by-ability.py │ ├── 185_spacecraft.py │ ├── 186_english-input.py │ ├── 187_get-the-most-food.py │ ├── 188_replanting-of-unlived-poplar-trees.py │ ├── 189_watch-the-performance.py │ ├── 190_demystify-the-time-of-the-crime.py │ ├── 191_calculate-the-nearest-number.py │ ├── 192_comment-convert-output.py │ ├── 193_super-mario-crosses-the-suspension-bridge.py │ ├── 194_street-lighting.py │ ├── 195_peevers-2.py │ ├── 196_jump-the-grid-1.py │ ├── 197_jump-the-grid-2.py │ ├── 198_kick-stones.py │ ├── 199_height-sorting.py │ ├── 200_height-and-weight-ordering.py │ ├── 201_interscholasticmeet.py │ ├── 202_continuous-substrings.py │ ├── 203_consecutive-letter-length.py │ ├── 204_elective-courses.py │ ├── 205_choose-the-seat.py │ ├── 206_soldier-assault.py │ ├── 207_reassemble-the-string.py │ ├── 208_Alibaba-looks-for-gold-treasure-chests-2.py │ ├── 209_Alibaba-looks-for-gold-treasure-chests-3.py │ ├── 210_Alibaba-looks-for-gold-treasure-chests-4.py │ ├── 211_Alibaba-looks-for-gold-treasure-chests-5.py │ ├── 212_collect-five-blessing-cards.py │ ├── 213_the-number-of-unbroadcast-servers.py │ ├── 214_non-strictly-increment-sequence.py │ ├── 215_canteen-feeding-speed.py │ ├── 216_efficient-task-plan.py │ ├── 217_Alibaba-looks-for-gold-treasure-chests-1.py │ ├── 218_colorize-the-blackboard.py │ └── 219_numeric-sequence-comparison.py └── questions200 │ ├── 001_excel-cell-value-statistics.py │ ├── 002_the-number-of-Linux-distributions.py │ ├── 003_the-road-to-work.py │ ├── 004_number-without-101.py │ ├── 005_Cloud-SMS-platform-promotional-activity.py │ ├── 006_task-hybridization.py │ ├── 007_preferred-nucleic-acid-testing-points.py │ ├── 008_elegant-array.py │ ├── 009_signal-transmission-and-reception.py │ ├── 010_maximum-subarray-of-geometric-mean.py │ ├── 011_bonus-distribution.py │ ├── 012_blockchain-file-dumping-system.py │ ├── 013_interval-connector.py │ ├── 014_removing-extra-whitespace.py │ ├── 015_intercepting-spam.py │ ├── 016_soldiers-crossing-the-river.py │ ├── 017_natural-reservoirs.py │ ├── 018_finding-similar-words.py │ ├── 019_finding-the-longest-substring-meeting-requirements.py │ ├── 020_open-day-activity.py │ ├── 021_express-delivery-station.py │ ├── 022_express-delivery-allocation-problem.py │ ├── 023_searching-land-plot-establishment.py │ ├── 024_number-addition-and-subtraction-game.py │ ├── 025_undirected-graph-coloring-problem.py │ ├── 026_optimal-resource-allocation.py │ ├── 027_best-opponent.py │ ├── 028_at-most-how-many-right-triangles.py │ ├── 029_max-count-of-equal-and-disjoint-contiguous-subsequences.py │ ├── 030_min-number-of-segments-covered.py │ ├── 031_worst-product-award.py │ ├── 032_best-location-for-service-center.py │ ├── 033_robot-movement-area.py │ ├── 034_cabinets-layout.py │ ├── 035_search-elements-in-the-tree.py │ ├── 036_find-the-largest-number.py │ ├── 037_wolf-sheep-crossing-the-river.py │ ├── 038_hardware-product-sales-plan.py │ ├── 039_combine-the-legal-minimum.py │ ├── 040_assemble-new-array.py │ ├── 041_min-number-of-uniform-weight-limit.py │ ├── 042_count-the-number-of-matching-pairs.py │ ├── 043_comparison-of-similarities-and-differences.py │ ├── 044_decompression-algorithm.py │ ├── 045_calculate-network-signal.py │ ├── 046_increment-string.py │ ├── 047_divide-the-string.py │ ├── 048_servers-that-can-make-up-a-network.py │ ├── 049_enhanced-strstr.py │ ├── 050_shopping.py │ └── 051_print-task-sequencing.py ├── docs ├── .nojekyll ├── README.md ├── _sidebar.md ├── choice100 │ ├── 001_AI-Processor-Combination.md │ ├── 002_task-execute-total-time.md │ ├── 003_count-outstanding-students.md │ ├── 004_PV-site-construction-plan.md │ ├── 005_anonymous-letter.md │ ├── 006_reverse-words.md │ ├── 007_base-station-maintenance-engineer.md │ ├── 008_find-duplicate-codes.md │ ├── 009_decrypt-string.md │ ├── 010_rearrange-string.md │ ├── 011_symmetric-string.md │ ├── 012_find-important-keys.md │ ├── 013_find-key.md │ ├── 014_find-the-middle-node-of-the-linked-list.md │ ├── 015_have-fun.md │ ├── 016_abnormal-attendance-records.md │ ├── 017_integration-testing-of-microservices.md │ ├── 018_print-file.md │ ├── 019_count-vehicle-colors.md │ ├── 020_find-number.md │ ├── 021_shooting-competition.md │ ├── 022_the-middle-position-of-array.md │ ├── 023_adjust-poker.md │ ├── 024_seat-assignment.md │ ├── 025_choose-location-of-the-new-school.md │ ├── 026_log-collection-system.md │ ├── 027_star-basketball-competition.md │ ├── 028_maximum-number-of-substrings-extracted.md │ ├── 029_max-salary.md │ ├── 030_minimum-applicator-energy-efficiency.md │ ├── 031_leftmost-redundant-overlay-substring.md │ ├── 032_min-board-length.md │ ├── 033_find-charging-device-combinations.md │ ├── 034_finds-a-single-entry-free-area.md │ ├── 035_specific-double-ended-que.md │ ├── 036_riddles.md │ ├── 037_block-games-with-the-same-number.md │ ├── 038_knowledge-graph-neologism-mining.md │ ├── 039_car-rental-ride-Green-Island.md │ ├── 040_port-merging.md │ ├── 041_the-boxes-are-arranged-in-a-zigzag-pattern.md │ ├── 042_Online-mall-promotions-1.md │ ├── 043_automatic-exposure.md │ ├── 044_obtain-the-maximum-software-version-number.md │ ├── 045_get-the-perfect-position.md │ ├── 046_currency-unit-conversion.md │ ├── 047_greedy-businessmen-maximum-profit.md │ ├── 048_bit-error-rate.md │ ├── 049_static-scanning-optimal-cost.md │ ├── 050_hotel-reservation.md │ └── images │ │ └── 041-001-zigzag.png ├── images │ └── qrcode.jpeg ├── index.html ├── others100 │ ├── 001_AI-recognition-panel.md │ ├── 002_GPU-scheduling.md │ ├── 003_convert-IPv4-addresses-to-integers.md │ ├── 004_n-decimal-subtraction.md │ ├── 005_rsa-encryption-algorithm.md │ ├── 006_tlv-code.md │ ├── 007_vlan-resource-pool.md │ ├── 008_we-are-a-team.md │ ├── 009_inequality.md │ ├── 010_product-maximum-value.md │ ├── 011_nine-cell-key-input.md │ ├── 012_minimum-absolute-sum-of-two-numbers-in-an-out-of-order-integer-sequence.md │ ├── 013_event-push.md │ ├── 014_five-key-keyboard.md │ ├── 015_swap-characters.md │ ├── 016_sites-with-the-most-people.md │ ├── 017_the-most-popular-store.md │ ├── 018_task-scheduling.md │ ├── 019_the-MELON-conundrum.md │ ├── 020_tug-of-war-competition.md │ ├── 021_LISP-like-computation.md │ ├── 022_mode-and-median.md │ ├── 023_find-max-distance.md │ ├── 024_code-editor.md │ ├── 025_push-and-pop.md │ ├── 026_related-string.md │ ├── 027_memory-pool.md │ ├── 028_taxi-fare.md │ ├── 029_faulty-or-gate-circuit.md │ ├── 030_sharing-blocks.md │ ├── 031_sharing-candies.md │ ├── 032_sharing-apples.md │ ├── 033_find-prime-factors.md │ ├── 034_remove-a-specified-directory.md │ ├── 035_remove-minimum-characters.md │ ├── 036_maximum-value-after-deletion.md │ ├── 037_identify-poker.md │ ├── 038_remaining-available-character-set.md │ ├── 039_pythagorean-triple.md │ ├── 040_reverse-words.md │ ├── 041_gomoku-fans.md │ ├── 042_words-chain.md │ ├── 043_max-cards-number.md │ ├── 044_decode-compressed-string.md │ ├── 045_sum-number.md │ ├── 046_double-eleven.md │ ├── 047_rearrange-books.md │ ├── 048_hot-pot.md │ ├── 049_the-boundary-value-of-the-matrix-element.md │ ├── 050_highway-charging-planning.md │ ├── 051_employee-attendance.md │ ├── 052_respond-message-time.md │ ├── 053_take-the-confidential-elevator.md │ ├── 054_shout-seven.md │ ├── 055_good-friend.md │ ├── 056_the-length-of-substring.md │ ├── 057_letter-elimination.md │ ├── 058_letter-combination.md │ ├── 059_letter-counts.md │ ├── 060_string-encryption.md │ ├── 061_string-abstract.md │ ├── 062_string-match.md │ ├── 063_escape-room-game.md │ ├── 064_find-max-value.md │ ├── 065_determine-the-string-subsequence.md │ ├── 066_find-path.md │ ├── 067_search-continuous-scope.md │ ├── 068_shot-competition.md │ ├── 069_children-order.md │ ├── 070_broadcast-server.md │ ├── 071_seat-adjustment.md │ ├── 072_express-truck.md │ ├── 073_quick-construct-station.md │ ├── 074_total-fastest-detection-efficiency.md │ ├── 075_recover-array.md │ ├── 076_discount-fruit.md │ ├── 077_find-co-occurrence-number.md │ ├── 078_find-same-class-children.md │ ├── 079_find-it.md │ ├── 080_find-word.md │ ├── 081_find-city.md │ ├── 082_find-characters.md │ ├── 083_find-number.md │ ├── 084_find-the-minimum-number.md │ ├── 085_find-the-endpoint.md │ ├── 086_sound-off.md │ ├── 087_sound-off-game.md │ ├── 088_recruitment.md │ ├── 089_check-parentheses.md │ ├── 090_stitch-URLs.md │ ├── 091_replace-binary-trees-by-path.md │ ├── 092_pick-the-string.md │ ├── 093_the-priority-queue.md │ ├── 094_sensitive-field-encryption.md │ ├── 095_sequence-restore.md │ ├── 096_count-the-wild-geese.md │ ├── 097_the-numbers-game.md │ ├── 098_the-arrangement-of-numbers.md │ ├── 099_data-classification.md │ ├── 100_queuing-games.md │ ├── 101_the-most-economical-way-to-back-up-data.md │ ├── 102_array-merge.md │ ├── 103_array-sort.md │ ├── 104_array-make-up-minimum-number.md │ ├── 105_integer-factorization.md │ ├── 106_minimum-sum-of-integers.md │ ├── 107_integer-code.md │ ├── 108_file-directory-size.md │ ├── 109_doudizhu-1.md │ ├── 110_doudizhu-2.md │ ├── 111_new-job-number-system.md │ ├── 112_spin-dice.md │ ├── 113_time-format.md │ ├── 114_mingming's-random-number.md │ ├── 115_optimal-scheduling-policy.md │ ├── 116_lowest-order.md │ ├── 117_optimal-plant-distance-for-planting-trees.md │ ├── 118_the-largest-submatrix.md │ ├── 119_maximum-permutation.md │ ├── 119_the-N-permutation.md │ ├── 120_maximum-return-stock-return-problem.md │ ├── 121_maximum-connection-number-of-boys.md │ ├── 122_minimal-delivery-delay.md │ ├── 123_minimum-leaf-node.md │ ├── 124_minimum-number-of-steps.md │ ├── 125_minimum-number-of-exchanges.md │ ├── 126_minimum-number-of-stoped-cars.md │ ├── 127_the-least-time.md │ ├── 128_the-nearest-hospital.md │ ├── 129_the-nearest-point.md │ ├── 130_the-longest-common-suffix.md │ ├── 131_the-longest-legal-expression.md │ ├── 132_the-longest-vowel-string.md │ ├── 133_the-longest-continuous-alternating-square-wave-signal.md │ ├── 134_the-longest-consecutive-substring.md │ ├── 135_valid-substring.md │ ├── 136_service-dependencies.md │ ├── 137_server-power-consumption.md │ ├── 138_robots-walk-mazes.md │ ├── 139_witty-delivery-boy.md │ ├── 140_number-of-squares.md │ ├── 141_tree-structured-queries.md │ ├── 142_dictionary-reference.md │ ├── 143_hot-topics-and-word-sorting.md │ ├── 144_champions-and-runners-up-in-competition.md │ ├── 145_competition-rating.md │ ├── 146_narcissistic-number.md │ ├── 147_narcissistic-number2.md │ ├── 148_minimum-sum-of-integers.md │ ├── 149_k-elements-sum.md │ ├── 150_consecutive-number.md │ ├── 151_caving.md │ ├── 152_pipeline.md │ ├── 153_sliding-window.md │ ├── 154_mars-transformation.md │ ├── 155_martian_compute.md │ ├── 156_CBT-non-leaf-parts-post-order-traversal.md │ ├── 157_hostpot-network-statistivs.md │ ├── 158_guess-password.md │ ├── 159_monkey-climbing-the-mountain.md │ ├── 160_card-player.md │ ├── 161_longest-substring-in-circle.md │ ├── 162_copy-the-files-from-the-floppy-disk.md │ ├── 163_virus-infection.md │ ├── 164_max-number-of-consecutive-occurrences-of-the-same-char.md │ ├── 165_relative-open-syllable.md │ ├── 166_the-area-for-intersect-rectangles.md │ ├── 167_matrix-sparse-scan.md │ ├── 168_disk-capacity.md │ ├── 169_weighing-weights.md │ ├── 170_vertical-quad.md │ ├── 171_qualified-substring-length.md │ ├── 172_the-letter-of-the-k-th-min-code-value.md │ ├── 173_the-k-th-longest-string.md │ ├── 174_simple-memory-battery.md │ ├── 175_simple-unzip.md │ ├── 176_basketball-match.md │ ├── 177_screen-saver.md │ ├── 178_draw-machine.md │ ├── 179_meet-rules-combination.md │ ├── 180_statistical-monitoring.md │ ├── 181_surviving-on-a-desert-island.md │ ├── 182_archaeologist.md │ ├── 183_dinner-venue.md │ ├── 184_team-up-by-ability.md │ ├── 185_spacecraft.md │ ├── 186_english-input.md │ ├── 187_get-the-most-food.md │ ├── 188_replanting-of-unlived-poplar-trees.md │ ├── 189_watch-the-performance.md │ ├── 190_demystify-the-time-of-the-crime.md │ ├── 191_calculate-the-nearest-number.md │ ├── 192_comment-convert-output.md │ ├── 193_super-mario-crosses-the-suspension-bridge.md │ ├── 194_street-lighting.md │ ├── 195_peevers-2.md │ ├── 196_jump-the-grid-1.md │ ├── 197_jump-the-grid-2.md │ ├── 198_kick-stones.md │ ├── 199_height-sorting.md │ ├── 200_height-and-weight-ordering.md │ ├── 201_interscholasticmeet.md │ ├── 202_continuous-substrings.md │ ├── 203_consecutive-letter-length.md │ ├── 204_elective-courses.md │ ├── 205_choose-the-seat.md │ ├── 206_soldier-assault.md │ ├── 207_reassemble-the-string.md │ ├── 208_Alibaba-looks-for-gold-treasure-chests-2.md │ ├── 209_Alibaba-looks-for-gold-treasure-chests-3.md │ ├── 210_Alibaba-looks-for-gold-treasure-chests-4.md │ ├── 211_Alibaba-looks-for-gold-treasure-chests-5.md │ ├── 212_collect-five-blessing-cards.md │ ├── 213_the-number-of-unbroadcast-servers.md │ ├── 214_non-strictly-increment-sequence.md │ ├── 215_canteen-feeding-speed.md │ ├── 216_efficient-task-plan.md │ ├── 217_Alibaba-looks-for-gold-treasure-chests-1.md │ ├── 218_colorize-the-blackboard.md │ ├── 219_numeric-sequence-comparison.md │ └── images │ │ ├── 138_robot-mazes-eg2.png │ │ ├── 138_robots-mazes-demo.png │ │ ├── 170_vertical-quad1.png │ │ ├── 170_vertical-quad2.png │ │ └── 192_comment-sample1.png ├── questions200 │ ├── 001_excel-cell-value-statistics.md │ ├── 002_the-number-of-Linux-distributions.md │ ├── 003_the-road-to-work.md │ ├── 004_number-without-101.md │ ├── 005_Cloud-SMS-platform-promotional-activity.md │ ├── 006_task-hybridization.md │ ├── 007_preferred-nucleic-acid-testing-points.md │ ├── 008_elegant-array.md │ ├── 009_signal-transmission-and-reception.md │ ├── 010_maximum-subarray-of-geometric-mean.md │ ├── 011_bonus-distribution.md │ ├── 012_blockchain-file-dumping-system.md │ ├── 013_interval-connector.md │ ├── 014_removing-extra-whitespace.md │ ├── 015_intercepting-spam.md │ ├── 016_soldiers-crossing-the-river.md │ ├── 017_natural-reservoirs.md │ ├── 018_finding-similar-words.md │ ├── 019_finding-the-longest-substring-meeting-requirements.md │ ├── 020_open-day-activity.md │ ├── 021_express-delivery-station.md │ ├── 022_express-delivery-allocation-problem.md │ ├── 023_searching-land-plot-establishment.md │ ├── 024_number-addition-and-subtraction-game.md │ ├── 025_undirected-graph-coloring-problem.md │ ├── 026_optimal-resource-allocation.md │ ├── 027_best-opponent.md │ ├── 028_at-most-how-many-right-triangles.md │ ├── 029_max-count-of-equal-and-disjoint-contiguous-subsequences.md │ ├── 030_min-number-of-segments-covered.md │ ├── 031_worst-product-award.md │ ├── 032_best-location-for-service-center.md │ ├── 033_robot-movement-area.md │ ├── 034_cabinets-layout.md │ ├── 035_search-elements-in-the-tree.md │ ├── 036_find-the-largest-number.md │ ├── 037_wolf-sheep-crossing-the-river.md │ ├── 038_hardware-product-sales-plan.md │ ├── 039_combine-the-legal-minimum.md │ ├── 040_assemble-new-array.md │ ├── 041_min-number-of-uniform-weight-limit.md │ ├── 042_count-the-number-of-matching-pairs.md │ ├── 043_comparison-of-similarities-and-differences.md │ ├── 044_decompression-algorithm.md │ ├── 045_calculate-network-signal.md │ ├── 046_increment-string.md │ ├── 047_divide-the-string.md │ ├── 048_servers-that-can-make-up-a-network.md │ ├── 049_enhanced-strstr.md │ ├── 050_shopping.md │ ├── 051_print-task-sequencing.md │ └── images │ │ ├── 001-001-sample1-analysis.png │ │ ├── 002-001-sample-analysis.png │ │ ├── 003-001-sample-analysis.png │ │ ├── 003-002-dfs-description.png │ │ ├── 003-003-debug.png │ │ ├── 007-001-sample-analysis.png │ │ ├── 009-001-description.png │ │ ├── 014-001-description.png │ │ ├── 020-001-description.png │ │ ├── 033-001-sample-analysis.png │ │ ├── 035-001-sample-analysis.png │ │ ├── 045-001-sample-analysis1.png │ │ └── 045-002-sample-analysis2.png └── template.md └── images └── qrcode.jpeg /codes/choice100/002_task-execute-total-time.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 002_task-execute-total-time.py 6 | @time: 2023/7/11 23:08 7 | @project: huawei-od-python 8 | @desc: 002 任务总执行时长 9 | """ 10 | 11 | 12 | def solve_method(string): 13 | str_list = string.split(",") 14 | taskA = int(str_list[0]) 15 | taskB = int(str_list[1]) 16 | num = int(str_list[2]) 17 | 18 | result = [] 19 | for i in range(num + 1): 20 | total_time = i * taskA + (num - i) * taskB 21 | result.append(total_time) 22 | 23 | return list(set(result)) 24 | 25 | 26 | if __name__ == '__main__': 27 | assert solve_method("1,2,3") == [3, 4, 5, 6] 28 | -------------------------------------------------------------------------------- /codes/choice100/005_anonymous-letter.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 005_anonymous-letter.py 6 | @time: 2023/7/12 11:39 7 | @project: huawei-od-python 8 | @desc: 005 匿名信 9 | """ 10 | 11 | 12 | def solve_method(newspaper, anonymousLetter): 13 | newspaper_list = [sorted(l) for l in newspaper] 14 | anonymous_letter_list = [sorted(l) for l in anonymousLetter] 15 | 16 | for letter in anonymous_letter_list: 17 | if letter not in newspaper_list: 18 | return False 19 | 20 | return True 21 | 22 | 23 | if __name__ == '__main__': 24 | newspaper = ["ab", "cd"] 25 | anonymousLetter = ["ab"] 26 | assert solve_method(newspaper, anonymousLetter) is True 27 | 28 | newspaper = ["ab", "ef"] 29 | anonymousLetter = ["aef"] 30 | assert solve_method(newspaper, anonymousLetter) is False 31 | 32 | newspaper = ["ab", "bcd", "ef"] 33 | anonymousLetter = ["cbd", "fe"] 34 | assert solve_method(newspaper, anonymousLetter) is True 35 | 36 | newspaper = ["ab", "bcd", "ef"] 37 | anonymousLetter = ["cb", "fe"] 38 | assert solve_method(newspaper, anonymousLetter) is False 39 | -------------------------------------------------------------------------------- /codes/choice100/006_reverse-words.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 006_reverse-words.py 6 | @time: 2023/7/12 14:35 7 | @project: huawei-od-python 8 | @desc: 006 单词倒序 9 | """ 10 | 11 | 12 | def solve_method(line): 13 | result = "" 14 | # 使用临时字符串记录单词 15 | words = "" 16 | for char in line: 17 | if char.isalpha(): 18 | words += char 19 | else: 20 | # 将单词逆序 21 | result += words[::-1] 22 | words = "" 23 | result += char 24 | 25 | return result 26 | 27 | 28 | if __name__ == '__main__': 29 | s = "yM eman si boB." 30 | assert solve_method(s) == "My name is Bob." 31 | 32 | s = "woh era uoy ? I ma enif." 33 | assert solve_method(s) == "how are you ? I am fine." 34 | -------------------------------------------------------------------------------- /codes/choice100/008_find-duplicate-codes.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 008_find-duplicate-codes.py 6 | @time: 2023/7/12 20:24 7 | @project: huawei-od-python 8 | @desc: 008 找出重复代码(最长公共子序列) 9 | """ 10 | 11 | 12 | def solve_method(text1, text2): 13 | dp = [[0] * (len(text2) + 1) for _ in range(len(text1) + 1)] 14 | end = 0 15 | max_length = 0 16 | for i in range(1, len(text1) + 1): 17 | for j in range(1, len(text2) + 1): 18 | if text1[i - 1] == text2[j - 1]: 19 | dp[i][j] = dp[i - 1][j - 1] + 1 20 | # 获取最大长度 21 | if dp[i][j] > max_length: 22 | max_length = dp[i][j] 23 | end = i - 1 24 | # 返回最长公共子串 25 | return text1[end - max_length + 1: end + 1] 26 | 27 | 28 | if __name__ == '__main__': 29 | assert solve_method("hello123world", "hello123abc4") == "hello123" 30 | assert solve_method("private_void_method", "public_void_method") == "_void_method" 31 | assert solve_method("hiworld", "hiweb") == "hiw" 32 | -------------------------------------------------------------------------------- /codes/choice100/010_rearrange-string.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 010_rearrange-string.py 6 | @time: 2023/7/13 14:23 7 | @project: huawei-od-python 8 | @desc: 010 字符串重新排序 9 | """ 10 | from collections import defaultdict 11 | 12 | 13 | def solve_method(string): 14 | words = string.split() 15 | word_freqs = defaultdict(int) 16 | for word in words: 17 | rearrange_word = "".join(sorted(word)) 18 | word_freqs[rearrange_word] += 1 19 | 20 | sorted_freqs = sorted(word_freqs.items(), 21 | key=lambda x: (-x[1], len(x[0]), x[0])) 22 | 23 | result = [] 24 | for (word, freqs) in sorted_freqs: 25 | result.extend([word] * freqs) 26 | 27 | result = " ".join(result) 28 | return result 29 | 30 | 31 | if __name__ == '__main__': 32 | assert solve_method("This is an apple") == "an is This aelpp" 33 | assert solve_method("My sister is in the house not in the yard") \ 34 | == "in in eht eht My is not adry ehosu eirsst" 35 | -------------------------------------------------------------------------------- /codes/choice100/012_find-important-keys.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 012_find-important-keys.py 6 | @time: 2023/7/13 19:55 7 | @project: huawei-od-python 8 | @desc: 012 寻找关键钥匙 9 | """ 10 | 11 | 12 | def solve_method(key, line): 13 | boxes = line.split() 14 | processed_boxes = [] 15 | for box in boxes: 16 | # 得到箱子的有效密码串 17 | box_keys = [char.lower() for char in box if char.isalpha()] 18 | if len(box_keys) > 0: 19 | # 对字符串排序 20 | box_keys.sort() 21 | processed_boxes.append("".join(box_keys)) 22 | try: 23 | # 返回对应箱子编号 24 | return processed_boxes.index(key) + 1 25 | except ValueError: 26 | # 如果不存在,则返回-1 27 | return -1 28 | 29 | 30 | if __name__ == '__main__': 31 | assert solve_method("abc", "s,sdf123 A2c4b") == 2 32 | assert solve_method("abc", "s,sdf123 A2c4bd 523[]") == -1 33 | -------------------------------------------------------------------------------- /codes/choice100/013_find-key.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 013_find-key.py 6 | @time: 2023/7/13 20:27 7 | @project: huawei-od-python 8 | @desc: 013 寻找密码 9 | """ 10 | 11 | 12 | def solve_method(line): 13 | codebook = line.split() 14 | # 保证字典序 15 | codebook.sort() 16 | # 用于去重判断 17 | code_set = set(codebook) 18 | # 逆向遍历密码本 19 | for i in range(len(codebook) - 1, -1, -1): 20 | code = codebook[i] 21 | tmp_code = code[:-1] 22 | # 是否存在的标识 23 | flag = True 24 | # 循环判断从末尾开始依次去掉一位的密码是否在密码本中 25 | while len(tmp_code) > 0: 26 | if tmp_code in code_set: 27 | tmp_code = tmp_code[:-1] 28 | else: 29 | flag = False 30 | break 31 | if flag: 32 | return code 33 | 34 | return "" 35 | 36 | 37 | if __name__ == '__main__': 38 | assert solve_method("h he hel hell hello") == "hello" 39 | assert solve_method("b eredderd bw bww bwwl bwwln bwwlm") == "bwwln" 40 | -------------------------------------------------------------------------------- /codes/choice100/014_find-the-middle-node-of-the-linked-list.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 014_find-the-middle-node-of-the-linked-list.py 6 | @time: 2023/7/13 20:55 7 | @project: huawei-od-python 8 | @desc: 014 寻找链表的中间结点 9 | """ 10 | 11 | 12 | def solve_method(head, link): 13 | if len(link) == 0: 14 | return -1 15 | 16 | data = [] 17 | next_node = head 18 | # 遍历链表 19 | while next_node in link: 20 | # 获取下一个结点 21 | node = link[next_node] 22 | # 将下一个结点的数据加入到列表中 23 | data.append(node[0]) 24 | next_node = node[1] 25 | 26 | # 获取中间结点的数据 27 | return data[len(data) // 2] 28 | 29 | 30 | if __name__ == '__main__': 31 | head = "00100" 32 | n = 4 33 | link = {"00000": [4, -1], 34 | "00100": [1, "12309"], 35 | "33218": [3, "00000"], 36 | "12309": [2, "33218"]} 37 | assert solve_method(head, link) == 3 38 | 39 | head = "10000" 40 | n = 3 41 | link = {"76892": [7, "12309"], 42 | "12309": [5, -1], 43 | "10000": [1, "76892"]} 44 | assert solve_method(head, link) 45 | -------------------------------------------------------------------------------- /codes/choice100/019_count-vehicle-colors.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 019_count-vehicle-colors.py 6 | @time: 2023/7/14 15:31 7 | @project: huawei-od-python 8 | @desc: 019 找出通过车辆最多颜色 9 | """ 10 | import collections 11 | 12 | 13 | def solve_method(cars: list, time_windows: int): 14 | max_color = 0 15 | for i, car in enumerate(cars): 16 | # 读取窗口中的数据 17 | if i + 1 >= time_windows: 18 | count_cars = cars[i + 1 - time_windows: i + 1] 19 | else: 20 | count_cars = cars[:i + 1] 21 | # 使用Counter类构建哈希表 22 | counter = collections.Counter(count_cars) 23 | # 得到最多的频数 24 | max_color = max(max_color, counter.most_common(1)[0][1]) 25 | 26 | return max_color 27 | 28 | 29 | if __name__ == '__main__': 30 | assert solve_method([0, 1, 2, 1], 3) == 2 31 | assert solve_method([0, 1, 2, 1], 2) == 1 32 | -------------------------------------------------------------------------------- /codes/choice100/022_the-middle-position-of-array.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 022_the-middle-position-of-array.py 6 | @time: 2023/7/14 17:53 7 | @project: huawei-od-python 8 | @desc: 022 数组的中心位置 9 | """ 10 | from functools import reduce 11 | 12 | 13 | def solve_method(arr): 14 | l_prod = 1 15 | # 初始化右侧乘积,计算数组所有元素乘积 16 | r_prod = reduce(lambda x, y: x * y, arr) 17 | 18 | for i in range(len(arr)): 19 | # 逐步剔除左侧元素 20 | r_prod //= arr[i] 21 | if l_prod == r_prod: 22 | return i 23 | else: 24 | # 逐步乘以左侧元素 25 | l_prod *= arr[i] 26 | 27 | return -1 28 | 29 | 30 | if __name__ == '__main__': 31 | assert solve_method([2, 5, 3, 6, 5, 6]) == 3 32 | -------------------------------------------------------------------------------- /codes/choice100/024_seat-assignment.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 024_seat-assignment.py 6 | @time: 2023/7/14 20:03 7 | @project: huawei-od-python 8 | @desc: 024 新员工座位安排系统 9 | """ 10 | 11 | 12 | def solve_method(arr): 13 | max_value = 0 14 | max_pos = 0 15 | for i in range(len(arr)): 16 | if arr[i] == 0: 17 | if i == 0: 18 | # 最左侧 19 | friend_value = arr[i + 1] 20 | elif i == len(arr) - 1: 21 | # 最右侧 22 | friend_value = arr[i - 1] 23 | else: 24 | # 其他区域 25 | friend_value = arr[i - 1] + arr[i + 1] 26 | # 比较友好度 27 | if friend_value > max_value: 28 | max_value = friend_value 29 | max_pos = i 30 | return max_pos + 1 31 | 32 | 33 | if __name__ == '__main__': 34 | assert solve_method([0, 1, 0]) == 1 35 | assert solve_method([1, 1, 0, 1, 2, 1, 0]) == 3 36 | -------------------------------------------------------------------------------- /codes/choice100/025_choose-location-of-the-new-school.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 025_choose-location-of-the-new-school.py 6 | @time: 2023/7/14 20:24 7 | @project: huawei-od-python 8 | @desc: 025 新学校选址 9 | """ 10 | 11 | 12 | def solve_method(arr): 13 | arr.sort() 14 | if len(arr) % 2 == 0: 15 | return arr[len(arr) // 2 - 1] 16 | else: 17 | return arr[len(arr) // 2] 18 | 19 | 20 | if __name__ == '__main__': 21 | assert solve_method([0, 20, 40, 10, 30]) == 20 22 | -------------------------------------------------------------------------------- /codes/choice100/026_log-collection-system.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 026_log-collection-system.py 6 | @time: 2023/7/14 23:04 7 | @project: huawei-od-python 8 | @desc: 026 日志采集系统 9 | """ 10 | 11 | 12 | def calc_score(arr, cur_val): 13 | score = 0 14 | # 累计日志条数 15 | total = 0 16 | for i in range(cur_val + 1): 17 | if total + arr[i] < 100: 18 | # 直接奖励分 19 | score += arr[i] 20 | # 延迟上报扣分 21 | score -= arr[i] * (cur_val - i) 22 | total += arr[i] 23 | else: 24 | # 超过100条,强制上报 25 | score = 100 - total 26 | return score 27 | 28 | return score 29 | 30 | 31 | def solve_method(arr): 32 | score = 0 33 | 34 | if len(arr) > 0 and arr[0] >= 100: 35 | return 100 36 | 37 | for i in range(len(arr)): 38 | score = max(score, calc_score(arr, i)) 39 | 40 | return score 41 | 42 | 43 | if __name__ == '__main__': 44 | assert solve_method([1, 98, 1]) == 98 45 | assert solve_method([50, 60, 1]) == 50 46 | -------------------------------------------------------------------------------- /codes/choice100/029_max-salary.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: Jack Lee C.S. 5 | @file: 029_max-salary.py 6 | @time: 2023/07/12 14:39 7 | @project: huawei-od-python 8 | @desc: 029 最大报酬 9 | """ 10 | 11 | 12 | def solve_method(T, jobs): 13 | N = len(jobs) 14 | # 最短工作时长 15 | min_time = min(jobs, key=lambda x: x[0])[0] 16 | # dp[i][j]表示在前i个任务中,花费时间不超过j的最大报酬 17 | dp = [[0] * (T + 1) for _ in range(N + 1)] 18 | for i in range(1, N + 1): 19 | for j in range(min_time, T + 1): 20 | job = jobs[i - 1] 21 | w1 = dp[i - 1][j] 22 | w2 = job[1] + dp[i - 1][j - job[0]] if job[0] <= j else 0 23 | dp[i][j] = max(w1, w2) 24 | return dp[N][T] 25 | 26 | 27 | if __name__ == '__main__': 28 | jobs = [[20, 10], 29 | [20, 20], 30 | [20, 5]] 31 | assert solve_method(40, jobs) == 30 32 | -------------------------------------------------------------------------------- /codes/choice100/030_minimum-applicator-energy-efficiency.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 030_minimum-applicator-energy-efficiency.py 6 | @time: 2023/7/31 18:38 7 | @project: huawei-od-python 8 | @desc: 030 最小施肥机能效 9 | """ 10 | 11 | 12 | def solve_method(n, fields): 13 | if len(fields) > n: 14 | return -1 15 | 16 | left = 1 17 | result = right = max(fields) 18 | while left <= right: 19 | m = (left + right) // 2 20 | # 统计当前效能时,需要的天数 21 | day = sum(list(map(lambda x: x // m if x % m == 0 else (x // m) + 1, fields))) 22 | if day <= n: 23 | right = m - 1 24 | result = min(result, m) 25 | else: 26 | left = m + 1 27 | return result 28 | 29 | 30 | if __name__ == '__main__': 31 | assert solve_method(7, [5, 7, 9, 15, 10]) == 9 32 | assert solve_method(1, [2, 3, 4]) == -1 33 | -------------------------------------------------------------------------------- /codes/choice100/031_leftmost-redundant-overlay-substring.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 031_leftmost-redundant-overlay-substring.py 6 | @time: 2023/7/31 20:38 7 | @project: huawei-od-python 8 | @desc: 031 最左侧冗余覆盖子串 9 | """ 10 | from collections import Counter 11 | 12 | 13 | def solve_method(s1, s2, k): 14 | c1 = Counter(s1) 15 | for i in range(len(s2) - len(s1) - k): 16 | c2 = Counter(s2[i:i + len(s1) + k]) 17 | if c1 < c2: 18 | return i 19 | 20 | return - 1 21 | 22 | 23 | if __name__ == '__main__': 24 | assert solve_method("ab", "aabcd", 1) == 0 25 | assert solve_method("abc", "dfs", 10) == -1 26 | -------------------------------------------------------------------------------- /codes/choice100/032_min-board-length.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 032_min-board-length.py 6 | @time: 2023/7/31 21:45 7 | @project: huawei-od-python 8 | @desc: 032 最短木板长度 9 | """ 10 | 11 | 12 | def solve_method(m, A): 13 | N = len(A) 14 | A.sort() 15 | i = 0 16 | for j in range(m): 17 | # 补上最短的 18 | A[i] += 1 19 | if i == (N - 1) or A[i + 1] >= A[i]: 20 | # 如果当前依然是最短的,则继续让最短的加1 21 | i = 0 22 | else: 23 | # 如果后面那个更短,则向后比较 24 | i += 1 25 | return sorted(A)[0] 26 | 27 | 28 | if __name__ == '__main__': 29 | assert solve_method(3, [4, 5, 3, 5, 5]) == 5 30 | assert solve_method(2, [4, 5, 3, 5, 5]) == 4 31 | assert solve_method(10, [4, 5, 5, 5, 5]) == 6 32 | -------------------------------------------------------------------------------- /codes/choice100/033_find-charging-device-combinations.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 033_find-charging-device-combinations.py 6 | @time: 2023/8/1 10:53 7 | @project: huawei-od-python 8 | @desc: 033 查找充电设备组合 9 | """ 10 | 11 | 12 | def solve_method(P, p_max): 13 | N = len(P) 14 | # dp[i][j]表示前i个充电设备中选取功率不超过j的充电设备的最大功率和 15 | dp = [[0] * (p_max + 1) for _ in range(N + 1)] 16 | for i in range(1, N + 1): 17 | for j in range(min(P), p_max + 1): 18 | if P[i - 1] > j: 19 | # 当前充电设备功率超过了最大功率j,不选择该充电设备 20 | dp[i][j] = dp[i - 1][j] 21 | else: 22 | # 选择该充电设备,选择功率和较大的一个 23 | dp[i][j] = max(dp[i - 1][j], P[i - 1] + dp[i - 1][j - P[i - 1]]) 24 | return dp[N][p_max] 25 | 26 | 27 | if __name__ == '__main__': 28 | assert solve_method([50, 20, 20, 60], 90) == 90 29 | assert solve_method([50, 40], 30) == 0 30 | -------------------------------------------------------------------------------- /codes/choice100/035_specific-double-ended-que.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 035_specific-double-ended-que.py 6 | @time: 2023/8/1 17:39 7 | @project: huawei-od-python 8 | @desc: 035 特异性双端队列 9 | """ 10 | 11 | 12 | def solve_method(cmds): 13 | size = 0 14 | # 标识当前是否输出的顺序正确 15 | isSorted = True 16 | count = 0 17 | 18 | for cmd in cmds: 19 | if cmd.startswith("head add"): 20 | # 从头部添加,导致输出顺序不正常,设置标识为False 21 | if size > 0 and isSorted: 22 | isSorted = False 23 | size += 1 24 | elif cmd.startswith("tail add"): 25 | size += 1 26 | else: 27 | if size <= 0: 28 | continue 29 | 30 | if not isSorted: 31 | # 如果标识为False,则调整次数加1 32 | count += 1 33 | isSorted = True 34 | 35 | # 队列长度减1 36 | size -= 1 37 | 38 | return count 39 | 40 | 41 | if __name__ == '__main__': 42 | cmds = ["head add 1", 43 | "remove", 44 | "tail add 2", 45 | "head add 3", 46 | "remove", 47 | "remove"] 48 | assert solve_method(cmds) == 1 49 | -------------------------------------------------------------------------------- /codes/choice100/036_riddles.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 036_riddles.py 6 | @time: 2023/8/1 18:27 7 | @project: huawei-od-python 8 | @desc: 036 猜字谜 9 | """ 10 | 11 | 12 | def solve_method(issues, answers): 13 | ans = [] 14 | 15 | for issue in issues: 16 | str1 = "".join(sorted(set(issue.lower()))) 17 | find = False 18 | 19 | for answer in answers: 20 | answer = answer.lower() 21 | str2 = "".join(sorted(set(answer))) 22 | 23 | if str1 == str2: 24 | ans.append(answer) 25 | find = True 26 | 27 | if not find: 28 | ans.append("not found") 29 | 30 | return ans 31 | 32 | 33 | if __name__ == '__main__': 34 | assert solve_method(["conection"], ["connection", "today"]) == ["connection"] 35 | assert solve_method(["bdni", "wooood"], ["bind", "wrong", "wood"]) == ["bind", "wood"] 36 | -------------------------------------------------------------------------------- /codes/choice100/037_block-games-with-the-same-number.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 037_block-games-with-the-same-number.py 6 | @time: 2023/8/1 18:52 7 | @project: huawei-od-python 8 | @desc: 037 相同数字的积木游戏 9 | """ 10 | from collections import defaultdict 11 | 12 | 13 | def solve_method(nums): 14 | idx = defaultdict(list) 15 | 16 | for i in range(len(nums)): 17 | idx[nums[i]].append(i) 18 | 19 | ans = -1 20 | for k in idx.keys(): 21 | if len(idx[k]) > 1: 22 | ans = max(ans, idx[k][-1] - idx[k][0]) 23 | 24 | return ans 25 | 26 | 27 | if __name__ == '__main__': 28 | assert solve_method([1, 2, 3, 1, 4]) == 3 29 | assert solve_method([1, 2]) == -1 30 | -------------------------------------------------------------------------------- /codes/choice100/038_knowledge-graph-neologism-mining.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 038_knowledge-graph-neologism-mining.py 6 | @time: 2023/8/2 9:06 7 | @project: huawei-od-python 8 | @desc: 038 知识图谱新词挖掘 9 | """ 10 | from collections import Counter 11 | 12 | 13 | def solve_method(content: str, word: str) -> int: 14 | w = len(word) 15 | count = 0 16 | c2 = Counter(word) 17 | 18 | for i in range(len(content) - w + 1): 19 | c1 = Counter(content[i:i + w]) 20 | if c1 == c2: 21 | count += 1 22 | 23 | return count 24 | 25 | 26 | if __name__ == '__main__': 27 | assert solve_method("qweebaewqd", "qwe") == 2 28 | assert solve_method("abab", "ab") == 3 29 | -------------------------------------------------------------------------------- /codes/choice100/039_car-rental-ride-Green-Island.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 039_car-rental-ride-Green-Island.py 6 | @time: 2023/8/2 9:21 7 | @project: huawei-od-python 8 | @desc: 039 租车骑绿岛 9 | """ 10 | 11 | 12 | def solve_method(m, arr): 13 | arr.sort() 14 | count = 0 15 | 16 | i = 0 17 | j = len(arr) - 1 18 | 19 | while i < j: 20 | if arr[i] + arr[j] <= m: 21 | i += 1 22 | j -= 1 23 | count += 1 24 | 25 | if i == j: 26 | count += 1 27 | 28 | return count 29 | 30 | 31 | if __name__ == '__main__': 32 | assert solve_method(3, [3, 2, 2, 1]) == 3 33 | -------------------------------------------------------------------------------- /codes/choice100/040_port-merging.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 040_port-merging.py 6 | @time: 2023/8/2 10:11 7 | @project: huawei-od-python 8 | @desc: 040 端口合并 9 | """ 10 | 11 | 12 | def merge(ports): 13 | for i in range(len(ports)): 14 | for j in range(i + 1, len(ports)): 15 | seti = set(ports[i]) 16 | setj = set(ports[j]) 17 | merge_set = seti.union(setj) 18 | if len(merge_set) <= len(seti) + len(setj) - 2: 19 | ports[i] = list(merge_set) 20 | ports.pop(j) 21 | return True 22 | return False 23 | 24 | 25 | def solve_method(ports): 26 | while merge(ports): 27 | pass 28 | 29 | return [sorted(list(set(p))) for p in ports] 30 | 31 | 32 | if __name__ == '__main__': 33 | ports = [[4], [2, 3, 2], [1, 2], [5]] 34 | assert solve_method(ports) == [[4], [2, 3], [1, 2], [5]] 35 | ports = [[2, 3, 1], [4, 3, 2], [5]] 36 | assert solve_method(ports) == [[1, 2, 3, 4], [5]] 37 | ports = [[10], [4, 2, 1], [9], [3, 6, 9, 2], [6, 3, 4], [8]] 38 | assert solve_method(ports) == [[10], [1, 2, 3, 4, 6, 9], [9], [8]] 39 | -------------------------------------------------------------------------------- /codes/choice100/041_the-boxes-are-arranged-in-a-zigzag-pattern.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 041_the-boxes-are-arranged-in-a-zigzag-pattern.py 6 | @time: 2023/8/2 10:52 7 | @project: huawei-od-python 8 | @desc: 041 箱子之字形摆放 9 | """ 10 | 11 | 12 | def solve_method(chars, n): 13 | result = ["" for _ in range(n)] 14 | index = 0 15 | # 标识:True表示从上到下,False表示从下到上 16 | asc = True 17 | 18 | for c in chars: 19 | if index == -1: 20 | index = 0 21 | asc = True 22 | if index == n: 23 | index = n - 1 24 | asc = False 25 | result[index] += c 26 | 27 | if asc: 28 | index += 1 29 | else: 30 | index -= 1 31 | 32 | return result 33 | 34 | 35 | if __name__ == '__main__': 36 | assert solve_method("ABCDEFG", 3) == ["AFG", "BE", "CD"] 37 | -------------------------------------------------------------------------------- /codes/choice100/043_automatic-exposure.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 043_automatic-exposure.py 6 | @time: 2023/8/2 23:47 7 | @project: huawei-od-python 8 | @desc: 043 自动曝光 9 | """ 10 | 11 | 12 | def solve_method(img): 13 | avg = sum(img) // len(img) 14 | diff = avg - 128 15 | new_img = img.copy() 16 | 17 | k = 0 18 | if diff > 0: 19 | # 如果差值大于0,则k是负数 20 | while avg > 127: 21 | k -= 1 22 | avg = get_new_image_avg(new_img, k) 23 | if diff < 0: 24 | # 如果差值小于0,则k是正数 25 | while avg < 128: 26 | k += 1 27 | avg = get_new_image_avg(new_img, k) 28 | return k 29 | 30 | 31 | def get_new_image_avg(new_img, k): 32 | new_img = [min(max(0, x + k), 255) for x in new_img] 33 | return sum(new_img) // len(new_img) 34 | 35 | 36 | if __name__ == '__main__': 37 | assert solve_method([0, 0, 0, 0]) == 128 38 | assert solve_method([129, 130, 129, 130]) == -2 39 | -------------------------------------------------------------------------------- /codes/choice100/048_bit-error-rate.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 048_bit-error-rate.py 6 | @time: 2023/8/3 16:51 7 | @project: huawei-od-python 8 | @desc: 048 通信误码 9 | """ 10 | import math 11 | from collections import defaultdict 12 | 13 | 14 | def solve_method(arr): 15 | # 构建误码索引字典,key为误码值,value为该值所在的位置 16 | num_index = defaultdict(list) 17 | 18 | for i in range(len(arr)): 19 | num_index[arr[i]].append(i) 20 | 21 | max_count = 0 22 | min_len = math.inf 23 | 24 | for values in num_index.values(): 25 | count = len(values) 26 | length = values[-1] - values[0] + 1 27 | 28 | # 如果出现次数较大,或次数相同时,距离较小,则记录最大出现次数和最小距离。 29 | if count > max_count or (count == max_count and length < min_len): 30 | max_count = max(count, max_count) 31 | min_len = min(length, min_len) 32 | 33 | return min_len 34 | 35 | 36 | if __name__ == '__main__': 37 | assert solve_method([1, 2, 2, 4, 1]) == 2 38 | assert solve_method([1, 2, 2, 4, 2, 1, 1]) == 4 39 | -------------------------------------------------------------------------------- /codes/choice100/050_hotel-reservation.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 050_hotel-reservation.py 6 | @time: 2023/8/3 17:43 7 | @project: huawei-od-python 8 | @desc: 050 预订酒店 9 | """ 10 | 11 | 12 | def solve_method(k, x, prices): 13 | near_prices = [] 14 | 15 | # 计算各个价格与心理价位的距离 16 | for p in prices: 17 | near_prices.append((p, abs(p - x))) 18 | 19 | # 按照距离从小到大排序,距离相同时,按照价格从小到大排序 20 | near_prices.sort(key=lambda x: (x[1], x[0])) 21 | 22 | # 取前k个与心理价位最近的酒店价格 23 | ans = list(map(lambda x: x[0], near_prices[:k])) 24 | ans.sort() 25 | 26 | return ans 27 | 28 | 29 | if __name__ == '__main__': 30 | assert solve_method(5, 6, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) == [4, 5, 6, 7, 8] 31 | assert solve_method(4, 6, [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]) == [4, 5, 6, 7] 32 | assert solve_method(3, 1000, [30, 30, 200, 500, 70, 300]) == [200, 300, 500] 33 | -------------------------------------------------------------------------------- /codes/others100/002_GPU-scheduling.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 002_GPU-scheduling.py 6 | @time: 2023/7/26 9:46 7 | @project: huawei-od-python 8 | @desc: 002 GPU调度 9 | """ 10 | 11 | 12 | def solve_method(n, jobs): 13 | # 初始化总运行时间和等待时间 14 | total_time, remaining_time = 0, 0 15 | for job in jobs: 16 | if job + remaining_time > n: 17 | # 计算等待时间 18 | remaining_time = job + remaining_time - n 19 | else: 20 | # 无需等待 21 | remaining_time = 0 22 | total_time += 1 23 | 24 | # 继续等待作业完成,每载入n个作业,总时间累加1 25 | while remaining_time > 0: 26 | remaining_time -= n 27 | total_time += 1 28 | 29 | return total_time 30 | 31 | 32 | if __name__ == '__main__': 33 | assert solve_method(3, [1, 2, 3, 4, 5]) == 6 34 | assert solve_method(4, [5, 4, 1, 1, 1]) == 5 35 | -------------------------------------------------------------------------------- /codes/others100/003_convert-IPv4-addresses-to-integers.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 003_convert-IPv4-addresses-to-integers.py 6 | @time: 2023/7/26 10:07 7 | @project: huawei-od-python 8 | @desc: 003 IPv4地址转换成整数 9 | """ 10 | 11 | 12 | def check_ip(ip): 13 | is_valid = False 14 | if "#" in ip: 15 | ip_strings = ip.split("#") 16 | length = len(ip_strings) 17 | if length == 4 \ 18 | and 1 <= int(ip_strings[0]) <= 128 \ 19 | and 0 <= int(ip_strings[1]) <= 255 \ 20 | and 0 <= int(ip_strings[2]) <= 255 \ 21 | and 0 <= int(ip_strings[3]) <= 255: 22 | is_valid = True 23 | 24 | return is_valid 25 | 26 | 27 | def solve_method(ip): 28 | if check_ip(ip): 29 | ip_strings = ip.split("#") 30 | num = 0 31 | for ip_string in ip_strings: 32 | num += int(ip_string) 33 | num = num << 8 34 | 35 | num = num >> 8 36 | return num 37 | else: 38 | return "invalid IP" 39 | 40 | 41 | if __name__ == '__main__': 42 | assert solve_method("100#101#1#5") == 1684340997 43 | assert solve_method("1#2#3") == "invalid IP" 44 | -------------------------------------------------------------------------------- /codes/others100/004_n-decimal-subtraction.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 004_n-decimal-subtraction.py 6 | @time: 2023/7/26 10:35 7 | @project: huawei-od-python 8 | @desc: 004 N进制减法 9 | """ 10 | 11 | 12 | def to_str(num, base): 13 | base_str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXY" 14 | if num < base: 15 | return base_str[num] 16 | else: 17 | return to_str(num // base, base) + base_str[num % base] 18 | 19 | 20 | def solve_method(N, a, b): 21 | if N < 2 or N > 35: 22 | return "-1", "ERROR" 23 | 24 | diff = int(a, N) - int(b, N) 25 | sign = 0 if diff >= 0 else 1 26 | return sign, to_str(diff, N) 27 | 28 | 29 | if __name__ == '__main__': 30 | assert solve_method(2, "11", "1") == (0, "10") 31 | -------------------------------------------------------------------------------- /codes/others100/005_rsa-encryption-algorithm.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 005_rsa-encryption-algorithm.py 6 | @time: 2023/7/26 11:17 7 | @project: huawei-od-python 8 | @desc: 005 RSA加密算法 9 | """ 10 | 11 | 12 | def solve_method(num): 13 | # 找出所有小于num的素数 14 | tmp = num 15 | f = 2 16 | factors = set() 17 | while tmp != 1: 18 | if tmp % f != 0: 19 | f += 1 20 | else: 21 | factors.add(f) 22 | tmp //= f 23 | 24 | # 找出两个素数,满足因数分解 25 | for f1 in factors: 26 | for f2 in factors: 27 | if f1 * f2 == num: 28 | return min(f1, f2), max(f1, f2) 29 | 30 | return -1, -1 31 | 32 | 33 | if __name__ == '__main__': 34 | assert solve_method(15) == (3, 5) 35 | assert solve_method(27) == (-1, -1) 36 | -------------------------------------------------------------------------------- /codes/others100/006_tlv-code.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 006_tlv-code.py 6 | @time: 2023/7/26 13:54 7 | @project: huawei-od-python 8 | @desc: 006 TLV编码 9 | """ 10 | 11 | 12 | def solve_method(tag, source): 13 | index = 0 14 | while index < len(source): 15 | cur_tag = source[index] 16 | # 小端序 17 | length = int(source[index + 2] + source[index + 1], 16) 18 | if tag == cur_tag: 19 | value = source[index + 3: index + 3 + length] 20 | return value 21 | 22 | index += 3 + length 23 | 24 | 25 | if __name__ == '__main__': 26 | source = ["32", "01", "00", "AE", "90", "02", "00", "01", "02", "30", "03", "00", 27 | "AB", "32", "31", "31", "02", "00", "32", "33", "33", "01", "00", "CC"] 28 | assert solve_method("31", source) == ["32", "33"] 29 | -------------------------------------------------------------------------------- /codes/others100/010_product-maximum-value.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 010_product-maximum-value.py 6 | @time: 2023/7/26 17:09 7 | @project: huawei-od-python 8 | @desc: 010 乘积最大值 9 | """ 10 | 11 | 12 | def solve_method(strings): 13 | max_value = 0 14 | for i in range(len(strings)): 15 | for j in range(i, len(strings)): 16 | set_a = set(strings[i]) 17 | set_b = set(strings[j]) 18 | # 判断set集合合并的长度是否等于各集合的长度和,如果等于,则表示没有相同字符 19 | if len(set_a.union(set_b)) == len(set_a) + len(set_b): 20 | max_value = max(max_value, len(set_a) * len(set_b)) 21 | 22 | return max_value 23 | 24 | 25 | if __name__ == '__main__': 26 | assert solve_method(["iwdvpbn", "hk", "iuop", "iikd", "kadgpf"]) == 14 27 | -------------------------------------------------------------------------------- /codes/others100/012_minimum-absolute-sum-of-two-numbers-in-an-out-of-order-integer-sequence.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 012_minimum-absolute-sum-of-two-numbers-in-an-out-of-order-integer-sequence.py 6 | @time: 2023/7/26 19:25 7 | @project: huawei-od-python 8 | @desc: 012 乱序整数序列两数之和绝对值最小 9 | """ 10 | import math 11 | 12 | 13 | def solve_method(nums): 14 | min_value = math.inf 15 | min_num = 0 16 | max_num = 0 17 | for i in range(len(nums)): 18 | for j in range(i, len(nums)): 19 | a, b = nums[i], nums[j] 20 | if a != b: 21 | value = abs(a + b) 22 | if value < min_value: 23 | min_value = value 24 | min_num = min(a, b) 25 | max_num = max(a, b) 26 | 27 | return [min_num, max_num, min_value] 28 | 29 | 30 | if __name__ == '__main__': 31 | assert solve_method([-1, -3, 7, 5, 11, 15]) == [-3, 5, 2] 32 | -------------------------------------------------------------------------------- /codes/others100/013_event-push.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 013_event-push.py 6 | @time: 2023/7/26 19:44 7 | @project: huawei-od-python 8 | @desc: 013 事件推送 9 | """ 10 | 11 | 12 | def solve_method(A, B, R): 13 | result = [] 14 | index = 0 15 | for a in A: 16 | while index < len(B): 17 | if a <= B[index] and B[index] - a <= R: 18 | result.append([a, B[index]]) 19 | break 20 | index += 1 21 | 22 | return result 23 | 24 | 25 | if __name__ == '__main__': 26 | A = [1, 5, 5, 10] 27 | B = [1, 3, 8, 8, 20] 28 | assert solve_method(A, B, 5) == [[1, 1], [5, 8], [5, 8]] 29 | -------------------------------------------------------------------------------- /codes/others100/015_swap-characters.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 015_swap-characters.py 6 | @time: 2023/7/26 20:37 7 | @project: huawei-od-python 8 | @desc: 015 交换字符 9 | """ 10 | 11 | 12 | def solve_method(string): 13 | string = list(string) 14 | pos = 0 15 | min_char = string[0] 16 | for i in range(1, len(string)): 17 | char = string[i] 18 | if char <= min_char: 19 | min_char, pos = char, i 20 | 21 | if pos != 0: 22 | string[pos], string[0] = string[0], min_char 23 | 24 | return "".join(string) 25 | 26 | 27 | if __name__ == '__main__': 28 | assert solve_method("abcdef") == "abcdef" 29 | assert solve_method("bcdefa") == "acdefb" 30 | -------------------------------------------------------------------------------- /codes/others100/016_sites-with-the-most-people.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 016_sites-with-the-most-people.py 6 | @time: 2023/7/26 20:58 7 | @project: huawei-od-python 8 | @desc: 016 人数最多的站点 9 | """ 10 | from collections import defaultdict 11 | 12 | 13 | def solve_method(people): 14 | station_people = defaultdict(int) 15 | for p in people: 16 | for station in range(p[0], p[1] + 1): 17 | station_people[station] += 1 18 | 19 | result = sorted(station_people.items(), key=lambda x: -x[1]) 20 | return result[0][0] 21 | 22 | 23 | if __name__ == '__main__': 24 | people = [(1, 3), (2, 4), (1, 4)] 25 | assert solve_method(people) == 2 26 | -------------------------------------------------------------------------------- /codes/others100/020_tug-of-war-competition.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 007_tug-of-war-competition.py 6 | @time: 2023/9/1 11:37 7 | @project: huawei-od-python 8 | @desc: 020 拔河比赛 9 | """ 10 | 11 | 12 | def solve_method(arr): 13 | # 按照身高从高到低、体重从大到小排序 14 | arr.sort(key=lambda x: (-x[0], -x[1])) 15 | # 选出10名选手 16 | return arr[:10] 17 | 18 | 19 | if __name__ == '__main__': 20 | arr = [[181, 70], [182, 70], [183, 70], [184, 70], [185, 70], 21 | [186, 70], [180, 71], [180, 72], [180, 73], [180, 74], 22 | [180, 75]] 23 | assert solve_method(arr) == [[186, 70], [185, 70], [184, 70], [183, 70], [182, 70], 24 | [181, 70], [180, 75], [180, 74], [180, 73], [180, 72]] 25 | -------------------------------------------------------------------------------- /codes/others100/023_find-max-distance.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 023_find-max-distance.py 6 | @time: 2023/8/7 13:53 7 | @project: huawei-od-python 8 | @desc: 023 停车场最大的距离 9 | """ 10 | 11 | 12 | def solve_method(sites): 13 | # 初始化最大距离为0 14 | max_distance = 0 15 | 16 | for i, n in enumerate(sites): 17 | if n == 0: 18 | # 计算该空车位与最近的已停车车辆之间的距离 19 | distance = min([abs(i - j) for j, x in enumerate(sites) if x == 1]) 20 | # 获取最大距离 21 | if distance > max_distance: 22 | max_distance = distance 23 | 24 | return max_distance 25 | 26 | 27 | if __name__ == '__main__': 28 | sites = [1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1] 29 | assert solve_method(sites) == 2 30 | -------------------------------------------------------------------------------- /codes/others100/025_push-and-pop.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 025_push-and-pop.py 6 | @time: 2023/8/7 14:46 7 | @project: huawei-od-python 8 | @desc: 025 入栈出栈 9 | """ 10 | 11 | 12 | def solve_method(nums): 13 | stack = [] 14 | 15 | for num in nums: 16 | # 标识是否重新入栈新元素 17 | changed = False 18 | if stack: 19 | i = n = len(stack) - 1 20 | 21 | while i > -1: 22 | # 当满足条件时,出栈相应元素,入栈新元素 23 | if sum(stack[i:n + 1]) == num: 24 | del stack[i:n + 1] 25 | stack.insert(i, num * 2) 26 | changed = True 27 | break 28 | i -= 1 29 | if not changed: 30 | stack.append(num) 31 | 32 | return stack[::-1] 33 | 34 | 35 | if __name__ == '__main__': 36 | nums = [5, 10, 20, 50, 85, 1] 37 | assert solve_method(nums) == [1, 170] 38 | 39 | nums = [6, 7, 8, 13, 9] 40 | assert solve_method(nums) == [9, 13, 8, 7, 6] 41 | 42 | nums = [1, 2, 5, 7, 9, 1, 2, 2] 43 | assert solve_method(nums) == [4, 1, 9, 14, 1] 44 | -------------------------------------------------------------------------------- /codes/others100/026_related-string.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 026_related-string.py 6 | @time: 2023/8/7 16:21 7 | @project: huawei-od-python 8 | @desc: 026 关联字串 9 | """ 10 | from collections import Counter 11 | 12 | 13 | def solve_method(str1, str2): 14 | length = len(str1) 15 | for i in range(len(str2) - length + 1): 16 | sub_str = list(str2[i:i + length]) 17 | if Counter(sub_str) == Counter(str1): 18 | return i 19 | return -1 20 | 21 | 22 | if __name__ == '__main__': 23 | assert solve_method("abc", "efghicabiii") == 5 24 | assert solve_method("abc", "efghicaibii") == -1 25 | -------------------------------------------------------------------------------- /codes/others100/028_taxi-fare.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 028_taxi-fare.py 6 | @time: 2023/8/7 18:10 7 | @project: huawei-od-python 8 | @desc: 028 出租车计费 9 | """ 10 | 11 | 12 | def solve_method(num): 13 | real = 0 14 | count = 0 15 | while num > 0: 16 | n = num % 10 17 | # 大于4的情况 18 | if n > 4: 19 | n = n - 1 20 | real = real + n * 9 ** count 21 | count = count + 1 22 | num = num // 10 23 | return real 24 | 25 | 26 | if __name__ == '__main__': 27 | assert solve_method(5) == 4 28 | assert solve_method(17) == 15 29 | assert solve_method(100) == 81 30 | -------------------------------------------------------------------------------- /codes/others100/029_faulty-or-gate-circuit.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: LiHongYu 5 | @file: 029_faulty-or-gate-circuit.py 6 | @time: 2023/8/7 18:44 7 | @project: huawei-od-python 8 | @desc: 029 出错的或电路 9 | """ 10 | 11 | 12 | def solve_method(binary1, binary2): 13 | # 先统计第1个二进制数的0与1的次数,方便后续计算次数改变 14 | cnt_binary1_1 = binary1.count('1') 15 | cnt_binary1_0 = binary1.count('0') 16 | 17 | # 存储结果 18 | count = 0 19 | 20 | # 遍历第2个二进制数的每一位 21 | for i in range(len(binary1)): 22 | # 遇到0时,分类计数 23 | if binary2[i] == '0': 24 | # 当第2个二进制数的某一位为1时,无论第1个二进制数对应的位改变了为什么,都不会影响结果。 25 | # 当某一位为0时,这时就会影响结果: 26 | # 当第1个二进制数的该位为1时,1|0=1变为0|0=0,那么统计第1个二进制数的所有0的次数,就是让电路改变结果的次数。 27 | # 当第1个二进制数的该位为0时,0|0=0变为1|0=1,同上,统计第1个二进制数的所有1的次数。 28 | if binary1[i] == '1': 29 | count += cnt_binary1_0 30 | if binary1[i] == '0': 31 | count += cnt_binary1_1 32 | return count 33 | 34 | 35 | if __name__ == '__main__': 36 | assert solve_method("010", "110") == 1 37 | assert solve_method("011011", "110110") == 4 38 | assert solve_method("110", "001") == 2 39 | -------------------------------------------------------------------------------- /codes/others100/030_sharing-blocks.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 030_sharing-blocks.py 6 | @time: 2023/8/7 20:58 7 | @project: huawei-od-python 8 | @desc: 030 分积木 9 | """ 10 | 11 | 12 | def solve_method(blocks): 13 | count = 0 14 | for i in blocks: 15 | # 进行异或运算,等同于不进位的加法 16 | count = count ^ i 17 | if count == 0: 18 | return sum(blocks) - min(blocks) 19 | else: 20 | return "No" 21 | 22 | 23 | if __name__ == '__main__': 24 | assert solve_method([3, 5, 6]) == 11 25 | -------------------------------------------------------------------------------- /codes/others100/031_sharing-candies.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 031_sharing-candies.py 6 | @time: 2023/8/7 21:40 7 | @project: huawei-od-python 8 | @desc: 031 分糖果 9 | """ 10 | 11 | 12 | def divide_candies(candies_num): 13 | if candies_num == 2: 14 | # 当糖果数为2时,返回1 15 | return 1 16 | if candies_num % 2 == 0: 17 | # 当糖果数为偶数时,平均分配 18 | return divide_candies(candies_num // 2) + 1 19 | else: 20 | # 当糖果数为奇数时,取出或放回的次数最小值 21 | return min(divide_candies(candies_num + 1) + 1, divide_candies(candies_num - 1) + 1) 22 | 23 | 24 | def solve_method(n): 25 | if n > 1000000: 26 | return False 27 | 28 | return divide_candies(n) 29 | 30 | 31 | if __name__ == '__main__': 32 | assert solve_method(15) == 5 33 | -------------------------------------------------------------------------------- /codes/others100/032_sharing-apples.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 032_sharing-apples.py 6 | @time: 2023/8/7 22:56 7 | @project: huawei-od-python 8 | @desc: 032 分苹果 9 | """ 10 | 11 | 12 | def solve_method(apples): 13 | count = 0 14 | for i in apples: 15 | # 进行异或运算,等同于不进位的加法 16 | count = count ^ i 17 | if count == 0: 18 | return sum(apples) - min(apples) 19 | else: 20 | return -1 21 | 22 | 23 | if __name__ == '__main__': 24 | assert solve_method([3, 5, 6]) == 11 25 | assert solve_method([7258, 6579, 2602, 6716, 3050, 3564, 5396, 1773]) == 35165 26 | -------------------------------------------------------------------------------- /codes/others100/033_find-prime-factors.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 033_find-prime-factors.py 6 | @time: 2023/8/7 23:00 7 | @project: huawei-od-python 8 | @desc: 033 分解质因数 9 | """ 10 | 11 | 12 | def solve_method(num): 13 | # 找出所有小于num的素数 14 | tmp = num 15 | f = 2 16 | factors = set() 17 | while tmp != 1: 18 | if tmp % f != 0: 19 | f += 1 20 | else: 21 | factors.add(f) 22 | tmp //= f 23 | 24 | # 找出两个素数,满足因数分解 25 | for f1 in factors: 26 | for f2 in factors: 27 | if f1 * f2 == num: 28 | return min(f1, f2), max(f1, f2) 29 | 30 | return "NO" 31 | 32 | 33 | if __name__ == '__main__': 34 | assert solve_method(15) == (3, 5) 35 | assert solve_method(7) == "NO" 36 | -------------------------------------------------------------------------------- /codes/others100/035_remove-minimum-characters.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 035_remove-minimum-characters.py 6 | @time: 2023/8/8 10:18 7 | @project: huawei-od-python 8 | @desc: 035 删除最少的字符 9 | """ 10 | from collections import Counter 11 | 12 | 13 | def solve_method(chars): 14 | # 统计字符频率 15 | freq = Counter(chars) 16 | # 求最小值 17 | min_freq = min(freq.values()) 18 | result = "" 19 | for c in chars: 20 | if freq[c] != min_freq: 21 | result += c 22 | 23 | if len(result) == 0: 24 | return 'empty' 25 | return result 26 | 27 | 28 | if __name__ == '__main__': 29 | assert solve_method("abcdd") == "dd" 30 | assert solve_method("aabbccdd") == "empty" 31 | -------------------------------------------------------------------------------- /codes/others100/036_maximum-value-after-deletion.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 036_maximum-value-after-deletion.py 6 | @time: 2023/8/8 10:28 7 | @project: huawei-od-python 8 | @desc: 036 删除重复数字后的最大数字 9 | """ 10 | from collections import Counter 11 | 12 | 13 | def backstracking(nums: list, paths: list): 14 | num_count = Counter(nums) 15 | # 当所有频数都为1时,可得到消除重复的数字 16 | if all([True if x[1] == 1 else False for x in num_count.items()]): 17 | paths.append(int("".join(nums))) 18 | return 19 | 20 | for i, num in enumerate(nums): 21 | if num_count[num] > 1: 22 | nums.pop(i) 23 | backstracking(nums, paths) 24 | nums.insert(i, num) 25 | 26 | 27 | def solve_method(num): 28 | nums = list(str(num)) 29 | paths = [] 30 | backstracking(nums, paths) 31 | 32 | return max(paths) 33 | 34 | 35 | if __name__ == '__main__': 36 | assert solve_method(12341) == 2341 37 | assert solve_method(42234) == 423 38 | -------------------------------------------------------------------------------- /codes/others100/038_remaining-available-character-set.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 038_remaining-available-character-set.py 6 | @time: 2023/8/8 13:36 7 | @project: huawei-od-python 8 | @desc: 038 剩余可用字符集 9 | """ 10 | 11 | 12 | def solve_method(line): 13 | total, used = line.split("@") 14 | 15 | def to_counter(chars): 16 | chars = chars.split(",") 17 | chars_map = {} 18 | for char in chars: 19 | char = char.split(":") 20 | chars_map[char[0]] = int(char[1]) 21 | return chars_map 22 | 23 | total_map = to_counter(total) 24 | if used: 25 | used_map = to_counter(used) 26 | for k in used_map.keys(): 27 | diff = total_map[k] - used_map[k] 28 | if diff > 0: 29 | total_map[k] = diff 30 | else: 31 | total_map.pop(k) 32 | 33 | return ",".join([k + ":" + str(v) for k, v in total_map.items()]) 34 | 35 | 36 | if __name__ == '__main__': 37 | assert solve_method("a:3,b:5,c:2@a:1,b:2") == "a:2,b:3,c:2" 38 | assert solve_method("a:3,b:5,c:2@") == "a:3,b:5,c:2" 39 | -------------------------------------------------------------------------------- /codes/others100/039_pythagorean-triple.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 039_pythagorean-triple.py 6 | @time: 2023/8/8 14:34 7 | @project: huawei-od-python 8 | @desc: 勾股数 9 | """ 10 | import math 11 | 12 | 13 | def solve_method(n, m): 14 | result = [] 15 | 16 | # 判断是否是互质 17 | def is_coprime(a, b, c): 18 | return math.gcd(a, b) == 1 and math.gcd(b, c) == 1 and math.gcd(a, c) == 1 19 | 20 | # 求勾股数A、B、C 21 | for a in range(n, m + 1): 22 | for b in range(a + 1, m + 1): 23 | # c^2 = i^2+b^2 24 | c_squared = a ** 2 + b ** 2 25 | c = int(c_squared ** 0.5) 26 | if c_squared == c ** 2 and c <= m and is_coprime(a, b, c): 27 | result.append([a, b, c]) 28 | 29 | return result if result else "Na" 30 | 31 | 32 | if __name__ == '__main__': 33 | assert solve_method(1, 20) == [[3, 4, 5], [5, 12, 13], [8, 15, 17]] 34 | assert solve_method(5, 10) == "Na" 35 | -------------------------------------------------------------------------------- /codes/others100/040_reverse-words.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 040_reverse-words.py 6 | @time: 2023/7/27 10:27 7 | @project: huawei-od-python 8 | @desc: 040 单词反转 9 | """ 10 | 11 | 12 | def solve_method(line, start, end): 13 | words = line.strip().split(" ") 14 | if end > len(words) - 1: 15 | end = len(words) - 1 16 | if len(words) == 0 or start < 0 or end <= start: 17 | return line 18 | 19 | # 提取区域内的字符串并反转 20 | sub_words = words[start:end + 1] 21 | sub_words.reverse() 22 | # 合并字符串数组并输出 23 | if end + 1 < len(words): 24 | result = words[:start] + sub_words + words[end + 1:] 25 | else: 26 | result = words[:start] + sub_words 27 | 28 | return " ".join(result) 29 | 30 | 31 | if __name__ == '__main__': 32 | assert solve_method("I am a developer.", 0, 3) == "developer. a am I" 33 | assert solve_method("hello world!", 0, 3) == "world! hello" 34 | assert solve_method("I am a developer", 1, 2) == "I a am developer" 35 | assert solve_method("hello world", -1, 1) == "hello world" 36 | -------------------------------------------------------------------------------- /codes/others100/042_words-chain.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 042_words-chain.py 6 | @time: 2023/7/27 11:06 7 | @project: huawei-od-python 8 | @desc: 042 单词接龙 9 | """ 10 | 11 | 12 | def solve_method(words, k): 13 | result = words[k] 14 | words.pop(k) 15 | words = sorted(words, key=lambda x: (-len(x), x)) 16 | index = 0 17 | while words and index < len(words): 18 | # 判断是否为前一个单词的尾字母 19 | if words[index].startswith(result[-1]): 20 | result += words[index] 21 | words.pop(index) 22 | # 遍历的时候,确保从第一个单词开始遍历 23 | index = 0 24 | else: 25 | # 继续遍历 26 | index += 1 27 | return result 28 | 29 | 30 | if __name__ == '__main__': 31 | words = ["word", "dd", "da", "dc", "dword", "d"] 32 | assert solve_method(words, 0) == "worddwordda" 33 | 34 | words = ["word", "dd", "da", "dc", "dword", "d"] 35 | assert solve_method(words, 4) == "dwordda" 36 | -------------------------------------------------------------------------------- /codes/others100/043_max-cards-number.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 043_max-cards-number.py 6 | @time: 2023/7/27 13:30 7 | @project: huawei-od-python 8 | @desc: 043 卡片组成的最大数字 9 | """ 10 | 11 | 12 | def solve_method(strings): 13 | length = len(strings) 14 | 15 | for i in range(length): 16 | for j in range(i + 1, length): 17 | if int(strings[i] + strings[j]) < int(strings[j] + strings[i]): 18 | strings[i], strings[j] = strings[j], strings[i] 19 | 20 | return "".join(strings) 21 | 22 | 23 | if __name__ == '__main__': 24 | assert solve_method(["22", "221"]) == "22221" 25 | assert solve_method(["4589", "101", "41425", "9999"]) == "9999458941425101" 26 | -------------------------------------------------------------------------------- /codes/others100/044_decode-compressed-string.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 044_decode-compressed-string.py 6 | @time: 2023/7/27 14:04 7 | @project: huawei-od-python 8 | @desc: 044 压缩报文还原 9 | """ 10 | 11 | 12 | def solve_method(string): 13 | prev, current = "", "" 14 | stack = [] 15 | num = 0 16 | for i in range(len(string)): 17 | if string[i].isdigit(): 18 | num = num * 10 + int(string[i]) 19 | elif string[i] == "[": 20 | stack.append(current) 21 | stack.append(num) 22 | current = "" 23 | num = 0 24 | elif string[i] == "]": 25 | curr_num = stack.pop() 26 | prev = stack.pop() 27 | current = prev + curr_num * current 28 | else: 29 | current += string[i] 30 | 31 | return current 32 | 33 | 34 | if __name__ == '__main__': 35 | assert solve_method("3[k]2[mn]") == "kkkmnmn" 36 | assert solve_method("3[m2[c]]") == "mccmccmcc" 37 | -------------------------------------------------------------------------------- /codes/others100/045_sum-number.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 045_sum-number.py 6 | @time: 2023/7/27 14:39 7 | @project: huawei-od-python 8 | @desc: 045 去重求和 9 | """ 10 | 11 | 12 | def solve_method(nums, n): 13 | nums = sorted(set(nums)) 14 | result = -1 15 | if len(nums) >= 2 * n: 16 | result = sum(nums[:n]) + sum(nums[-n:]) 17 | 18 | return result 19 | 20 | 21 | if __name__ == '__main__': 22 | assert solve_method([95, 88, 83, 64, 100], 2) == 342 23 | assert solve_method([3, 2, 3, 4, 2], 2) == -1 24 | -------------------------------------------------------------------------------- /codes/others100/046_double-eleven.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: Kaiwen Zuo 5 | @file: 046_double-eleven.py 6 | @time: 2023/08/11 0:47 7 | @project: huawei-od-python 8 | @desc: 046 双十一 9 | """ 10 | 11 | 12 | def solve_method(M, R): 13 | """ 14 | :param M: 商品价格数组 15 | :param R: 购买资金的额度 16 | """ 17 | # 按照商品价格从小到大排序 18 | M.sort() 19 | 20 | # 检查是否有至少3个商品,且最便宜的3个商品的总价不超过R 21 | if len(M) < 3 or sum(M[:3]) > R: 22 | return -1 23 | 24 | max_price = -1 25 | for i in range(len(M) - 2): 26 | # 左指针从当前元素的下一个位置开始 27 | left = i + 1 28 | # 右指针从数组的最后一个位置开始 29 | right = len(M) - 1 30 | while left < right: 31 | current_sum = M[i] + M[left] + M[right] 32 | if max_price < current_sum <= R: 33 | max_price = current_sum 34 | left += 1 35 | else: 36 | right -= 1 37 | 38 | return max_price 39 | 40 | 41 | if __name__ == '__main__': 42 | assert solve_method([23, 26, 36, 27], 78) == 76 43 | assert solve_method([23, 30, 40], 26) == -1 44 | -------------------------------------------------------------------------------- /codes/others100/047_rearrange-books.py: -------------------------------------------------------------------------------- 1 | # !/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: Kaiwen Zuo 5 | @file: 047_rearrange-books.py 6 | @time: 2023/08/11 0:47 7 | @project: huawei-od-python 8 | @desc: 047 叠放书籍 9 | """ 10 | 11 | 12 | def solve_method(books): 13 | # 按照长度从大到小、宽度从大到小排序 14 | books.sort(key=lambda x: (x[0], x[1]), reverse=True) 15 | 16 | # 可叠放的书的个数 17 | count = 1 18 | # 前一本书 19 | prev = books[0] 20 | for book in books[1:]: 21 | # 如果当前书的长度和宽度都小于前一本书,则个数加1 22 | if prev[0] > book[0] and prev[1] > book[1]: 23 | count += 1 24 | prev = book 25 | 26 | return count 27 | 28 | 29 | if __name__ == '__main__': 30 | assert solve_method([[20, 16], [15, 11], [10, 10], [9, 10]]) == 3 31 | -------------------------------------------------------------------------------- /codes/others100/048_hot-pot.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 048_hot-pot.py 6 | @time: 2023/7/27 14:56 7 | @project: huawei-od-python 8 | @desc: 048 吃火锅 9 | """ 10 | 11 | 12 | def solve_method(dishes, m): 13 | dish_cooked_time = [] 14 | for dish in dishes: 15 | dish_cooked_time.append(dish[0] + dish[1]) 16 | # 得到每个菜可以吃的时刻 17 | dish_cooked_time.sort() 18 | 19 | # 上一个吃到菜的时刻 20 | pre_time = dish_cooked_time[0] 21 | count = 1 22 | for i in range(1, len(dishes)): 23 | # 如果等待的时间能吃到这次的菜 24 | if dish_cooked_time[i] >= pre_time + m: 25 | # 记录吃到菜的时刻 26 | pre_time = dish_cooked_time[i] 27 | # 累计次数 28 | count += 1 29 | 30 | return count 31 | 32 | 33 | if __name__ == '__main__': 34 | dishes = [[1, 2], 35 | [2, 1]] 36 | assert solve_method(dishes, 1) == 1 37 | 38 | dishes = [[1, 2], 39 | [1, 3], 40 | [2, 3]] 41 | assert solve_method(dishes, 1) == 3 42 | -------------------------------------------------------------------------------- /codes/others100/049_the-boundary-value-of-the-matrix-element.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 010_the-boundary-value-of-the-matrix-element.py 6 | @time: 2023/9/1 16:15 7 | @project: huawei-od-python 8 | @desc: 049 矩阵元素的边界值 9 | """ 10 | 11 | 12 | def solve_method(arr): 13 | result_lst = arr[0] 14 | for lst in arr[1:]: 15 | result_lst = [max(x, y) for x, y in zip(result_lst, lst)] 16 | 17 | result_lst.sort() 18 | return result_lst[0] 19 | 20 | 21 | if __name__ == '__main__': 22 | arr = [[1, 2], [3, 4]] 23 | assert solve_method(arr) == 3 24 | 25 | arr = [[1, 2]] 26 | assert solve_method(arr) == 1 27 | 28 | 29 | -------------------------------------------------------------------------------- /codes/others100/050_highway-charging-planning.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 050_highway-charging-planning.py 6 | @time: 2023/08/11 0:47 7 | @project: huawei-od-python 8 | @desc: 050 高速公路充电技术 9 | """ 10 | 11 | 12 | def solve_method(D, N, sites): 13 | # 已行驶公里路程 14 | pos = 0 15 | # 行驶时间 16 | driving_time = D // 100 17 | # 休息时间 18 | total_rest_time = 0 19 | while D > 1000: 20 | # 当前1000公里内的充电站 21 | _sites = [x for x in sites if pos <= x[0] <= pos + 1000] 22 | # 将充电站按照充电排队时间从小到大排序 23 | _sites = sorted(_sites, key=lambda x: x[1]) 24 | # 进站排队充电 25 | pos, rest_time = _sites[0] 26 | # 充电时间 27 | rest_time += 1 28 | total_rest_time += rest_time 29 | D -= pos 30 | 31 | total_time = driving_time + total_rest_time 32 | return total_time 33 | 34 | 35 | if __name__ == "__main__": 36 | sites = [[300, 2], 37 | [600, 1], 38 | [1000, 0], 39 | [1200, 0]] 40 | assert solve_method(1500, 4, sites) == 16 41 | 42 | sites = [[300, 0], 43 | [600, 0]] 44 | assert solve_method(800, 2, sites) == 8 45 | -------------------------------------------------------------------------------- /codes/others100/052_respond-message-time.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: Kaiwen Zuo 5 | @file: 052_respond-message-time.py 6 | @time: 2023/08/11 0:47 7 | @project: huawei-od-python 8 | @desc: 052 报文响应时间 9 | """ 10 | import math 11 | 12 | 13 | def solve_method(N, response_times): 14 | min_value = math.inf 15 | for [T, M] in response_times: 16 | if M >= 128: 17 | # 提取M的位4-7,与0x10进行逻辑或操作,然后根据M的位0-2值左移 18 | M = ((M >> 3) & 0xF | 0x10) << (M & 0x7 + 3) 19 | resp_time = T + M 20 | if resp_time < min_value: 21 | min_value = resp_time 22 | 23 | return min_value 24 | 25 | 26 | if __name__ == '__main__': 27 | response_times = [[0, 20], 28 | [1, 10], 29 | [8, 20]] 30 | assert solve_method(3, response_times) == 11 31 | 32 | response_times = [[0, 255], 33 | [200, 60]] 34 | assert solve_method(2, response_times) == 260 35 | -------------------------------------------------------------------------------- /codes/others100/054_shout-seven.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: Kaiwen Zuo 5 | @file: 054_shout-seven.py 6 | @time: 2023/08/11 0:47 7 | @project: huawei-od-python 8 | @desc: 054 喊七 9 | """ 10 | 11 | 12 | def solve_method(nums): 13 | result = [0] * len(nums) 14 | N = 7 * (sum(nums) + len(nums)) 15 | j = 0 16 | for i in range(1, N): 17 | # 判断当前j在列表中的位置 18 | j %= len(nums) 19 | # 如果当前数字是7的倍数或者含有7 20 | if i % 7 == 0 or '7' in str(i): 21 | result[j] += 1 22 | # 如果结果列表的总和等于输入数字的总和,终止循环 23 | if sum(result) == sum(nums): 24 | break 25 | j += 1 26 | 27 | return result 28 | 29 | 30 | if __name__ == "__main__": 31 | assert solve_method([0, 1, 0]) == [1, 0, 0] 32 | assert solve_method([0, 0, 0, 2, 1]) == [0, 2, 0, 1, 0] 33 | -------------------------------------------------------------------------------- /codes/others100/055_good-friend.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: Kaiwen Zuo 5 | @file: 055_good-friend.py 6 | @time: 2023/08/11 0:47 7 | @project: huawei-od-python 8 | @desc: 055 好朋友 9 | """ 10 | 11 | 12 | def solve_method(n, heights): 13 | result = [] 14 | 15 | for i in range(n): 16 | pos = 0 17 | for j in range(i + 1, n): 18 | if heights[j] > heights[i]: 19 | # 将找到的第一个更大的数的位置存入列表中 20 | pos = j 21 | break 22 | 23 | result.append(pos) 24 | 25 | return result 26 | 27 | 28 | if __name__ == "__main__": 29 | assert solve_method(2, [100, 95]) == [0, 0] 30 | assert solve_method(8, [123, 124, 125, 121, 119, 122, 126, 123]) == [1, 2, 6, 5, 5, 6, 0, 0] 31 | -------------------------------------------------------------------------------- /codes/others100/056_the-length-of-substring.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: Kaiwen Zuo 5 | @file: 056_the-length-of-substring.py 6 | @time: 2023/08/11 0:47 7 | @project: huawei-od-python 8 | @desc: 056 子序列长度 9 | """ 10 | 11 | 12 | def solve_method(nums, target_sum): 13 | max_len = -1 14 | 15 | if sum(nums) < target_sum: 16 | return -1 17 | 18 | left, right = 0, 1 19 | while left < len(nums): 20 | sub_sum = sum(nums[left:right]) 21 | if right == len(nums) - 1 and sub_sum < target_sum: 22 | break 23 | 24 | if sub_sum == target_sum: 25 | length = right - left 26 | if max_len < length: 27 | max_len = length 28 | left += 1 29 | elif sub_sum > target_sum: 30 | left += 1 31 | else: 32 | right += 1 33 | 34 | return max_len 35 | 36 | 37 | if __name__ == '__main__': 38 | nums = [1, 2, 3, 4, 2] 39 | assert solve_method(nums, 6) == 3 40 | 41 | nums = [1, 2, 3, 4, 2] 42 | assert solve_method(nums, 20) == -1 43 | -------------------------------------------------------------------------------- /codes/others100/057_letter-elimination.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: Kaiwen Zuo 5 | @file: 057_letter-elimination.py 6 | @time: 2023/08/14 21:00 7 | @project: huawei-od-python 8 | @desc: 057 字母消消乐 9 | """ 10 | 11 | 12 | def solve_method(chars): 13 | # 仅保留字母 14 | lst = [c for c in chars if c.isalpha()] 15 | 16 | if len(lst) == 0: 17 | # 如果列表中没有字母,则是异常输入,返回0 18 | return 0 19 | 20 | i = 0 21 | while i < len(lst) - 1: 22 | if lst[i] == lst[i + 1]: 23 | # 当前字符与后一个字符相同时,则删除重复项 24 | del lst[i:i + 2] 25 | # 位置倒退一个 26 | i = max(0, i - 1) 27 | else: 28 | i += 1 29 | return len(lst) 30 | 31 | 32 | if __name__ == '__main__': 33 | assert solve_method("gg") == 0 34 | assert solve_method("mMbccbc") == 3 35 | -------------------------------------------------------------------------------- /codes/others100/058_letter-combination.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: Kaiwen Zuo 5 | @file: 058_letter-combination.py 6 | @time: 2023/08/14 21:00 7 | @project: huawei-od-python 8 | @desc: 058 字母组合 9 | """ 10 | 11 | import itertools 12 | 13 | # 定义数字到字母的映射 14 | digits_mapping = [ 15 | ["a", "b", "c"], 16 | ["d", "e", "f"], 17 | ["g", "h", "i"], 18 | ["j", "k", "l"], 19 | ["m", "n", "o"], 20 | ["p", "q", "r"], 21 | ["s", "t"], 22 | ["u", "v"], 23 | ["w", "x"], 24 | ["y", "z"] 25 | ] 26 | 27 | 28 | def solve_method(nums, block_words): 29 | nums = list(nums) 30 | words = [set(digits_mapping[int(num)]) for num in nums] 31 | # 得到各列表之间的笛卡尔积 32 | result = list(itertools.product(*words)) 33 | 34 | result = ["".join(x) for x in result if block_words not in "".join(x)] 35 | result.sort() 36 | return result 37 | 38 | 39 | if __name__ == "__main__": 40 | assert solve_method("78", "ux") == ["uw", "vw", "vx"] 41 | assert solve_method("78", "x") == ["uw", "vw"] 42 | -------------------------------------------------------------------------------- /codes/others100/059_letter-counts.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: Kaiwen Zuo 5 | @file: 059_letter-counts.py 6 | @time: 2023/08/16 13:12 7 | @project: huawei-od-python 8 | @desc: 059 字母计数 9 | """ 10 | from collections import Counter 11 | 12 | 13 | def solve_method(line): 14 | # 使用Counter计算每个字符的出现次数 15 | char_counter = Counter(line) 16 | 17 | # 按字母出现次数从大到小,如果相等,按照自然顺序排序,小写字母在前,大写字母在后 18 | char_count_pairs = sorted(char_counter.items(), key=lambda x: (-x[1], x[0].isupper())) 19 | 20 | result = "" 21 | for char, count in char_count_pairs: 22 | # 每个字符及其出现次数 23 | result += f"{char}:{count};" 24 | 25 | return result 26 | 27 | 28 | if __name__ == "__main__": 29 | assert solve_method("xyxyXX") == "x:2;y:2;X:2;" 30 | assert solve_method("abababb") == "b:4;a:3;" 31 | -------------------------------------------------------------------------------- /codes/others100/060_string-encryption.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: zhangchao 5 | @file: 060_string-encryption.py 6 | @time: 2023/7/29 23:55 7 | @project: huawei-od-python 8 | @desc: 060 字符串加密 9 | """ 10 | 11 | 12 | def solve_method(strings): 13 | offsets = [0] * 50 14 | offsets[0:2] = [1, 2, 4] 15 | for i in range(3, 51): 16 | offsets[i] = offsets[i - 1] + offsets[i - 2] + offsets[i - 3] 17 | 18 | result = [] 19 | for chars in strings: 20 | chars = list(chars) 21 | for i in range(len(chars)): 22 | c = chars[i] 23 | chars[i] = chr((ord(c) - 97 + offsets[i]) % 26 + 97) 24 | result.append("".join(chars)) 25 | return result 26 | 27 | 28 | if __name__ == '__main__': 29 | assert solve_method(["xy"]) == ["ya"] 30 | assert solve_method(["xyabcdef", "abcdefghijk"]) == ["yaeipbwi", "bdgkrdykbxu"] 31 | -------------------------------------------------------------------------------- /codes/others100/063_escape-room-game.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: zhangchao 5 | @file: 063_escape-room-game 6 | @time: 2023/8/17 10:48 7 | @project: huawei-od-python 8 | @desc: 063 密室逃生游戏 9 | """ 10 | 11 | 12 | def solve_method(key, boxes) -> int: 13 | key_lst = list(key) 14 | for index, chars in enumerate(boxes): 15 | chars = [char.lower() for char in chars if char.isalpha()] 16 | chars.sort() 17 | if chars == key_lst: 18 | return index 19 | return -1 20 | 21 | 22 | if __name__ == '__main__': 23 | assert solve_method("abc", ["s", "sdf134", "A2c4b"]) == 2 24 | assert solve_method("abc", ["s", "sdf134", "A2c4bd", "523[]"]) == -1 25 | -------------------------------------------------------------------------------- /codes/others100/065_determine-the-string-subsequence.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 012_determine-the-string-subsequence.py 6 | @time: 2023/9/1 18:11 7 | @project: huawei-od-python 8 | @desc: 065 判断字符串子序列 9 | """ 10 | 11 | 12 | def solve_method(target, source): 13 | t_pos = len(target) - 1 14 | s_pos = len(source) - 1 15 | 16 | # 由于获取下标较大的位置,则从后向前遍历 17 | while t_pos >= 0 and s_pos >= 0: 18 | if target[t_pos] == source[s_pos]: 19 | t_pos -= 1 20 | if t_pos < 0: 21 | # 当遍历得到第一个子序列,则可以返回当前下标 22 | return s_pos 23 | 24 | s_pos -= 1 25 | 26 | return -1 27 | 28 | 29 | if __name__ == '__main__': 30 | assert solve_method("abc", "abcaybec") == 3 31 | -------------------------------------------------------------------------------- /codes/others100/066_find-path.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: zhangchao 5 | @file: 066_find-path 6 | @time: 2023/8/14 14:51 7 | @project: huawei-od-python 8 | @desc: 066 寻找路径 9 | """ 10 | import math 11 | 12 | 13 | def solve_method(nums): 14 | nums.insert(0, 0) 15 | # nums第一个元素无意义,为额外添加元素,目的是对齐下标 16 | depth = math.ceil(math.log2(len(nums))) 17 | # 得到最后一层节点的索引 18 | start = 2 ** (depth - 1) 19 | min_value, index = math.inf, -1 20 | # 遍历该层的所有节点 21 | for i in range(start, len(nums)): 22 | if nums[i] != -1 and nums[i] < min_value: 23 | # 取出最小的叶子节点 24 | min_value, index = nums[i], i 25 | # 得到该节点的路径 26 | path = [] 27 | while index > 0: 28 | path.append(nums[index]) 29 | index = index // 2 30 | return path[::-1] 31 | 32 | 33 | if __name__ == '__main__': 34 | tree_nodes = [3, 5, 7, -1, -1, 2, 4] 35 | assert solve_method(tree_nodes) == [3, 7, 2] 36 | 37 | tree_nodes = [5, 9, 8, -1, -1, 7, -1, -1, -1, -1, -1, 6] 38 | assert solve_method(tree_nodes) == [5, 8, 7, 6] 39 | -------------------------------------------------------------------------------- /codes/others100/067_search-continuous-scope.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: zhangchao 5 | @file: 067_search-continuous-scope 6 | @time: 2023/8/13 23:38 7 | @project: huawei-od-python 8 | @desc: 067 寻找连续区间 9 | """ 10 | 11 | 12 | def solve_method(nums, target): 13 | n = len(nums) 14 | count = 0 15 | if sum(nums) < target: 16 | return count 17 | for i in range(n): 18 | for j in range(i, n): 19 | if sum(nums[i:j + 1]) >= target: 20 | count += 1 21 | return count 22 | 23 | 24 | if __name__ == '__main__': 25 | assert solve_method([3, 4, 7], 7) == 4 26 | assert solve_method([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 10000) == 0 27 | -------------------------------------------------------------------------------- /codes/others100/068_shot-competition.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: zhangchao 5 | @file: 068_shot-competition 6 | @time: /2023/8/23 17:24 7 | @project: huawei-od-python 8 | @desc: 068 射击比赛 9 | """ 10 | from collections import defaultdict 11 | 12 | 13 | def solve_method(players, scores): 14 | # 选手分数字典,key为选手id,value为选手得分 15 | id2score = defaultdict(list) 16 | for player_id, score in zip(players, scores): 17 | id2score[player_id].append(score) 18 | 19 | # 如果一个选手成绩小于3个,则忽略该选手 20 | id2score = dict(filter(lambda x: len(x[1]) > 2, id2score.items())) 21 | # 累加所有选手最高三个分数之和 22 | id2score = {k: sum(sorted(v, reverse=True)[:3]) for k, v in id2score.items()} 23 | # 对总和进行从大到小、对ID进行从大到小排序 24 | id2score = dict(sorted(id2score.items(), key=lambda x: (-x[1], -x[0]))) 25 | # 返回选手ID 26 | return [x for x in id2score.keys()] 27 | 28 | 29 | if __name__ == '__main__': 30 | players = [3, 3, 7, 4, 4, 4, 4, 7, 7, 3, 5, 5, 5] 31 | scores = [53, 80, 68, 24, 39, 76, 66, 16, 100, 55, 53, 80, 55] 32 | assert solve_method(players, scores) == [5, 3, 7, 4] 33 | -------------------------------------------------------------------------------- /codes/others100/069_children-order.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: zhangchao 5 | @file: 069_children-order 6 | @time: 2023/8/23 19:19 7 | @project: huawei-od-python 8 | @desc: 069 小朋友排队 9 | """ 10 | 11 | 12 | def solve_method(nums): 13 | # 检查参数是否合法 14 | if all([False if str(x).isdigit() else True for x in nums]): 15 | return [] 16 | 17 | for i in range(len(nums) - 1): 18 | if i & 1 == 1 and nums[i] > nums[i + 1]: 19 | # 当索引为奇数时,如果当前的小朋友身高比后一个小朋友的身高要高,则需要交换 20 | nums[i], nums[i + 1] = nums[i + 1], nums[i] 21 | elif i & 1 == 0 and nums[i] < nums[i + 1]: 22 | # 当索引为偶数时,0也算作偶数,如果当前的小朋友身高比后一个小朋友的身高要矮,则需要交换 23 | nums[i], nums[i + 1] = nums[i + 1], nums[i] 24 | 25 | return nums 26 | 27 | 28 | if __name__ == '__main__': 29 | assert solve_method([4, 1, 3, 5, 2]) == [4, 1, 5, 2, 3] 30 | assert solve_method([1, 1, 1, 1, 1]) == [1, 1, 1, 1, 1] 31 | assert solve_method(["xxx"]) == [] 32 | -------------------------------------------------------------------------------- /codes/others100/070_broadcast-server.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: zhangchao 5 | @file: 070_broadcast-server 6 | @time: 2023/8/23 19:46 7 | @project: huawei-od-python 8 | @desc: 070 广播服务器 9 | """ 10 | 11 | 12 | def solve_method(nums): 13 | N = len(nums) 14 | cover = set() 15 | count = 0 16 | 17 | for i in range(N): 18 | if i not in cover: 19 | count += 1 20 | cover.add(i) 21 | for j in filter(lambda x: x != i, range(N)): 22 | if nums[i][j] == 1: 23 | cover.add(j) 24 | 25 | return count 26 | 27 | 28 | if __name__ == '__main__': 29 | nums = [[1, 0, 0], 30 | [0, 1, 0], 31 | [0, 0, 1]] 32 | assert solve_method(nums) == 3 33 | 34 | nums = [[1, 1], 35 | [1, 1]] 36 | assert solve_method(nums) == 1 37 | -------------------------------------------------------------------------------- /codes/others100/071_seat-adjustment.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: zhangchao 5 | @file: 071_seat-adjustment 6 | @time: 2023/8/24 10:31 7 | @project: huawei-od-python 8 | @desc: 071 座位调整 9 | """ 10 | 11 | 12 | def solve_method(nums): 13 | if len(nums) < 3: 14 | return 0 if sum(nums) > 0 else 1 15 | 16 | if sum(nums) >= len(nums) // 2 + 1: 17 | return 0 18 | ans = 0 19 | n = len(nums) 20 | for i in range(n): 21 | if nums[i] == 1: 22 | continue 23 | elif i == 0 and nums[i + 1] == 0: 24 | # 如果第一个位置和第二个位置是空的,则第一个位置可坐人 25 | ans += 1 26 | nums[i] = 1 27 | elif i == n - 1 and nums[i - 1] == 0: 28 | # 如果最后一个位置和倒数第二个位置是空的,则最后一个位置可坐人 29 | ans += 1 30 | nums[i] = 1 31 | else: 32 | # 如果前一个和后一个均为0,则当前位置可坐人 33 | if nums[i - 1] == 0 and nums[i + 1] == 0: 34 | ans += 1 35 | nums[i] = 1 36 | return ans 37 | 38 | 39 | if __name__ == '__main__': 40 | assert solve_method([1, 0, 0, 0, 1]) == 1 41 | assert solve_method([0, 0, 0, 0, 0]) == 3 42 | assert solve_method([1, 0, 0, 0, 0, 0]) == 2 43 | -------------------------------------------------------------------------------- /codes/others100/072_express-truck.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: zhangchao 5 | @file: 072_express-truck 6 | @time: 2023/8/24 11:58 7 | @project: huawei-od-python 8 | @desc: 072 快递货车 9 | """ 10 | 11 | 12 | def solve_method(nums, weight): 13 | nums.sort() 14 | count = 0 15 | sum_weight = 0 16 | for i in range(len(nums)): 17 | sum_weight += nums[i] 18 | if sum_weight > weight: 19 | return count 20 | else: 21 | count += 1 22 | return count 23 | 24 | 25 | if __name__ == '__main__': 26 | assert solve_method([5, 10, 2, 11], 20) == 3 27 | -------------------------------------------------------------------------------- /codes/others100/074_total-fastest-detection-efficiency.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: zhangchao 5 | @file: 074_total-fastest-detection-efficiency 6 | @time: 2023/8/24 16:42 7 | @project: huawei-od-python 8 | @desc: 074 总最快检测效率 9 | """ 10 | 11 | 12 | def solve_method(samplers, num_volunteer): 13 | profit = [] 14 | for p in samplers: 15 | # 当有1个志愿者,增加20% 16 | # 每增加1个志愿者,效率提升10%,最多提升30% 17 | profit.extend([p * 0.2, p * 0.1, p * 0.1, p * 0.1]) 18 | # 没有志愿者协助时,每个采样员的效率是80% 19 | samplers = [i * 0.8 for i in samplers] 20 | # 选取志愿者能提升效率最高的分配给采样员,计算总和 21 | return sum(samplers) + sum(sorted(profit, reverse=True)[:num_volunteer]) 22 | 23 | 24 | if __name__ == '__main__': 25 | samplers = [200, 200] 26 | assert solve_method(samplers, 2) == 400 27 | -------------------------------------------------------------------------------- /codes/others100/075_recover-array.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: zhangchao 5 | @file: 075_recover-array 6 | @time: 2023/8/24 17:04 7 | @project: huawei-od-python 8 | @desc: 075 恢复数字序列 9 | """ 10 | from collections import Counter 11 | 12 | 13 | def solve_method(s, n): 14 | counter = Counter(s) 15 | candidate = [val for val in range(n)] 16 | candidate_counter = Counter(''.join(map(str, candidate))) 17 | for new in range(n, 1001): 18 | if candidate_counter == counter: 19 | return candidate[0] 20 | else: 21 | prev = candidate.pop(0) 22 | candidate_counter -= Counter(str(prev)) 23 | 24 | candidate.append(new) 25 | candidate_counter += Counter(str(new)) 26 | return -1 27 | 28 | 29 | if __name__ == '__main__': 30 | assert solve_method("19801211", 5) == 8 31 | assert solve_method("432111111111", 4) == 111 32 | -------------------------------------------------------------------------------- /codes/others100/078_find-same-class-children.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: zhangchao 5 | @file: 078_find-same-class-children 6 | @time: 2023/8/23 23:57 7 | @project: huawei-od-python 8 | @desc: 078 找出同班小朋友 9 | """ 10 | 11 | 12 | def solve_method(line): 13 | try: 14 | classes = [[], []] 15 | class_no = 0 16 | for i, p in enumerate(line): 17 | p_id, p_flag = p.split("/") 18 | 19 | if p_flag == "N": 20 | # 如果不同班,则换到另一个班 21 | class_no = not class_no 22 | 23 | # 进行分班 24 | classes[class_no].append(int(p_id)) 25 | 26 | # 按照第一个编号小的排在第一行 27 | classes.sort(key=lambda x: x[0]) 28 | return classes 29 | except: 30 | return "ERROR" 31 | 32 | 33 | if __name__ == '__main__': 34 | assert solve_method(["1/N", "2/Y", "3/N", "4/Y"]) == [[1, 2], [3, 4]] 35 | -------------------------------------------------------------------------------- /codes/others100/082_find-characters.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 082_find-characters.py 6 | @time: 2023/8/8 17:17 7 | @project: huawei-od-python 8 | @desc: 082 找字符 9 | """ 10 | 11 | 12 | def solve_method(str1, str2): 13 | str1 = set(str1) 14 | str2 = set(str2) 15 | return "".join(sorted(str1.intersection(str2))) 16 | 17 | 18 | if __name__ == '__main__': 19 | assert solve_method("bach", "bbaaccddfg") == "abc" 20 | assert solve_method("fach", "bbaaccedfg") == "acf" 21 | -------------------------------------------------------------------------------- /codes/others100/083_find-number.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 083_find-number.py 6 | @time: 2023/8/9 16:35 7 | @project: huawei-od-python 8 | @desc: 083 找数字 9 | """ 10 | 11 | 12 | def solve_method(arr): 13 | result = 0 14 | n = len(arr) 15 | for ints in arr: 16 | max_sum = -1 17 | for i in range(n): 18 | # 循环左移 19 | bin_int = ints[i:] + ints[:i] 20 | max_sum = max(max_sum, int(''.join(map(str, bin_int)), 2)) 21 | 22 | result += max_sum 23 | 24 | return result 25 | 26 | 27 | if __name__ == '__main__': 28 | arr = [[1, 0, 0, 0, 1], 29 | [0, 0, 0, 1, 1], 30 | [0, 1, 0, 1, 0], 31 | [1, 0, 0, 1, 1], 32 | [1, 0, 1, 0, 1]] 33 | assert solve_method(arr) == 122 34 | -------------------------------------------------------------------------------- /codes/others100/084_find-the-minimum-number.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 084_find-the-minimum-number.py 6 | @time: 2023/8/9 20:55 7 | @project: huawei-od-python 8 | @desc: 084 找最小数 9 | """ 10 | 11 | 12 | def solve_method(num, n): 13 | nums = list(str(num)) 14 | # 使用栈来存储数字 15 | stack = [] 16 | # 表示需要移除的数字个数 17 | removed = 0 18 | 19 | for digit in nums: 20 | # 如果栈顶的数字大于当前数字且还有剩余的移除次数,则弹出栈顶数字 21 | while stack and removed < n and stack[-1] > digit: 22 | stack.pop() 23 | removed += 1 24 | stack.append(digit) 25 | 26 | # 如果还有剩余的数字需要移除 27 | while removed < n: 28 | stack.pop() 29 | removed += 1 30 | 31 | # 构建最终的结果 32 | return int("".join(stack)) 33 | 34 | 35 | if __name__ == '__main__': 36 | assert solve_method(2615371, 4) == 131 37 | assert solve_method(123456, 4) == 12 38 | -------------------------------------------------------------------------------- /codes/others100/085_find-the-endpoint.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 085_find-the-endpoint.py 6 | @time: 2023/8/10 14:46 7 | @project: huawei-od-python 8 | @desc: 9 | """ 10 | import math 11 | 12 | 13 | def solve_method(nums): 14 | n = len(nums) 15 | result = math.inf 16 | for i in range(1, n // 2): 17 | # 根据选择的步长,走出第1步 18 | step = 1 19 | current_index = i 20 | while current_index < n: 21 | # 当走到最后一个位置时,可以比较得到最少步数 22 | if current_index == n - 1: 23 | result = min(result, step) 24 | step += 1 25 | current_index += nums[current_index] 26 | 27 | return result if result != math.inf else -1 28 | 29 | 30 | if __name__ == '__main__': 31 | assert solve_method([1, 2, 3, 7, 1, 5, 9, 3, 2, 1]) == -1 32 | assert solve_method([7, 5, 9, 4, 2, 7, 1, 1, 1, 1, 1, 1, 1]) == 2 33 | -------------------------------------------------------------------------------- /codes/others100/086_sound-off.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 086_sound-off.py 6 | @time: 2023/8/10 15:30 7 | @project: huawei-od-python 8 | @desc: 086 报数 9 | """ 10 | 11 | 12 | def solve_method(M): 13 | if M <= 1 or M >= 100: 14 | return "ERROR!" 15 | 16 | nums = [i for i in range(1, 101)] 17 | # 用于M的计数 18 | count = 0 19 | # 当前遍历到的位置 20 | index = 0 21 | while len(nums) >= M: 22 | # 开始计数第count个 23 | count += 1 24 | if count == M: 25 | # 如果到达了第M个,则该位置的人退出 26 | nums.pop(index) 27 | # 从0开始重新计数到M 28 | count = 0 29 | else: 30 | # 继续下一个人 31 | index += 1 32 | 33 | if index == len(nums): 34 | # 如果已经循环了一圈,则从0位置开始继续循环报数 35 | index = 0 36 | 37 | return nums 38 | 39 | 40 | if __name__ == '__main__': 41 | assert solve_method(3) == [58, 91] 42 | assert solve_method(4) == [34, 45, 97] 43 | -------------------------------------------------------------------------------- /codes/others100/087_sound-off-game.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 087_sound-off-game.py 6 | @time: 2023/8/10 15:43 7 | @project: huawei-od-python 8 | @desc: 087 报数游戏 9 | """ 10 | def solve_method(n, m): 11 | if m <= 1 or m >= 100: 12 | return "ERROR!" 13 | 14 | nums = [i for i in range(1, n + 1)] 15 | # 用于m的计数 16 | count = 0 17 | # 当前遍历到的位置 18 | index = 0 19 | while len(nums) > 1: 20 | # 开始计数第count个 21 | count += 1 22 | if count == m: 23 | # 如果到达了第m个,则该位置的人退出 24 | nums.pop(index) 25 | # 从0开始重新计数到m 26 | count = 0 27 | else: 28 | # 继续下一个人 29 | index += 1 30 | 31 | if index == len(nums): 32 | # 如果已经循环了一圈,则从0位置开始继续循环报数 33 | index = 0 34 | 35 | return nums[0] 36 | 37 | 38 | if __name__ == '__main__': 39 | assert solve_method(15, 3) == 5 40 | assert solve_method(7, 4) == 2 41 | 42 | 43 | -------------------------------------------------------------------------------- /codes/others100/089_check-parentheses.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 089_check-parentheses.py 6 | @time: 2023/8/10 16:33 7 | @project: huawei-od-python 8 | @desc: 089 括号检查 9 | """ 10 | 11 | 12 | def solve_method(string): 13 | string = list(string) 14 | depth = 0 15 | stack = [] 16 | 17 | pares = { 18 | ")": "(", 19 | "]": "[", 20 | "}": "{" 21 | } 22 | 23 | for char in string: 24 | if char in ["(", "[", "{"]: 25 | stack.append(char) 26 | depth = max(depth, len(stack)) 27 | elif char in [")", "]", "}"]: 28 | if len(stack) == 0: 29 | return 0 30 | if stack[-1] == pares.get(char): 31 | stack.pop() 32 | else: 33 | return 0 34 | 35 | if len(stack) == 0: 36 | return depth 37 | else: 38 | return 0 39 | 40 | 41 | if __name__ == '__main__': 42 | assert solve_method("[]") == 1 43 | assert solve_method("([]{()})") == 3 44 | assert solve_method("(]") == 0 45 | assert solve_method("([)]") == 0 46 | assert solve_method(")(") == 0 47 | -------------------------------------------------------------------------------- /codes/others100/090_stitch-URLs.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 090_stitch-URLs.py 6 | @time: 2023/8/10 17:28 7 | @project: huawei-od-python 8 | @desc: 090 拼接URL 9 | """ 10 | 11 | 12 | def solve_method(urls): 13 | urls = urls.split(",") 14 | if len(urls) == 0: 15 | return "/" 16 | 17 | urls = "/".join([x.strip("/") for x in urls]) 18 | result = urls if urls == "/" else "/" + urls 19 | return result 20 | 21 | 22 | if __name__ == '__main__': 23 | assert solve_method("/acm,/bb") == "/acm/bb" 24 | assert solve_method("/abc/,/bcd") == "/abc/bcd" 25 | assert solve_method("/acd,bef") == "/acd/bef" 26 | assert solve_method(",") == "/" 27 | -------------------------------------------------------------------------------- /codes/others100/092_pick-the-string.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 092_pick-the-string.py 6 | @time: 2023/8/11 16:12 7 | @project: huawei-od-python 8 | @desc: 092 挑选字符串 9 | """ 10 | 11 | 12 | def solve_method(a, b): 13 | count = 0 14 | while True: 15 | chars = list(b) 16 | # 设置遍历索引 17 | pos = 0 18 | for ch in chars: 19 | # 找到了字符串b中的一个字符 20 | index = a.find(ch, pos) 21 | if index != -1: 22 | # 如果找到了,那就用`_`替换掉 23 | a = a.replace(ch, "_", 1) 24 | # 把找到的位置设置给索引 25 | pos = index 26 | else: 27 | return count 28 | # 找到一个,累计加1 29 | count += 1 30 | 31 | 32 | if __name__ == '__main__': 33 | assert solve_method("badc", "bac") == 1 34 | assert solve_method("badc", "abc") == 0 35 | assert solve_method("abacc", "abc") == 1 36 | assert solve_method("abcabc", "abc") == 2 37 | -------------------------------------------------------------------------------- /codes/others100/093_the-priority-queue.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 093_the-priority-queue.py 6 | @time: 2023/8/11 16:46 7 | @project: huawei-od-python 8 | @desc: 093 支持优先级的队列 9 | """ 10 | 11 | 12 | def solve_method(arr): 13 | arr = list(set(arr)) 14 | arr.sort(key=lambda x: (-x[1], x[0])) 15 | return [x[0] for x in arr] 16 | 17 | 18 | if __name__ == '__main__': 19 | assert solve_method([(10, 1), (20, 1), (30, 2), (40, 3)]) == [40, 30, 10, 20] 20 | assert solve_method([(10, 1), (10, 1), (30, 2), (40, 3)]) == [40, 30, 10] 21 | -------------------------------------------------------------------------------- /codes/others100/095_sequence-restore.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 095_sequence-restore.py 6 | @time: 2023/8/11 18:41 7 | @project: huawei-od-python 8 | @desc: 095 数列还原 9 | """ 10 | 11 | 12 | def solve_method(n): 13 | prev_chars = "1" 14 | 15 | # 从第2项开始生成 16 | for _ in range(1, n + 1): 17 | # 获取前一项 18 | current = prev_chars 19 | # 计数器初始化为1 20 | count = 1 21 | description = "" 22 | 23 | for i in range(1, len(current)): 24 | if current[i] == current[i - 1]: 25 | # 如果当前数字与前一个数字相同,计数器加1 26 | count += 1 27 | else: 28 | # 将计数和数字添加到描述中,重置计数器为1 29 | description += str(count) + current[i - 1] 30 | count = 1 31 | 32 | # 处理最后一个数字,得到前一个数的描述 33 | description += str(count) + current[-1] 34 | # 将描述保存到prev_chars变量中 35 | prev_chars = description 36 | 37 | return prev_chars 38 | 39 | 40 | if __name__ == '__main__': 41 | assert solve_method(0) == "1" 42 | assert solve_method(4) == "111221" 43 | -------------------------------------------------------------------------------- /codes/others100/097_the-numbers-game.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 097_the-numbers-game.py 6 | @time: 2023/8/11 20:51 7 | @project: huawei-od-python 8 | @desc: 097 数字游戏 9 | """ 10 | 11 | 12 | def solve_method(cases): 13 | result = [] 14 | for case in cases: 15 | m = case[0] 16 | pokers = case[1] 17 | 18 | # 累加和 19 | pre_sum = 0 20 | is_true = 0 21 | # 余数集合 22 | remainders = set() 23 | for poker in pokers: 24 | pre_sum = (pre_sum + poker) % m 25 | # 如果余数与前几次的余数相同,则表示其中一定存在连续累加为m的数字 26 | if pre_sum in remainders: 27 | is_true = 1 28 | break 29 | remainders.add(pre_sum) 30 | 31 | result.append(is_true) 32 | 33 | return result 34 | 35 | 36 | if __name__ == '__main__': 37 | cases = [[7, [4, 4, 2, 5, 3, 5, 5]], 38 | [11, [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]] 39 | assert solve_method(cases) == [1, 0] 40 | -------------------------------------------------------------------------------- /codes/others100/099_data-classification.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 099_data-classification.py 6 | @time: 2023/8/11 21:36 7 | @project: huawei-od-python 8 | @desc: 099 数据分类 9 | """ 10 | from collections import defaultdict 11 | 12 | 13 | def solve_method(c, b, nums): 14 | result = defaultdict(int) 15 | 16 | for num in nums: 17 | # 转成16进制 18 | hex_num = str(hex(num)) 19 | # 转成4字节,位数不够,填充0 20 | hex_num = hex_num[2:].zfill(8) 21 | sum_ = 0 22 | for i in range(0, 8, 2): 23 | sum_ += int(hex_num[i: i + 2], 16) 24 | 25 | if sum_ % b < c: 26 | # 统计有效类型的个数 27 | result[sum_ % b] += 1 28 | 29 | # 返回类型最多的个数 30 | return max(result.values()) 31 | 32 | 33 | if __name__ == '__main__': 34 | nums = [256, 257, 258, 259, 260, 261, 262, 263, 264, 265] 35 | assert solve_method(3, 4, nums) == 3 36 | 37 | nums = [256, 257, 258, 259, 260, 261, 262, 263, 264, 265] 38 | assert solve_method(1, 4, nums) == 2 39 | -------------------------------------------------------------------------------- /codes/others100/100_queuing-games.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 013_queuing-games.py 6 | @time: 2023/9/1 18:25 7 | @project: huawei-od-python 8 | @desc: 100 排队游戏 9 | """ 10 | 11 | 12 | def solve_method(k, prickers_pos, students): 13 | satisfied_value = 0 14 | 15 | for i in range(len(students)): 16 | if i not in prickers_pos: 17 | satisfied_value += sum([1 for j in range(0, i) if students[j] > students[i]]) 18 | 19 | if satisfied_value > k: 20 | return 1 21 | return 0 22 | 23 | 24 | if __name__ == '__main__': 25 | prickers_pos = [0, 1] 26 | students = [1810, 1809, 1801, 1802] 27 | assert solve_method(3, prickers_pos, students) == 1 28 | -------------------------------------------------------------------------------- /codes/others100/101_the-most-economical-way-to-back-up-data.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 101_the-most-economical-way-to-back-up-data.py 6 | @time: 2023/7/25 11:20 7 | @project: huawei-od-python 8 | @desc: 101 数据最节约的备份方法 9 | """ 10 | 11 | 12 | def dfs(arr, num): 13 | if len(arr) <= 1: 14 | num += len(arr) 15 | return num 16 | 17 | if arr[0] + arr[-1] < 500: 18 | arr[-1] += arr[0] 19 | arr.pop(0) 20 | elif arr[0] + arr[-1] == 500: 21 | arr.pop() 22 | arr.pop(0) 23 | num += 1 24 | else: 25 | arr.pop() 26 | num += 1 27 | return dfs(arr, num) 28 | 29 | 30 | def solve_method(arr): 31 | arr.sort() 32 | result = dfs(arr, 0) 33 | return result 34 | 35 | 36 | if __name__ == '__main__': 37 | assert solve_method([100, 500, 300, 200, 400]) == 3 38 | assert solve_method([100, 100, 200, 300]) == 2 39 | -------------------------------------------------------------------------------- /codes/others100/102_array-merge.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 102_array-merge.py 6 | @time: 2023/7/25 15:56 7 | @project: huawei-od-python 8 | @desc: 102 数组合并 9 | """ 10 | 11 | 12 | def solve_method(k, arr): 13 | result = [] 14 | index = 0 15 | while len(arr) > 0: 16 | num_list = arr[index] 17 | for i in range(k): 18 | if len(num_list) == 0: 19 | arr.pop(index) 20 | # 将位置指向前一个 21 | index -= 1 22 | break 23 | result.append(num_list.pop(0)) 24 | # 由于前面删除了一行,index需要加1,继续回到这一行的位置 25 | index += 1 26 | if index >= len(arr): 27 | index = 0 28 | return result 29 | 30 | 31 | if __name__ == '__main__': 32 | arr = [[2, 5, 6, 7, 9, 5, 7], 33 | [1, 7, 4, 3, 4]] 34 | assert solve_method(3, arr) == [2, 5, 6, 1, 7, 4, 7, 9, 5, 3, 4, 7] 35 | 36 | arr = [[1, 2, 3, 4, 5, 6], 37 | [1, 2, 3], 38 | [1, 2, 3, 4]] 39 | assert solve_method(4, arr) == [1, 2, 3, 4, 1, 2, 3, 1, 2, 3, 4, 5, 6] 40 | -------------------------------------------------------------------------------- /codes/others100/103_array-sort.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 103_array-sort.py 6 | @time: 2023/7/25 21:15 7 | @project: huawei-od-python 8 | @desc: 103 数组排序 9 | """ 10 | from collections import defaultdict 11 | 12 | 13 | def solve_method(nums): 14 | # key为数字,value为(位置, 个数) 15 | num_dict = {} 16 | for i, num in enumerate(nums): 17 | if num not in num_dict: 18 | num_dict[num] = (i, 1) 19 | else: 20 | count = num_dict[num][1] + 1 21 | num_dict[num] = (num_dict[num][0], count) 22 | 23 | result = sorted(num_dict.items(), key=lambda x: (-x[1][1], x[1][0])) 24 | return [x[0] for x in result] 25 | 26 | 27 | if __name__ == '__main__': 28 | assert solve_method([1, 3, 3, 3, 2, 4, 4, 4, 5]) == [3, 4, 1, 2, 5] 29 | -------------------------------------------------------------------------------- /codes/others100/104_array-make-up-minimum-number.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 104_array-make-up-minimum-number.py 6 | @time: 2023/7/25 21:40 7 | @project: huawei-od-python 8 | @desc: 104 数组组成的最小数字 9 | """ 10 | 11 | 12 | def solve_method(arr): 13 | len_arr = len(arr) 14 | arr.sort() 15 | nums_len = 2 if len_arr == 2 else 3 16 | nums_str = [str(arr[i]) for i in range(nums_len)] 17 | nums_str.sort() 18 | result = int(''.join(nums_str)) 19 | return result 20 | 21 | 22 | if __name__ == "__main__": 23 | assert solve_method([21, 30, 62, 5, 31]) == 21305 24 | assert solve_method([5,21]) == 215 25 | -------------------------------------------------------------------------------- /codes/others100/106_minimum-sum-of-integers.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: catcooc 5 | @file: 106_minimum-sum-of-integers.py 6 | @time: 2023-07-26 10:22:50 7 | @project: huawei-od-python 8 | @desc: 106 整数对最小和 9 | """ 10 | 11 | 12 | import heapq 13 | 14 | 15 | def solve_method(arr1, arr2, k): 16 | # 如果含有长度为1的数组,则返回该数组元素和另一个数组前k个元素的相加 17 | if len(arr1) == 1: 18 | return arr1[0] * k + sum(arr2[:k]) 19 | elif len(arr2) == 1: 20 | return arr2[0] * k + sum(arr1[:k]) 21 | sums = [] 22 | for x in arr1: 23 | for y in arr2: 24 | heapq.heappush(sums, x + y) 25 | 26 | return sum(heapq.nsmallest(k, sums)) 27 | 28 | 29 | if __name__ == "__main__": 30 | assert solve_method([1, 1, 2], [1, 2, 3], 2) == 4 31 | assert solve_method([1], [1, 2, 3], 2) == 5 32 | assert solve_method([1, 1, 2], [1, 2, 3], 2) == 4 33 | assert solve_method([1, 1, 1], [1, 2, 3], 3) == 6 34 | assert solve_method([1, 1], [1, 2, 3], 5) == 14 35 | assert solve_method([1, 1], [1, 2, 3], 6) == 18 36 | assert solve_method([1, 2, 3, 4], [1, 2, 3], 2) == 5 37 | -------------------------------------------------------------------------------- /codes/others100/107_integer-code.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: catcooc 5 | @file: 107_integer-code.py 6 | @time: 2023-07-26 14:01:22 7 | @project: huawei-od-python 8 | @desc: 107 整数编码 9 | """ 10 | 11 | 12 | def solve_method(num): 13 | # 得到二进制 14 | binary = bin(num)[2:] 15 | length = len(binary) 16 | result = "" 17 | # 从后向前,每7位遍历一次 18 | for i in range(length, 0, -7): 19 | start = max(i - 7, 0) 20 | bin_7bit = binary[start:i] 21 | # 位数不够7位左侧补0 22 | bin_7bit = bin_7bit.zfill(7) 23 | # 置1表示后面还有更多的字节,置0表示当前字节为最后一个字节 24 | bin_7bit = "0" + bin_7bit if i - 7 <= 0 else "1" + bin_7bit 25 | # 将二进制转成十六进制,并转成大写字母,然后位数不够2位,左边补0 26 | hex_7bit = hex(int(bin_7bit, 2)).upper()[2:].zfill(2) 27 | result += hex_7bit 28 | 29 | return result 30 | 31 | 32 | if __name__ == "__main__": 33 | assert solve_method(0) == '00' 34 | assert solve_method(100) == '64' 35 | assert solve_method(1000) == 'E807' 36 | -------------------------------------------------------------------------------- /codes/others100/111_new-job-number-system.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: catcooc 5 | @file: 111_new-job-number-system.py 6 | @time: 2023-07-28 11:33:18 7 | @project: huawei-od-python 8 | @desc: 111 新工号系统 9 | """ 10 | 11 | import math 12 | 13 | 14 | def solve_method(x, y): 15 | # 计算由y个字母能组成的工号个数 16 | cb = math.pow(26, y) 17 | result = 1 18 | if x > cb: 19 | # 取10为底的log,计算由多少个十数字组成工号。 20 | result = math.ceil(math.log(x / cb, 10)) 21 | return result 22 | 23 | 24 | if __name__ == "__main__": 25 | assert solve_method(260, 1) == 1 26 | assert solve_method(26, 1) == 1 27 | assert solve_method(2600, 1) == 2 28 | assert solve_method(27, 1) == 1 29 | assert solve_method(2601, 1) == 3 30 | -------------------------------------------------------------------------------- /codes/others100/112_spin-dice.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: catcooc 5 | @file: 112_spin-dice.py 6 | @time: 2023-07-28 12:09:14 7 | @project: huawei-od-python 8 | @desc: 112 骰子旋转 9 | """ 10 | 11 | 12 | def solve_method(line): 13 | res = ['1', '2', '3', '4', '5', '6'] 14 | 15 | roll_switch = { 16 | "L": lambda: roll(res, 0, 2, 4, 6), 17 | "R": lambda: roll(res, 4, 6, 0, 2), 18 | "F": lambda: roll(res, 2, 4, 4, 6), 19 | "B": lambda: roll(res, 4, 6, 2, 4), 20 | "A": lambda: roll(res, 2, 4, 0, 2), 21 | "C": lambda: roll(res, 0, 2, 2, 4) 22 | } 23 | for c in line: 24 | roll_switch[c]() 25 | print(res) 26 | 27 | return "".join(res) 28 | 29 | 30 | def roll(res, s1, e1, s2, e2): 31 | res[s1:e1], res[s2:e2] = res[s2:e2], res[s1:e1][::-1] 32 | return res 33 | 34 | 35 | if __name__ == "__main__": 36 | assert solve_method("LR") == "123456" 37 | # assert solve_method("FCR") == "342156" 38 | -------------------------------------------------------------------------------- /codes/others100/113_time-format.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: catcooc 5 | @file: 113_time-format.py 6 | @time: 2023-07-28 15:07:36 7 | @project: huawei-od-python 8 | @desc: 113 时间格式化 9 | """ 10 | 11 | 12 | def solve_method(times): 13 | sorted_times = sorted(times, key=get_time) 14 | return sorted_times 15 | 16 | 17 | def get_time(time_str: str): 18 | time_strs = time_str.split(':') 19 | h = int(time_strs[0]) 20 | m = int(time_strs[1]) 21 | s = int(time_strs[-1].split('.')[0]) 22 | n = int(time_strs[-1].split('.')[1]) 23 | return h * 60 * 60 * 1000 + m * 60 * 1000 + s * 1000 + n 24 | 25 | 26 | if __name__ == '__main__': 27 | assert solve_method(["01:41:8.9", "1:1:09.211"]) == ["1:1:09.211", "01:41:8.9"] 28 | assert solve_method(["23:41:08.023", "1:1:09.211", "08:01:22.0"]) == ["1:1:09.211", "08:01:22.0", "23:41:08.023"] 29 | assert solve_method(["22:41:08.023", "22:41:08.23"]) == ["22:41:08.023", "22:41:08.23"] 30 | -------------------------------------------------------------------------------- /codes/others100/114_mingming's-random-number.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: catcooc 5 | @file: 114_mingming's-random-number.py 6 | @time: 2023-07-28 15:49:08 7 | @project: huawei-od-python 8 | @desc: 114 明明的随机数 9 | """ 10 | 11 | 12 | def solve_method(nums): 13 | nums = list(set(nums)) 14 | nums.sort() 15 | return nums 16 | 17 | 18 | if __name__ == '__main__': 19 | assert solve_method([2, 2, 1]) == [1, 2] 20 | assert solve_method([3, 6, 9, 8, 2, 1, 1, 9, 8]) == [1, 2, 3, 6, 8, 9] 21 | -------------------------------------------------------------------------------- /codes/others100/116_lowest-order.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: catcooc 5 | @file: 116_lowest-order.py 6 | @time: 2023-07-28 16:14:43 7 | @project: huawei-od-python 8 | @desc: 116 最低位排序 9 | """ 10 | 11 | 12 | def solve_method(nums): 13 | nums.sort(key=lambda x: abs(x) % 10) 14 | return nums 15 | 16 | 17 | if __name__ == '__main__': 18 | arr = [1, 2, 5, -21, 22, 11, 55, -101, 42, 8, 7, 32] 19 | assert solve_method(arr) == [1, -21, 11, -101, 2, 22, 42, 32, 5, 55, 7, 8] 20 | -------------------------------------------------------------------------------- /codes/others100/119_maximum-permutation.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: catcooc 5 | @file: 119_maximum-permutation.py 6 | @time: 2023-07-30 18:22:36 7 | @project: huawei-od-python 8 | @desc: 119 最大排列 9 | """ 10 | 11 | 12 | def solve_method(nums): 13 | length = len(nums) 14 | 15 | for i in range(length): 16 | for j in range(i + 1, length): 17 | if int(nums[i] + nums[j]) < int(nums[j] + nums[i]): 18 | nums[i], nums[j] = nums[j], nums[i] 19 | 20 | return int("".join(nums)) 21 | 22 | 23 | if __name__ == '__main__': 24 | assert solve_method(["10", "9"]) == 910 25 | -------------------------------------------------------------------------------- /codes/others100/119_the-N-permutation.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: catcooc 5 | @file: 119_the-N-permutation.py 6 | @time: 2023-07-31 15:01:53 7 | @project: huawei-od-python 8 | @desc: 119 第N个排列 9 | """ 10 | import itertools 11 | 12 | 13 | def solve_method(n, k): 14 | arr = [i + 1 for i in range(n)] 15 | perms = list(itertools.permutations(arr)) 16 | result = int("".join(str(x) for x in perms[k - 1])) 17 | return result 18 | 19 | 20 | if __name__ == '__main__': 21 | assert solve_method(3, 3) == 213 22 | assert solve_method(2, 2) == 21 23 | -------------------------------------------------------------------------------- /codes/others100/120_maximum-return-stock-return-problem.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: catcooc 5 | @file: 120_maximum-return-stock-return-problem.py 6 | @time: 2023-07-30 18:42:56 7 | @project: huawei-od-python 8 | @desc: 120 最大收益股票收益问题 9 | """ 10 | 11 | 12 | def convert_yuan(price: str): 13 | value = int(price[:-1]) 14 | if price.endswith("S"): 15 | value *= 7 16 | return value 17 | 18 | 19 | def solve_method(prices): 20 | prices = list(map(convert_yuan, prices)) 21 | 22 | length = len(prices) 23 | # dp[i][0] 表示第i天持有股票所得现金。 24 | # dp[i][1] 表示第i天不持有股票所得最多现金 25 | dp = [[0] * 2 for _ in range(length)] 26 | dp[0][0] = -prices[0] 27 | dp[0][1] = 0 28 | for i in range(1, length): 29 | # 第i天持有股票所得现金=第i-1天持有股票与第i天买入股票的最大值 30 | dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] - prices[i]) 31 | # 第i天不持有股票所得最多现金=第i-1天不持有股票和第i天卖出股票的最大值 32 | dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + prices[i]) 33 | return dp[-1][1] 34 | 35 | 36 | if __name__ == '__main__': 37 | assert solve_method(["2Y", "3S", "4S", "6Y", "8S"]) == 76 38 | -------------------------------------------------------------------------------- /codes/others100/121_maximum-connection-number-of-boys.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 121_maximum-connection-number-of-boys.py 6 | @time: 2023/7/17 18:52 7 | @project: huawei-od-python 8 | @desc: 121 最大相连男生数 9 | """ 10 | 11 | 12 | def dfs(pos, arr, row, column, lenght): 13 | if not (0 <= row < len(arr) and 0 <= column < len(arr[0])): 14 | return lenght 15 | 16 | if arr[row][column] == "M": 17 | lenght += 1 18 | 19 | return dfs(pos, arr, row + pos[0], column + pos[1], lenght) 20 | 21 | 22 | def solve_method(arr): 23 | # 表示水平、垂直、对角线或者反对角线方向 24 | positions = [[0, 1], [1, 0], [1, 1], [-1, -1]] 25 | 26 | max_len = 0 27 | for i in range(len(arr)): 28 | for j in range(len(arr[0])): 29 | for pos in positions: 30 | max_len = max(max_len, dfs(pos, arr, i, j, 0)) 31 | 32 | return max_len 33 | 34 | 35 | if __name__ == '__main__': 36 | arr = [ 37 | ["F", "M", "M", "F"], 38 | ["F", "M", "M", "F"], 39 | ["F", "F", "F", "M"] 40 | ] 41 | assert solve_method(arr) == 3 42 | -------------------------------------------------------------------------------- /codes/others100/124_minimum-number-of-steps.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 124_minimum-number-of-steps.py 6 | @time: 2023/7/18 14:11 7 | @project: huawei-od-python 8 | @desc: 124 最小步骤数 9 | """ 10 | import math 11 | 12 | 13 | def solve_method(nums): 14 | min_step = math.inf 15 | n = len(nums) 16 | for i in range(1, n // 2): 17 | step = 1 18 | index = i 19 | 20 | while True: 21 | # 以所在元素的数字走相应的步数 22 | index += nums[index] 23 | step += 1 24 | if index > n - 1: 25 | break 26 | elif index == n - 1: 27 | min_step = min(min_step, step) 28 | break 29 | 30 | return -1 if min_step == math.inf else min_step 31 | 32 | 33 | if __name__ == '__main__': 34 | assert solve_method([7, 5, 9, 4, 2, 6, 8, 3, 5, 4, 3, 9]) == 2 35 | assert solve_method([1, 2, 3, 7, 1, 5, 9, 3, 2, 1]) == -1 36 | -------------------------------------------------------------------------------- /codes/others100/125_minimum-number-of-exchanges.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 125_minimum-number-of-exchanges.py 6 | @time: 2023/7/18 15:09 7 | @project: huawei-od-python 8 | @desc: 125 最少交换次数 9 | """ 10 | 11 | 12 | def solve_method(arr, k): 13 | # 得到小于k的掩码数组 14 | less_k_arr = [1 if n < k else 0 for n in arr] 15 | n = len(less_k_arr) 16 | # 得到需要相邻的数字个数 17 | m = sum(less_k_arr) 18 | 19 | # 使用滑动窗口求和,得到窗口内已满足条件的个数 20 | dp = [sum(less_k_arr[i: i + m]) for i in range(n)] 21 | # 得到最少交换次数 22 | result = m - max(dp) 23 | return result 24 | 25 | 26 | if __name__ == '__main__': 27 | assert solve_method([1, 3, 1, 4, 0], 2) == 1 28 | assert solve_method([0, 0, 0, 1, 0], 2) == 0 29 | assert solve_method([2, 3, 2], 1) == 0 30 | -------------------------------------------------------------------------------- /codes/others100/126_minimum-number-of-stoped-cars.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 126_minimum-number-of-stoped-cars.py 6 | @time: 2023/7/18 15:45 7 | @project: huawei-od-python 8 | @desc: 126 最少停车数 9 | """ 10 | 11 | 12 | def solve_method(line): 13 | cars = line.replace(",", "").split("0") 14 | count = 0 15 | for car in cars: 16 | car_len = len(car) 17 | # 超过3个1,就计算1辆大卡车 18 | while car_len > 3: 19 | count += 1 20 | car_len -= 1 21 | # 1或2个1,就计算1辆货车或1个小车 22 | if car_len != 0: 23 | count += 1 24 | return count 25 | 26 | 27 | if __name__ == '__main__': 28 | assert solve_method("1,0,1") == 2 29 | assert solve_method("1,1,0,0,1,1,1,0,1") == 3 30 | -------------------------------------------------------------------------------- /codes/others100/128_the-nearest-hospital.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 128_the-nearest-hospital.py 6 | @time: 2023/7/18 18:48 7 | @project: huawei-od-python 8 | @desc: 128 最近的医院 9 | """ 10 | 11 | 12 | def solve_method(x, y, m, l, n): 13 | tA = l + (x * 1000 / m) 14 | tB = y * 1000 / n 15 | 16 | if tA < tB: 17 | return "Taxi" 18 | elif tA > tB: 19 | return "Walk" 20 | else: 21 | return "Same" 22 | 23 | 24 | if __name__ == '__main__': 25 | assert solve_method(50, 5, 500, 30, 90) == "Walk" 26 | -------------------------------------------------------------------------------- /codes/others100/129_the-nearest-point.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 129_the-nearest-point.py 6 | @time: 2023/7/18 19:10 7 | @project: huawei-od-python 8 | @desc: 129 最近的点 9 | """ 10 | 11 | 12 | def solve_method(setA, setB, R): 13 | result = [] 14 | for a in setA: 15 | for b in setB: 16 | if a <= b and b - a <= R: 17 | result.append([a, b]) 18 | setB.remove(b) 19 | break 20 | 21 | return result 22 | 23 | 24 | if __name__ == '__main__': 25 | setA = [1, 5, 5, 10] 26 | setB = [1, 3, 8, 8, 20] 27 | R = 5 28 | assert solve_method(setA, setB, R) == [[1, 1], [5, 8], [5, 8]] 29 | -------------------------------------------------------------------------------- /codes/others100/130_the-longest-common-suffix.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 130_the-longest-common-suffix.py 6 | @time: 2023/7/18 20:11 7 | @project: huawei-od-python 8 | @desc: 130 最长公共后缀 9 | """ 10 | 11 | 12 | def solve_method(arr): 13 | pre = arr[0] 14 | for i in range(1, len(arr)): 15 | cur = arr[i] 16 | # 记录重复的字符位置 17 | j = 1 18 | # 从后往前对比两个字符串 19 | while j <= min(len(pre), len(cur)) and pre[-j] == cur[-j]: 20 | j += 1 21 | if j == 1: 22 | pre = "@Zero" 23 | break 24 | 25 | # 取出公共后缀 26 | pre = pre[-j + 1:] 27 | 28 | return pre 29 | 30 | 31 | if __name__ == '__main__': 32 | assert solve_method(["abc", "bbc", "c"]) == "c" 33 | assert solve_method(["aa", "bb", "cc"]) == "@Zero" 34 | -------------------------------------------------------------------------------- /codes/others100/131_the-longest-legal-expression.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 131_the-longest-legal-expression.py 6 | @time: 2023/7/18 21:02 7 | @project: huawei-od-python 8 | @desc: 131 最长合法表达式 9 | """ 10 | import re 11 | 12 | 13 | def check_valid(expression): 14 | p = re.compile("^([0-9]+([+\\-*/][0-9]+)+)$") 15 | return p.match(expression) 16 | 17 | 18 | def solve_method(line): 19 | SAMPLE_OPS = "0123456789+-*/" 20 | 21 | nums = [] 22 | for i in range(len(line)): 23 | if line[i].isdigit(): 24 | start = i 25 | # 确定满足简单数学表达式的连续字符 26 | while i + 1 < len(line) and line[i + 1] in SAMPLE_OPS: 27 | i += 1 28 | 29 | expression = line[start: i + 1] 30 | # 验证字符串是否为合法数学表达式 31 | if check_valid(expression): 32 | nums.append(expression) 33 | 34 | # 按照表达式长度进行排序 35 | nums.sort(key=lambda x: len(x), reverse=True) 36 | if nums: 37 | # 计算表达式的值 38 | return eval(nums[0]) 39 | else: 40 | return 0 41 | 42 | 43 | if __name__ == '__main__': 44 | assert solve_method("1-2abcd") == -1 45 | -------------------------------------------------------------------------------- /codes/others100/132_the-longest-vowel-string.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 132_the-longest-vowel-string.py 6 | @time: 2023/7/18 21:34 7 | @project: huawei-od-python 8 | @desc: 132 最长的元音字符串 9 | """ 10 | import re 11 | 12 | 13 | def solve_method(string): 14 | p = re.compile("[aeiouAEIOU]+") 15 | result = p.findall(string) 16 | result.sort(key=lambda x: len(x), reverse=True) 17 | return len(result[0]) if len(result) != 0 else 0 18 | 19 | 20 | if __name__ == '__main__': 21 | assert solve_method("asdbuiodevauufgh") == 3 22 | assert solve_method("fgh") == 0 23 | -------------------------------------------------------------------------------- /codes/others100/133_the-longest-continuous-alternating-square-wave-signal.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 133_the-longest-continuous-alternating-square-wave-signal.py 6 | @time: 2023/7/18 22:02 7 | @project: huawei-od-python 8 | @desc: 133 最长连续交替方波信号 9 | """ 10 | import re 11 | 12 | 13 | def solve_method(line): 14 | result = [] 15 | # 设置正则表达式 16 | p = re.compile("0(10)+") 17 | pos = 0 18 | while pos < len(line): 19 | # 使用正则表达式查找 20 | m = p.search(line, pos) 21 | # 记录字符串位置 22 | pos = m.span()[1] 23 | # 保存子串 24 | result.append(m.group()) 25 | # 按照子串长度从大到小排序 26 | result.sort(key=lambda x: len(x), reverse=True) 27 | # 如果存在,返回最大长度,如果不存在,返回-1 28 | return result[0] if len(result) != 0 else -1 29 | 30 | 31 | if __name__ == '__main__': 32 | assert solve_method("0010101010110000101000010") == "010101010" 33 | -------------------------------------------------------------------------------- /codes/others100/134_the-longest-consecutive-substring.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 134_the-longest-consecutive-substring.py 6 | @time: 2023/7/18 22:50 7 | @project: huawei-od-python 8 | @desc: 134 最长连续子串 9 | """ 10 | import re 11 | 12 | 13 | def solve_method(line): 14 | # 使用双指针 15 | start = 0 16 | end = 1 17 | max_length = -1 18 | # 设置正则表达式 19 | p = re.compile("[a-zA-Z]") 20 | while start < len(line) and end < len(line): 21 | end += 1 22 | sub_str = line[start:end] 23 | # 判断子串中是否只有一个字母 24 | if len(p.findall(sub_str)) == 1: 25 | max_length = max(max_length, len(sub_str)) 26 | else: 27 | # 扩大范围 28 | start += 1 29 | 30 | return max_length 31 | 32 | 33 | if __name__ == '__main__': 34 | assert solve_method("abC124ACb") == 4 35 | assert solve_method("a5") == 2 36 | assert solve_method("aBB9") == 2 37 | assert solve_method("abcdef") == -1 38 | assert solve_method("a12b234g1234") == 8 39 | -------------------------------------------------------------------------------- /codes/others100/135_valid-substring.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 135_valid-substring.py 6 | @time: 2023/7/18 23:34 7 | @project: huawei-od-python 8 | @desc: 135 有效子字符串 9 | """ 10 | 11 | 12 | def solve_method(s, l): 13 | last_valid_index = -1 14 | for ch in s: 15 | # 判断有效位置是否还在L长度范围内 16 | while last_valid_index < len(l) - 1: 17 | last_valid_index += 1 18 | # 判断S中的字符是否等于L中的字符 19 | if ch == l[last_valid_index]: 20 | # 如果相等,则跳出while循环,继续遍历S字符串 21 | break 22 | else: 23 | # 当有效位置已经超过了L长度范围,但S字符串还有字符,直接跳出遍历 24 | last_valid_index = -1 25 | break 26 | return last_valid_index 27 | 28 | 29 | if __name__ == '__main__': 30 | assert solve_method("ace", "abcde") == 4 31 | assert solve_method("fgh", "abcde") == -1 32 | assert solve_method("acce", "abcdec") == -1 33 | -------------------------------------------------------------------------------- /codes/others100/139_witty-delivery-boy.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 139_witty-delivery-boy.py 6 | @time: 2023/7/19 14:38 7 | @project: huawei-od-python 8 | @desc: 139 机智的外卖员 9 | """ 10 | 11 | 12 | def solve_method(N, M): 13 | if N >= M: 14 | return 0 15 | 16 | dp = [0] * (M + 1) 17 | # 向下步行需要花费的时间 18 | for i in range(N + 1): 19 | dp[i] = N - i 20 | for i in range(N + 1, M + 1): 21 | if i % 2 == 0: 22 | # 从上一层步行到达这一层,耗时+1 23 | # 坐电梯到达这一层,耗时+1 24 | dp[i] = min(dp[i - 1] + 1, dp[i // 2] + 1) 25 | else: 26 | # 从上一层步行到达这一层,耗时+1 27 | # 乘坐电梯再向下走一层,耗时+2 28 | dp[i] = min(dp[i - 1] + 1, dp[(i + 1) // 2] + 2) 29 | 30 | return dp[M] 31 | 32 | 33 | if __name__ == '__main__': 34 | assert solve_method(5, 17) == 4 35 | -------------------------------------------------------------------------------- /codes/others100/141_tree-structured-queries.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: Kaiwen Zuo 5 | @file: 141_tree-structured-queries.py 6 | @time: 2023/9/2 4:42 7 | @project: huawei-od-python 8 | @desc: 001 树状结构查询 9 | """ 10 | from collections import defaultdict 11 | 12 | 13 | def solve_method(tree, target): 14 | # 父节点下的所有子节点字典,key为父节点,value为所有子节点 15 | nodes = defaultdict(list) 16 | for node in tree: 17 | nodes[node[1]].append(node[0]) 18 | 19 | # 检索目标节点的子节点 20 | children = nodes.get(target, []) 21 | result = [] 22 | 23 | # 使用广度优先搜索 24 | while children: 25 | node = children.pop(0) 26 | result.append(node) 27 | children.extend(nodes.get(node, [])) 28 | 29 | # 对结果列表按照字典序排序 30 | result.sort() 31 | return result 32 | 33 | 34 | if __name__ == "__main__": 35 | tree = [["b", "a"], ["c", "a"], ["d", "c"], ["e", "c"], ["f", "d"]] 36 | assert solve_method(tree, "c") == ["d", "e", "f"] 37 | -------------------------------------------------------------------------------- /codes/others100/142_dictionary-reference.py: -------------------------------------------------------------------------------- 1 | # encoding: utf-8 2 | """ 3 | @author: Yalin Feng 4 | @file: 142_dictionary-reference.py 5 | @time: 2023/8/8 2:00 6 | @project: huawei-od-python 7 | @desc: 142 查字典 8 | """ 9 | 10 | from typing import List 11 | 12 | 13 | def solve_method(prefix: str, length: int, words: List[str]): 14 | # 空列表results,存储与前缀相匹配的单词 15 | results = [] 16 | 17 | for word in words: 18 | # 判断字符串的前缀是否与给定的前缀相同,如果相同,将该字符串添加到results列表中 19 | if word.startswith(prefix): 20 | results.append(word) 21 | 22 | return results if results else -1 23 | 24 | 25 | if __name__ == '__main__': 26 | assert solve_method("b", 3, ["a", "b", "c"]) == ["b"] 27 | assert solve_method("abc", 4, ["a", "ab", "abc", "abcd"]) == ["abc", "abcd"] 28 | assert solve_method("a", 3, ["b", "c", "d"]) == -1 29 | -------------------------------------------------------------------------------- /codes/others100/150_consecutive-number.py: -------------------------------------------------------------------------------- 1 | # encoding: utf-8 2 | """ 3 | @author: Yalin Feng 4 | @file: 150_consecutive-number.py 5 | @time: 2023/8/11 16:00 6 | @project: huawei-od-python 7 | @desc: 150 求解连续数列 8 | """ 9 | 10 | 11 | def solve_method(S: int, N: int): 12 | # 用公式求解最小项 13 | a1 = (S - (N ** 2 - N) / 2) / N 14 | 15 | if abs(a1 - int(a1)) < 1e-6: 16 | a1 = int(a1) 17 | result = [num for num in range(a1, a1 + N)] 18 | else: 19 | return -1 20 | 21 | return result 22 | 23 | 24 | if __name__ == '__main__': 25 | assert solve_method(525, 6) == [85, 86, 87, 88, 89, 90] 26 | assert solve_method(7, 3) == -1 27 | -------------------------------------------------------------------------------- /codes/others100/151_caving.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 151_caving.py 6 | @time: 2023/8/29 0:17 7 | @project: huawei-od-python 8 | @desc: 151 洞穴探险 9 | """ 10 | import re 11 | 12 | 13 | def solve_method(records): 14 | coordinates = re.findall(r'\((\d+),(\d+)\)', records) 15 | valid_coordinates = [] 16 | for coordinate in coordinates: 17 | x, y = map(int, coordinate) 18 | if 0 < x < 1000 and 0 < y < 1000: 19 | valid_coordinates.append((x, y)) 20 | if len(valid_coordinates) == 0: 21 | return "(0,0)" 22 | 23 | valid_coordinates.sort(key=lambda x: x[0] ** 2 + x[1] ** 2, reverse=True) 24 | return f"({valid_coordinates[0][0]},{valid_coordinates[0][1]})" 25 | 26 | 27 | if __name__ == '__main__': 28 | records = "ferg(3,10)a13fdsf3(3,4)f2r3rfasf(5,10)" 29 | assert solve_method(records) == "(5,10)" 30 | 31 | records = "asfefaweawfawf(0,1)fe" 32 | assert solve_method(records) == "(0,0)" 33 | -------------------------------------------------------------------------------- /codes/others100/152_pipeline.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 152_pipeline.py 6 | @time: 2023/8/29 0:18 7 | @project: huawei-od-python 8 | @desc: 152 流水线 9 | """ 10 | 11 | 12 | def solve_method(m, n, job_times): 13 | # 将作业处理时间按升序排序 14 | job_times.sort() 15 | # 初始化流水线的处理时间为0 16 | pipelines = [0] * m 17 | 18 | for i in range(n): 19 | # 找到处理时间最短的流水线 20 | min_pipeline = min(pipelines) 21 | index = pipelines.index(min_pipeline) 22 | # 将当前作业分配给最短处理时间的流水线 23 | pipelines[index] += job_times[i] 24 | 25 | total_time = max(pipelines) 26 | 27 | return total_time 28 | 29 | 30 | if __name__ == '__main__': 31 | assert solve_method(3, 5, [8, 4, 3, 2, 10]) == 13 32 | -------------------------------------------------------------------------------- /codes/others100/153_sliding-window.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 153_sliding-window.py 6 | @time: 2023/8/29 0:18 7 | @project: huawei-od-python 8 | @desc: 153 滑动窗口 9 | """ 10 | import math 11 | 12 | 13 | def solve_method(nums, window_size): 14 | if window_size > len(nums): 15 | return sum(nums) 16 | 17 | max_sum = - math.inf 18 | for i in range(len(nums) - window_size + 1): 19 | max_sum = max(max_sum, sum(nums[i:i + window_size])) 20 | 21 | return max_sum 22 | 23 | 24 | if __name__ == '__main__': 25 | assert solve_method([12, 10, 20, 30, 15, 23], 3) == 68 26 | -------------------------------------------------------------------------------- /codes/others100/155_martian_compute.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 155_martian_compute.py 6 | @time: 2023/8/29 14:20 7 | @project: huawei-od-python 8 | @desc: 155 火星文计算 9 | """ 10 | import re 11 | 12 | 13 | def sharp(x, y): 14 | return 4 * x + 3 * y + 2 15 | 16 | 17 | def dollar(x, y): 18 | return 2 * x + y + 3 19 | 20 | 21 | def solve_method(line): 22 | # 将数字和操作符存储在列表中 23 | chars = re.findall(r"(\d+)|([#$])", line) 24 | chars = [match[0] or match[1] for match in chars] 25 | chars = [int(x) if x.isdigit() else x for x in chars] 26 | 27 | while len(chars) != 1: 28 | pos = chars.index("#") if "#" in chars else -1 29 | if pos != -1: 30 | # 优先计算#操作 31 | tmp = sharp(chars[pos - 1], chars[pos + 1]) 32 | chars[pos - 1:pos + 2] = [tmp] 33 | else: 34 | pos = chars.index("$") if "$" in chars else -1 35 | if pos != -1: 36 | # 再计算$操作 37 | tmp = dollar(chars[pos - 1], chars[pos + 1]) 38 | chars[pos - 1:pos + 2] = [tmp] 39 | 40 | return chars[0] 41 | 42 | 43 | if __name__ == '__main__': 44 | assert solve_method("7#6$5#12") == 157 45 | -------------------------------------------------------------------------------- /codes/others100/156_CBT-non-leaf-parts-post-order-traversal.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: Kaiwen Zuo 5 | @file: 156_CBT-non-leaf-parts-post-order-traversal.py 6 | @time: 2023/9/3 3:59 7 | @project: huawei-od-python 8 | @desc: 002 完全二叉树非叶子部分后序遍历 9 | """ 10 | 11 | 12 | def solve_method(nums): 13 | def post_order_non_leaf(index): 14 | # 如果索引超出列表范围,返回空列表 15 | if index >= len(nums): 16 | return [] 17 | 18 | # 计算左右子节点的索引 19 | left_child = 2 * index + 1 20 | right_child = 2 * index + 2 21 | 22 | # 后序遍历:先左子树,再右子树,最后根节点 23 | left_result = post_order_non_leaf(left_child) 24 | right_result = post_order_non_leaf(right_child) 25 | 26 | # 如果当前节点是非叶子节点,添加到结果中 27 | if left_child < len(nums) or right_child < len(nums): 28 | return left_result + right_result + [index] 29 | return [] 30 | 31 | non_leaf_index = post_order_non_leaf(0) 32 | return [nums[x] for x in non_leaf_index] 33 | 34 | 35 | if __name__ == "__main__": 36 | assert solve_method([1, 2, 3, 4, 5, 6, 7]) == [2, 3, 1] 37 | assert solve_method([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) == [4, 5, 2, 3, 1] 38 | -------------------------------------------------------------------------------- /codes/others100/158_guess-password.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 158_guess-password.py 6 | @time: 2023/8/29 0:20 7 | @project: huawei-od-python 8 | @desc: 158 猜密码 9 | """ 10 | from itertools import combinations 11 | 12 | 13 | def solve_method(nums, k): 14 | # 生成所有可能的组合 15 | combinations_list = [] 16 | for length in range(k, len(nums) + 1): 17 | combinations_list.extend(combinations(nums, length)) 18 | 19 | # 按照要求进行排序 20 | combinations_list.sort() 21 | 22 | return [list(x) for x in combinations_list ] 23 | 24 | 25 | if __name__ == '__main__': 26 | assert solve_method([2, 3, 4], 2) == [[2, 3], [2, 3, 4], [2, 4], [3, 4]] 27 | -------------------------------------------------------------------------------- /codes/others100/159_monkey-climbing-the-mountain.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 159-monkey-climbing-the-mountain.py 6 | @time: 2023/8/29 0:21 7 | @project: huawei-od-python 8 | @desc: 159 猴子爬山 9 | """ 10 | 11 | 12 | def solve_method(n): 13 | # dp[i]表示到达第i个台阶的跳跃方式数 14 | dp = [0] * (n + 1) 15 | 16 | # 达到第1个台阶的跳跃方式数为1 17 | dp[0] = 1 18 | 19 | # 计算每个台阶的跳跃方式数 20 | for i in range(1, n + 1): 21 | # 如果当前台阶可以跳1步,则加上从前一个台阶跳上来的方式数 22 | if i >= 1: 23 | dp[i] += dp[i - 1] 24 | # 如果当前台阶可以跳3步,则加上从前三个台阶跳上来的方式数 25 | if i >= 3: 26 | dp[i] += dp[i - 3] 27 | 28 | return dp[n] 29 | 30 | 31 | if __name__ == '__main__': 32 | assert solve_method(50) == 122106097 33 | assert solve_method(3) == 2 34 | assert solve_method(2) == 1 35 | -------------------------------------------------------------------------------- /codes/others100/160_card-player.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 160_card-player.py 6 | @time: 2023/8/29 0:21 7 | @project: huawei-od-python 8 | @desc: 160 玩牌高手 9 | """ 10 | 11 | 12 | def solve_method(scores): 13 | n = len(scores) 14 | # dp[i]表示第i轮获得的最大总分数 15 | dp = [0] * (n + 1) 16 | 17 | for i in range(1, n + 1): 18 | # 如果当前轮次小于等于3,则总分数置0 19 | if i <= 3: 20 | dp[i] = 0 21 | else: 22 | # 选择获取该轮牌面分数 23 | dp[i] = dp[i - 1] + scores[i - 1] 24 | # 跳过该轮,将当前总分数还原为三轮前的总分数 25 | dp[i] = max(dp[i], dp[i - 3]) 26 | 27 | return dp[n] 28 | 29 | 30 | if __name__ == '__main__': 31 | assert solve_method([1, -5, -6, 4, 3, 6, -2]) == 11 32 | -------------------------------------------------------------------------------- /codes/others100/161_longest-substring-in-circle.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: LiYuHong 5 | @file: 161_longest-substring-in-circle.py 6 | @time: 2023/8/8 21:27 7 | @project: huawei-od-python 8 | @desc: 161 环中最长子串 9 | """ 10 | 11 | 12 | def solve_method(s): 13 | n = len(s) 14 | # 统计o的个数,便于计算奇偶性 15 | cnt = s.count('o') 16 | # 偶数最长为整个s的长度,奇数则为去掉一个o的长度 17 | return n - 1 if cnt % 2 else n 18 | 19 | 20 | if __name__ == '__main__': 21 | assert solve_method("alolobo") == 6 22 | assert solve_method("looxdolx") == 7 23 | assert solve_method("bcbcbc") == 6 24 | assert solve_method("olo") == 3 25 | -------------------------------------------------------------------------------- /codes/others100/164_max-number-of-consecutive-occurrences-of-the-same-char.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: LiYuHong 5 | @file: 164_max-number-of-consecutive-occurrences-of-the-same-char.py 6 | @time: 2023/8/8 22:00 7 | @project: huawei-od-python 8 | @desc: 164 相同字符连续出现的最大次数 9 | """ 10 | import math 11 | 12 | 13 | def solve_method(s): 14 | # 双指针 15 | left = right = 0 16 | # 求最大值则初始化一个负无穷,当然也可以为0 17 | max_v = -math.inf 18 | 19 | # 双指针模板 20 | count = 0 21 | while right < len(s): 22 | # 遇到相同字符指针右移,次数+1 23 | if s[left] == s[right]: 24 | count += 1 25 | right += 1 26 | # 遇到不相同字符,统计left字符的count 27 | # 更新最大值,随后left移到新字符上,count归零 28 | else: 29 | max_v = max(max_v, right - left) 30 | left = right 31 | count = 0 32 | # 循环外结尾的字符还要更新一次,因为退出时没有进入while内的更新部分 33 | max_v = max(max_v, right - left) 34 | return max_v 35 | 36 | 37 | if __name__ == '__main__': 38 | assert solve_method("hello") == 2 39 | assert solve_method("word") == 1 40 | assert solve_method("aaabbc") == 3 41 | -------------------------------------------------------------------------------- /codes/others100/165_relative-open-syllable.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: LiYuHong 5 | @file: 165_relative-open-syllable.py 6 | @time: 2023/8/8 22:03 7 | @project: huawei-od-python 8 | @desc: 165 相对开音节 9 | """ 10 | import re 11 | 12 | 13 | def solve_method(line): 14 | # 空格分隔和反转 15 | s_list = list(map(lambda x: x[::-1] if x.isalpha() else x, line.split())) 16 | count = 0 17 | p = re.compile(r"[^aeiou][aeiou][^aeiour]e") 18 | for string in s_list: 19 | # 必须全部是字母,才符合要求 20 | if len(string) >= 4 and all([True if ch.isalpha() else False for ch in string]): 21 | for i in range(len(string) - 3): 22 | # 使用正则表达式判断是否满足条件 23 | result = p.search(string[i:i + 4]) 24 | if result: 25 | count += 1 26 | 27 | return count 28 | 29 | 30 | if __name__ == '__main__': 31 | assert solve_method("ekam a ekac") == 2 32 | assert solve_method("!ekam a ekekac") == 2 33 | -------------------------------------------------------------------------------- /codes/others100/167_matrix-sparse-scan.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: LiYuHong 5 | @file: 167_matrix-sparse-scan.py 6 | @time: 2023/8/8 22:23 7 | @project: huawei-od-python 8 | @desc: 167 矩阵稀疏扫描 9 | """ 10 | 11 | 12 | def solve_method1(row, col, matrix): 13 | count_row = 0 14 | required_zeros = col // 2 15 | for i in range(row): 16 | cnt = matrix[i].count(0) 17 | if cnt >= required_zeros: 18 | count_row += 1 19 | 20 | count_col = 0 21 | required_zeros = row // 2 22 | for i in range(col): 23 | tmp = [] 24 | for j in range(row): 25 | tmp.append(matrix[j][i]) 26 | cnt = tmp.count(0) 27 | if cnt >= required_zeros: 28 | count_col += 1 29 | return count_row, count_col 30 | 31 | 32 | if __name__ == '__main__': 33 | matrix = [[1, 0, 0], [0, 1, 0], [0, 0, 1]] 34 | assert solve_method1(3, 3, matrix) == (3, 3) 35 | matrix = [[-1, 0, 1], [0, 0, 0], [-1, 0, 0], [0, -1, 0], [0, 0, 0]] 36 | assert solve_method1(5, 3, matrix) == (5, 3) 37 | -------------------------------------------------------------------------------- /codes/others100/169_weighing-weights.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: LiYuHong 5 | @file: 169_weighing-weights.py 6 | @time: 2023/8/8 22:36 7 | @project: huawei-od-python 8 | @desc: 169 称砝码 9 | """ 10 | 11 | 12 | def solve_method(n, weights, nums): 13 | # 每个砝码的重量 14 | new_weights = [] 15 | for i in range(n): 16 | new_weights.extend([weights[i]] * nums[i]) 17 | # dp数组中的元素表示能否使用给定的砝码组成对应的重量 18 | # 默认重量为0也是一种,则初始化dp[0]=True。 19 | total_weight = sum(new_weights) 20 | dp = [False] * (total_weight + 1) 21 | dp[0] = True 22 | for w in new_weights: 23 | for j in range(total_weight, w - 1, -1): 24 | dp[j] = dp[j] or dp[j - w] 25 | return sum(dp) 26 | 27 | 28 | if __name__ == '__main__': 29 | assert solve_method(2, [1, 2], [2, 1]) == 5 30 | -------------------------------------------------------------------------------- /codes/others100/171_qualified-substring-length.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: LiYuHong 5 | @file: 171_qualified-substring-length.py 6 | @time: 2023/8/8 22:48 7 | @project: huawei-od-python 8 | @desc: 171 符合条件的子串长度 9 | """ 10 | 11 | 12 | def solve_method(s1, s2, v): 13 | diff_list = [] 14 | n = len(s1) 15 | for ch1, ch2 in zip(s1, s2): 16 | diff_list.append(abs(ord(ch1) - ord(ch2))) 17 | # 两个指针 18 | left = right = 0 19 | # 记录累加和 20 | accu = 0 21 | # 记录最大长度 22 | max_len = 0 23 | while right < n: 24 | accu += diff_list[right] 25 | # 累加和accu超过了v,左端点右移,accu减去相应的值 26 | while accu > v: 27 | accu -= diff_list[left] 28 | left += 1 29 | max_len = max(max_len, right - left + 1) 30 | right += 1 31 | return max_len 32 | 33 | 34 | if __name__ == '__main__': 35 | assert solve_method("xxcdefgx", "cdefghic", 5) == 2 36 | -------------------------------------------------------------------------------- /codes/others100/172_the-letter-of-the-k-th-min-code-value.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: LiYuHong 5 | @file: 172_the-letter-of-the-k-th-min-code-value.py 6 | @time: 2023/8/8 22:53 7 | @project: huawei-od-python 8 | @desc: 172 第k个最小码值的字母 9 | """ 10 | 11 | 12 | def solve_method(s, k): 13 | # 从小到大按照码值排序 14 | sorted_chars = sorted(s) 15 | 16 | # k超过整个s的长度,则取最后一个字符即码值最大的 17 | c = sorted_chars[-1] if k >= len(sorted_chars) else sorted_chars[k - 1] 18 | return s.index(c) 19 | 20 | 21 | if __name__ == '__main__': 22 | assert solve_method("AbCdeFG", 3) == 5 23 | assert solve_method("fAdDAkBbBq", 4) == 6 24 | -------------------------------------------------------------------------------- /codes/others100/176_basketball-match.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: LiYuHong 5 | @file: 176_basketball-match.py 6 | @time: 2023/8/8 23:28 7 | @project: huawei-od-python 8 | @desc: 176 篮球比赛 9 | """ 10 | 11 | 12 | def solve_method(players): 13 | # 从大到小排序 14 | players.sort(reverse=True) 15 | team1 = [] 16 | team2 = [] 17 | for player in players: 18 | # 所有队伍未满5人时,比较战斗力总和 19 | if len(team1) != 5 and len(team2) != 5: 20 | if sum(team1) <= sum(team2): 21 | team1.append(player) 22 | else: 23 | team2.append(player) 24 | # 某个队伍满了5人时 25 | else: 26 | # 第1队满了,则进入第2队 27 | if len(team1) == 5: 28 | team2.append(player) 29 | # 第2队满了,则进入第1队 30 | else: 31 | team1.append(player) 32 | return abs(sum(team1) - sum(team2)) 33 | 34 | 35 | if __name__ == '__main__': 36 | assert solve_method([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) == 1 37 | assert solve_method([1, 2, 3, 4, 5, 6, 7, 8, 999, 10]) == 973 38 | assert solve_method([1, 2, 3, 4, 5, 6, 7, 8, 999, 1000]) == 1 39 | -------------------------------------------------------------------------------- /codes/others100/177_screen-saver.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: LiYuHong 5 | @file: 177_screen-saver.py 6 | @time: 2023/8/8 23:33 7 | @project: huawei-od-python 8 | @desc: 177 经典屏保 9 | """ 10 | 11 | 12 | def solve_method(x, y, t): 13 | screen_width, screen_length = 800, 600 14 | logo_width, logo_length = 50, 25 15 | 16 | x_dir = 1 17 | y_dir = 1 18 | for i in range(t): 19 | # 继续移动 20 | x += x_dir 21 | y += y_dir 22 | 23 | # 当达到边缘时,转向 24 | if x == 0 or x == screen_width - logo_width: 25 | x_dir *= -1 26 | if y == 0 or y == screen_length - logo_length: 27 | y_dir *= -1 28 | 29 | return x, y 30 | 31 | 32 | if __name__ == '__main__': 33 | assert solve_method(0, 0, 10) == (10, 10) 34 | assert solve_method(500, 570, 10) == (510, 570) 35 | -------------------------------------------------------------------------------- /codes/others100/178_draw-machine.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: LiYuHong 5 | @file: 178_draw-machine.py 6 | @time: 2023/8/8 23:39 7 | @project: huawei-od-python 8 | @desc: 178 绘图机器 9 | """ 10 | 11 | 12 | def solve_method(n, e, steps): 13 | # 转变成字典表,便于查找 14 | dict_steps = dict(steps) 15 | # 每一步的位移,初始为0 16 | offset = 0 17 | # 总面积 18 | area = 0 19 | 20 | # 遍历起点到终点的长度e,如果该点在steps中能找到且做了位移,则进行改变 21 | for i in range(e): 22 | if i in dict_steps: 23 | # 更新位移 24 | offset += dict_steps[i] 25 | # 面积即位移大小,向终点走的每一步都要累加计算 26 | area += abs(offset) 27 | 28 | return area 29 | 30 | 31 | if __name__ == '__main__': 32 | assert solve_method(4, 10, [(1, 1), (2, 1), (3, 1), (4, -2)]) == 12 33 | assert solve_method(2, 4, [(0, 1), (2, -2)]) == 4 34 | -------------------------------------------------------------------------------- /codes/others100/179_meet-rules-combination.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: LiYuHong 5 | @file: 179_meet-rules-combination.py 6 | @time: 2023/8/8 23:44 7 | @project: huawei-od-python 8 | @desc: 179 满足规则的组合 9 | """ 10 | 11 | from collections import Counter 12 | 13 | 14 | def solve_method(n, nums): 15 | num_freq = Counter(nums) 16 | 17 | for i in range(n - 2): 18 | for j in range(i + 1, n - 1): 19 | a = nums[i] 20 | b = nums[j] 21 | num_freq[a] -= 1 22 | num_freq[b] -= 1 23 | k = a - b 24 | if k % 2 == 0 and k // 2 in num_freq and num_freq[k // 2] > 0: 25 | return [a, b, (k // 2)] 26 | num_freq[a] += 1 27 | num_freq[b] += 1 28 | return 0 29 | 30 | 31 | if __name__ == '__main__': 32 | assert solve_method(4, [2, 7, 3, 0]) == [7, 3, 2] 33 | assert solve_method(3, [1, 1, 1]) == 0 34 | -------------------------------------------------------------------------------- /codes/others100/180_statistical-monitoring.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: LiYuHong 5 | @file: 180_statistical-monitoring.py 6 | @time: 2023/8/8 23:48 7 | @project: huawei-od-python 8 | @desc: 180 统计监控 9 | """ 10 | 11 | 12 | def solve_method(m, n, nums): 13 | count = 0 14 | # 查找出所有已停车位位置,并在该位上打开监控 15 | cars = [] 16 | for i in range(m): 17 | for j in range(n): 18 | if nums[i][j] == 1: 19 | count += 1 20 | cars.append((i, j)) 21 | 22 | # 对每个汽车周围进行一次广度优先遍历,打开监控 23 | while cars: 24 | i, j = cars.pop() 25 | for x, y in (i + 1, j), (i - 1, j), (i, j + 1), (i, j - 1): 26 | if 0 <= x < m and 0 <= y < n and nums[x][y] == 0: 27 | count += 1 28 | nums[x][y] = 1 29 | return count 30 | 31 | 32 | if __name__ == '__main__': 33 | sites = [[0, 0, 0], 34 | [0, 1, 0], 35 | [0, 0, 0]] 36 | assert solve_method(3, 3, sites) == 5 37 | 38 | sites = [[1, 0, 0], 39 | [0, 1, 0], 40 | [0, 0, 0]] 41 | assert solve_method(3, 3, sites) == 6 42 | -------------------------------------------------------------------------------- /codes/others100/181_surviving-on-a-desert-island.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: Kaiwen Zuo 5 | @file: 181_surviving-on-a-desert-island.py 6 | @time: 2023/9/4 11:11 7 | @project: huawei-od-python 8 | @desc: 004 荒岛求生 9 | """ 10 | 11 | 12 | def solve_method(nums): 13 | # 将正数放入右侧列表,负数的绝对值放入左侧列表 14 | left = [abs(x) for x in nums if x <= 0] 15 | right = [x for x in nums if x > 0] 16 | right = right[::-1] 17 | 18 | # 迭代处理左侧和右侧列表 19 | while right and left: 20 | if left[0] > right[0]: 21 | left[0] -= right.pop(0) 22 | elif left[0] < right[0]: 23 | right[0] -= left.pop(0) 24 | else: 25 | left.pop(0) 26 | right.pop(0) 27 | 28 | # 输出最终列表的长度之和 29 | return len(right) + len(left) 30 | 31 | 32 | if __name__ == "__main__": 33 | assert solve_method([5, 10, 8, -8, -5]) == 2 34 | -------------------------------------------------------------------------------- /codes/others100/182_archaeologist.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 182_archaeologist.py 6 | @time: 2023/8/27 1:26 7 | @project: huawei-od-python 8 | @desc: 182 考古学家 9 | """ 10 | 11 | import itertools 12 | 13 | 14 | def solve_method(fragments): 15 | fragments = list(itertools.permutations(fragments)) 16 | fragments = list(set(["".join(i) for i in fragments])) 17 | fragments.sort() 18 | return fragments 19 | 20 | 21 | if __name__ == '__main__': 22 | assert solve_method(["a", "b", "c"]) == ["abc", "acb", "bac", "bca", "cab", "cba"] 23 | assert solve_method(["a", "b", "a"]) == ["aab", "aba", "baa"] 24 | assert solve_method(["a", "b", "ab"]) == ["aabb", "abab", "abba", "baab", "baba"] 25 | -------------------------------------------------------------------------------- /codes/others100/184_team-up-by-ability.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 184_team-up-by-ability.py 6 | @time: 2023/8/27 1:28 7 | @project: huawei-od-python 8 | @desc: 184 能力组队 9 | """ 10 | 11 | 12 | def solve_method(nums, min_power): 13 | # 过滤出小于最低能力值的人 14 | arr = sorted(filter(lambda x: x < min_power, nums)) 15 | # 大于等于最低能力值的人,都可以自成一队 16 | count = len(nums) - len(arr) 17 | # 双指针遍历 18 | i, j = 0, len(arr) - 1 19 | while i < j: 20 | if arr[i] + arr[j] >= min_power: 21 | # 如果最小值和最大值加起来小于最低能力值,可以组成一队 22 | count += 1 23 | i += 1 24 | j -= 1 25 | else: 26 | # 否则左指针向右移 27 | i += 1 28 | return count 29 | 30 | 31 | if __name__ == '__main__': 32 | assert solve_method([3, 1, 5, 7, 9], 8) == 3 33 | assert solve_method([3, 1, 5, 7, 9, 2, 6], 8) == 4 34 | assert solve_method([3, 1, 5, 6, 9], 8) == 2 35 | -------------------------------------------------------------------------------- /codes/others100/185_spacecraft.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 185_spacecraft.py 6 | @time: 2023/8/27 1:29 7 | @project: huawei-od-python 8 | @desc: 185 航天器 9 | """ 10 | 11 | 12 | def solve_method(nums): 13 | result = 0 14 | for i in range(len(nums)): 15 | for j in range(i + 1, len(nums)): 16 | area = min(nums[i], nums[j]) * (j - i) 17 | result = max(result, area) 18 | 19 | return result 20 | 21 | 22 | if __name__ == '__main__': 23 | assert solve_method([10, 9, 8, 7, 6, 5, 4, 3, 2, 1]) == 25 24 | -------------------------------------------------------------------------------- /codes/others100/186_english-input.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 186_english-input.py 6 | @time: 2023/8/27 1:29 7 | @project: huawei-od-python 8 | @desc: 186 英文输入法 9 | """ 10 | import re 11 | 12 | 13 | def solve_method(line, pre): 14 | words = re.findall(r'\w+', line) 15 | word_set = set(words) 16 | 17 | result = [] 18 | 19 | for word in word_set: 20 | if word.startswith(pre): 21 | result.append(word) 22 | if not result: 23 | result.append(pre) 24 | 25 | result.sort() 26 | return " ".join(result) 27 | 28 | 29 | if __name__ == '__main__': 30 | assert solve_method("I love you", "He") == "He" 31 | 32 | line = """The furthest distance in the world, 33 | Is not between life and death, 34 | But when I stand in front or you, 35 | Yet you don't know that I love you. """ 36 | assert solve_method(line, "f") == "front furthest" 37 | -------------------------------------------------------------------------------- /codes/others100/188_replanting-of-unlived-poplar-trees.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 188_replanting-of-unlived-poplar-trees.py 6 | @time: 2023/8/27 1:30 7 | @project: huawei-od-python 8 | @desc: 188 补活未成活胡杨树 9 | """ 10 | 11 | 12 | def solve_method(N, M, trees, K): 13 | rolls = [0] * N 14 | 15 | for tree in trees: 16 | rolls[tree - 1] = 1 17 | 18 | left = 0 19 | right = 0 20 | count = 0 21 | result = 0 22 | 23 | while right < N: 24 | while right < N and count <= K: 25 | if rolls[right] == 1: 26 | # 如果找到需要补种的胡杨树,则count累加1 27 | count += 1 28 | right += 1 29 | 30 | if count <= K: 31 | # 如果已经找到K个,计算补种之后的连续胡杨树的个数 32 | result = max(right - left, result) 33 | 34 | # 找到在[left, right]中第一个为1的位置,然后加1,更新左指针 35 | left = rolls.index(1, left, right) + 1 36 | # 减去多加的个数 37 | count -= 1 38 | return result 39 | 40 | 41 | if __name__ == '__main__': 42 | assert solve_method(10, 3, [2, 4, 7], 1) == 6 43 | assert solve_method(10, 3, [2, 4, 7], 2) == 8 44 | assert solve_method(10, 3, [2, 4, 7], 3) == 10 45 | -------------------------------------------------------------------------------- /codes/others100/189_watch-the-performance.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 189_watch-the-performance.py 6 | @time: 2023/8/27 1:30 7 | @project: huawei-od-python 8 | @desc: 189 观看文艺汇演问题、最多能看几场演出 9 | """ 10 | 11 | 12 | def solve_method(show_times): 13 | times = [] 14 | for start_time, duration in show_times: 15 | times.append([start_time, start_time + duration]) 16 | 17 | # 按照结束时间进行排序 18 | times.sort(key=lambda x: x[1]) 19 | result = 1 20 | 21 | prev_start_time = times[0][1] 22 | for i in range(1, len(times)): 23 | start_time, end_time = times[i] 24 | if start_time - prev_start_time >= 15: 25 | # 如果15分钟能赶得上,则可以看这场演出 26 | result += 1 27 | prev_start_time = end_time 28 | 29 | return result 30 | 31 | 32 | if __name__ == '__main__': 33 | show_times = [[720, 120], 34 | [840, 120]] 35 | assert solve_method(show_times) == 1 36 | 37 | show_times = [[0, 60], 38 | [90, 60]] 39 | assert solve_method(show_times) == 2 40 | -------------------------------------------------------------------------------- /codes/others100/191_calculate-the-nearest-number.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 191_calculate-the-nearest-number.py 6 | @time: 2023/8/27 1:31 7 | @project: huawei-od-python 8 | @desc: 191 计算最接近的数 9 | """ 10 | 11 | 12 | def find_median(nums): 13 | sorted_nums = sorted(nums) 14 | N = len(sorted_nums) 15 | return sorted_nums[N // 2] 16 | 17 | 18 | def solve_method(nums, K): 19 | # 得到数组的中位数 20 | mid = find_median(nums) 21 | min_distance = float('inf') 22 | index = -1 23 | for i in range(len(nums) - K + 1): 24 | # 计算表达式 25 | count = nums[i] 26 | for j in range(i + 1, i + K): 27 | count -= nums[j] 28 | 29 | # 计算与中位数的之差的绝对值 30 | distance = abs(count - mid) 31 | 32 | # 得到最近的距离和下标 33 | if distance < min_distance: 34 | min_distance = distance 35 | index = i 36 | return index 37 | 38 | 39 | if __name__ == '__main__': 40 | assert solve_method([1, 2, 3, 3], 2) == 2 41 | assert solve_method([50, 50, 2, 3], 2) == 1 42 | -------------------------------------------------------------------------------- /codes/others100/194_street-lighting.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 194_street-lighting.py 6 | @time: 2023/8/27 1:31 7 | @project: huawei-od-python 8 | @desc: 194 路灯照明 9 | """ 10 | 11 | 12 | def solve_method(nums): 13 | prev = nums[0] 14 | result = 0 15 | for i in range(1, len(nums)): 16 | if prev + nums[i] < 100: 17 | result += 100 - (prev + nums[i]) 18 | prev = nums[i] 19 | 20 | return result 21 | 22 | 23 | if __name__ == '__main__': 24 | assert solve_method([50, 50]) == 0 25 | assert solve_method([50, 40]) == 10 26 | assert solve_method([50, 40, 30, 20, 10]) == 160 27 | -------------------------------------------------------------------------------- /codes/others100/197_jump-the-grid-2.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 197_jump-the-grid-2.py 6 | @time: 2023/8/27 1:32 7 | @project: huawei-od-python 8 | @desc: 197 跳格子(2) 9 | """ 10 | 11 | 12 | def dpfunc(nums): 13 | # dp[i]表示第i个位置的最大和 14 | dp = [0] * len(nums) 15 | dp[0] = nums[0] 16 | 17 | for i in range(1, len(nums)): 18 | if i == 1: 19 | # 第二个值为前一个与当前值的最大值 20 | dp[i] = max(nums[i], dp[i - 1]) 21 | else: 22 | # 选择前一个值与跳一个之后加上该值的最大值 23 | dp[i] = max(dp[i - 1], dp[i - 2] + nums[i]) 24 | 25 | return dp[len(nums) - 1] 26 | 27 | 28 | def solve_method(nums): 29 | # 去掉最后一个格子或者去掉第一个格子,防止连续 30 | res = max(dpfunc(nums[:-1]), dpfunc(nums[1:])) 31 | return res 32 | 33 | 34 | if __name__ == '__main__': 35 | assert solve_method([2, 3, 2]) == 3 36 | assert solve_method([1, 2, 3, 1]) == 4 37 | -------------------------------------------------------------------------------- /codes/others100/198_kick-stones.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 198_kick-stones.py 6 | @time: 2023/8/27 1:32 7 | @project: huawei-od-python 8 | @desc: 198 踢石子问题 9 | """ 10 | 11 | 12 | def solve_method(K): 13 | if K <= 1 or K >= 100: 14 | return "ERROR!" 15 | 16 | nums = [i for i in range(1, 101)] 17 | # 用于K的计数 18 | count = 0 19 | # 当前遍历到的位置 20 | index = 0 21 | while len(nums) >= K: 22 | # 开始计数第count个 23 | count += 1 24 | if count == K: 25 | # 如果到达了第K个,将此石子踢出 26 | nums.pop(index) 27 | # 从0开始重新计数到K 28 | count = 0 29 | else: 30 | # 继续下一个石子 31 | index += 1 32 | 33 | if index == len(nums): 34 | # 如果已经循环了一圈,则从0位置开始继续循环报数 35 | index = 0 36 | 37 | return nums 38 | 39 | 40 | if __name__ == '__main__': 41 | assert solve_method(3) == [58, 91] 42 | -------------------------------------------------------------------------------- /codes/others100/199_height-sorting.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 199_height-sorting.py 6 | @time: 2023/8/27 1:33 7 | @project: huawei-od-python 8 | @desc: 199 身高排序 9 | """ 10 | 11 | 12 | def solve_method(H, N, heights): 13 | heights.sort(key=lambda x: (abs(x - H), x)) 14 | return heights 15 | 16 | 17 | if __name__ == '__main__': 18 | heights = [95, 96, 97, 98, 99, 101, 102, 103, 104, 105] 19 | assert solve_method(100, 10, heights) == [99, 101, 98, 102, 97, 103, 96, 104, 95, 105] 20 | -------------------------------------------------------------------------------- /codes/others100/200_height-and-weight-ordering.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 200_height-and-weight-ordering.py 6 | @time: 2023/8/27 1:33 7 | @project: huawei-od-python 8 | @desc: 200 身高体重排序 9 | """ 10 | 11 | 12 | def solve_method(n, heights, weights): 13 | heights = [(i + 1, h) for i, h in enumerate(heights)] 14 | students = [(h[0], h[1], w) for h, w in zip(heights, weights)] 15 | 16 | # 将身高从低到高、体重从小到大排序 17 | students.sort(key=lambda x: (x[1], x[2])) 18 | 19 | return "".join([str(x[0]) for x in students]) 20 | 21 | 22 | if __name__ == '__main__': 23 | assert solve_method(4, [100, 100, 120, 130], [40, 30, 60, 50]) == "2134" 24 | assert solve_method(3, [90, 110, 90], [45, 60, 45]) == "132" 25 | -------------------------------------------------------------------------------- /codes/others100/201_interscholasticmeet.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 201_interscholasticmeet.py 6 | @time: 2023/7/20 15:21 7 | @project: huawei-od-python 8 | @desc: 201 运动会 9 | """ 10 | 11 | 12 | def solve_method(n, heights, weights): 13 | result = [] 14 | for i in range(n): 15 | # 使用元组存储数据 16 | result.append((i + 1, heights[i], weights[i])) 17 | result.sort(key=lambda x: (x[1], x[2])) 18 | 19 | return [x[0] for x in result] 20 | 21 | 22 | if __name__ == "__main__": 23 | heights = [100, 100, 120, 130] 24 | weights = [40, 30, 60, 50] 25 | assert solve_method(4, heights, weights) == [2, 1, 3, 4] 26 | 27 | heights = [90, 110, 90] 28 | weights = [45, 60, 45] 29 | assert solve_method(3, heights, weights) == [1, 3, 2] 30 | -------------------------------------------------------------------------------- /codes/others100/202_continuous-substrings.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 202_continuous-substrings.py 6 | @time: 2023/7/20 13:47 7 | @project: huawei-od-python 8 | @desc: 202 连续子串 9 | """ 10 | 11 | 12 | def solve_method(t, p): 13 | # 实现 t.find(p) + 1 14 | p_len = len(p) 15 | for i in range(len(t) - p_len + 1): 16 | substring = t[i:i + p_len] 17 | if substring == p: 18 | return i + 1 19 | 20 | return "No" 21 | 22 | 23 | if __name__ == '__main__': 24 | assert solve_method("AVERDXIVYERDIAN", "RDXI") == 4 25 | -------------------------------------------------------------------------------- /codes/others100/203_consecutive-letter-length.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 203_consecutive-letter-length.py 6 | @time: 2023/7/20 14:05 7 | @project: huawei-od-python 8 | @desc: 203 连续字母长度、求第K长的字符串长度 9 | """ 10 | 11 | from collections import defaultdict 12 | 13 | 14 | def solve_method(strings, k): 15 | pre_char = strings[0] 16 | # 字符字典,key为字母,value为同一字母连续出现的最大次数 17 | ch_freq = defaultdict(int) 18 | 19 | count = 0 20 | for ch in strings: 21 | if pre_char == ch: 22 | count += 1 23 | else: 24 | if pre_char in ch_freq: 25 | ch_freq[pre_char] = max(ch_freq[pre_char], count) 26 | else: 27 | ch_freq[pre_char] = count 28 | pre_char = ch 29 | count = 1 30 | 31 | # 按照出现次数排序 32 | ch_list = sorted(ch_freq.values(), reverse=True) 33 | if k > len(ch_list): 34 | return -1 35 | else: 36 | # 取出第k个字母的连续出现的次数 37 | return ch_list[k - 1] 38 | 39 | 40 | if __name__ == '__main__': 41 | assert solve_method("AAAAHHHBBCDHHHH", 3) == 2 42 | assert solve_method("AABAAA", 2) == 1 43 | assert solve_method("ABC", 4) == -1 44 | -------------------------------------------------------------------------------- /codes/others100/208_Alibaba-looks-for-gold-treasure-chests-2.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 208_Alibaba-looks-for-gold-treasure-chests-2.py 6 | @time: 2023/7/22 0:16 7 | @project: huawei-od-python 8 | @desc: 208 阿里巴巴找黄金宝箱(2) 9 | """ 10 | from collections import defaultdict 11 | 12 | 13 | def solve_method(nums): 14 | half = len(nums) // 2 15 | num_freq = defaultdict(int) 16 | 17 | for num in nums: 18 | num_freq[num] += 1 19 | 20 | num_freq = sorted(num_freq.items(), key=lambda x: -x[1]) 21 | count = 0 22 | result = 0 23 | while count < half: 24 | count += num_freq[result][1] 25 | result += 1 26 | 27 | return result 28 | 29 | 30 | if __name__ == '__main__': 31 | assert solve_method([6, 6, 6, 6, 3, 3, 3, 1, 1, 5]) == 2 32 | assert solve_method([1, 1, 1, 1, 3, 3, 3, 6, 6, 8]) == 2 33 | assert solve_method([2, 2, 2, 2]) == 1 34 | -------------------------------------------------------------------------------- /codes/others100/209_Alibaba-looks-for-gold-treasure-chests-3.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 209_Alibaba-looks-for-gold-treasure-chests-3.py 6 | @time: 2023/7/22 0:41 7 | @project: huawei-od-python 8 | @desc: 209 阿里巴巴找黄金宝箱(3) 9 | """ 10 | 11 | 12 | def solve_method(nums, n): 13 | num_last_index = {} 14 | 15 | for i in range(len(nums)): 16 | current_num = nums[i] 17 | if current_num in num_last_index and i - num_last_index[current_num] <= n: 18 | return num_last_index[current_num] 19 | 20 | num_last_index[current_num] = i 21 | 22 | return -1 23 | 24 | 25 | if __name__ == '__main__': 26 | assert solve_method([1, 2, 3, 1], 3) == 0 27 | assert solve_method([6, 3, 1, 6], 3) == 0 28 | assert solve_method([5, 6, 7, 5, 6, 7], 2) == -1 29 | -------------------------------------------------------------------------------- /codes/others100/211_Alibaba-looks-for-gold-treasure-chests-5.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 211_Alibaba-looks-for-gold-treasure-chests-5.py 6 | @time: 2023/7/22 21:44 7 | @project: huawei-od-python 8 | @desc: 211 阿里巴巴找黄金宝箱(5) 9 | """ 10 | 11 | 12 | def solve_method(nums, k): 13 | max_value = 0 14 | # 使用滑动窗口 15 | for i in range(len(nums) - k + 1): 16 | # 计算窗口中的数字和 17 | max_value = max(sum(nums[i: k + i]), max_value) 18 | 19 | return max_value 20 | 21 | 22 | if __name__ == '__main__': 23 | assert solve_method([2, 10, -3, -8, 40, 5], 4) == 39 24 | assert solve_method([8], 1) == 8 25 | -------------------------------------------------------------------------------- /codes/others100/212_collect-five-blessing-cards.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 212_collect-five-blessing-cards.py 6 | @time: 2023/7/22 21:58 7 | @project: huawei-od-python 8 | @desc: 212 集五福 9 | """ 10 | 11 | 12 | def solve_method(arr): 13 | result = [0] * 5 14 | 15 | for cards in arr: 16 | for i in range(5): 17 | if cards[i] == "1": 18 | result[i] += 1 19 | 20 | return min(result) 21 | 22 | 23 | if __name__ == '__main__': 24 | assert solve_method(["11001", "11101"]) == 0 25 | assert solve_method(["11101", "10111"]) == 1 26 | -------------------------------------------------------------------------------- /codes/others100/214_non-strictly-increment-sequence.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 214_non-strictly-increment-sequence.py 6 | @time: 2023/7/22 22:45 7 | @project: huawei-od-python 8 | @desc: 214 非严格递增连续数字序列 9 | """ 10 | import math 11 | 12 | 13 | def solve_method(string): 14 | # 设置前一个数字 15 | pre_num = math.inf 16 | # 满足条件的最大长度 17 | max_len = 0 18 | cur_len = 0 19 | for ch in string: 20 | if ch.isdigit(): 21 | ch = int(ch) 22 | # 如果当前数字大于等于前一个数字,则继续保持非严格递增 23 | if cur_len == 0 or ch >= pre_num: 24 | cur_len += 1 25 | else: 26 | # 如果没有,则长度回到1 27 | max_len = max(max_len, cur_len) 28 | cur_len = 1 29 | # 保存前一个数字 30 | pre_num = ch 31 | else: 32 | # 如果不是数字,则直接保存当前长度 33 | max_len = max(max_len, cur_len) 34 | cur_len = 0 35 | pre_num = math.inf 36 | 37 | return max(cur_len, max_len) 38 | 39 | 40 | if __name__ == '__main__': 41 | assert solve_method("abc2234019A334bc") == 4 42 | -------------------------------------------------------------------------------- /codes/others100/215_canteen-feeding-speed.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 215_canteen-feeding-speed.py 6 | @time: 2023/7/22 23:19 7 | @project: huawei-od-python 8 | @desc: 215 食堂供餐 9 | """ 10 | 11 | 12 | def solve_method(total, M, N, P): 13 | min_speed, max_speed = 0, total - M 14 | result = max_speed 15 | # 二分查找,速度范围为[min_speed, max_speed] 16 | while min_speed <= max_speed: 17 | mid = min_speed + (max_speed - min_speed) // 2 18 | # 检查该速度是否能满足 19 | if check_speed(mid, M, N, P): 20 | # 如果能满足 21 | result = mid 22 | # 继续查找左侧 23 | max_speed = mid - 1 24 | else: 25 | # 如果不能,则继续查找右侧 26 | min_speed = mid + 1 27 | 28 | return result 29 | 30 | 31 | def check_speed(speed, foods, N, P): 32 | for i in range(N): 33 | foods -= P[i] 34 | if foods < 0: 35 | return False 36 | foods += speed 37 | return True 38 | 39 | 40 | if __name__ == '__main__': 41 | P = [10, 4, 5] 42 | assert solve_method(sum(P), 14, 3, P) == 3 43 | -------------------------------------------------------------------------------- /codes/others100/217_Alibaba-looks-for-gold-treasure-chests-1.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 217_Alibaba-looks-for-gold-treasure-chests-1.py 6 | @time: 2023/7/23 0:38 7 | @project: huawei-od-python 8 | @desc: 217 阿里巴巴找黄金宝箱(1) 9 | """ 10 | 11 | 12 | def solve_method(nums): 13 | left_sum = 0 14 | right_sum = sum(nums) 15 | for i in range(len(nums)): 16 | # 每次遍历将右侧数字和减去当前数字 17 | right_sum -= nums[i] 18 | if left_sum == right_sum: 19 | # 如果满足相等条件,则返回坐标 20 | return i 21 | # 每次遍历将左侧数字和加上当前数字 22 | left_sum += nums[i] 23 | 24 | # 如果不存在,返回-1 25 | return -1 26 | 27 | 28 | if __name__ == '__main__': 29 | assert solve_method([2, 5, -1, 8, 6]) == 3 30 | assert solve_method([8, 9]) == -1 31 | assert solve_method([11]) == 0 32 | -------------------------------------------------------------------------------- /codes/others100/218_colorize-the-blackboard.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 218_colorize-the-blackboard.py 6 | @time: 2023/7/23 0:50 7 | @project: huawei-od-python 8 | @desc: 218 黑板上色 9 | """ 10 | 11 | 12 | def solve_method(nums): 13 | # 去重 14 | nums = set(nums) 15 | # 如果有数字1,则只需要一种颜色 16 | if 1 in nums: 17 | return 1 18 | 19 | # 重新转换成列表,并排序 20 | nums = list(nums) 21 | nums.sort() 22 | # 遍历数组 23 | i = 0 24 | while i < len(nums): 25 | cur = nums[i] 26 | j = i + 1 27 | # 删除数组中能被整除的数 28 | while j < len(nums): 29 | if nums[j] % cur == 0: 30 | nums.pop(j) 31 | else: 32 | j += 1 33 | i += 1 34 | 35 | # 返回剩余数组的长度,即为需要的最少颜色种数 36 | return len(nums) 37 | 38 | 39 | if __name__ == '__main__': 40 | assert solve_method([2, 4, 6]) == 1 41 | assert solve_method([2, 3, 4, 9]) == 2 42 | -------------------------------------------------------------------------------- /codes/others100/219_numeric-sequence-comparison.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: Kaiwen Zuo 5 | @file: 219_numeric-sequence-comparison.py 6 | @time: 2023/9/4 11:12 7 | @project: huawei-od-python 8 | @desc: 005 数字序列比大小 9 | """ 10 | 11 | 12 | def solve_method(A, B): 13 | # 对列表A和B按照从小到大排序 14 | A.sort() 15 | B.sort() 16 | 17 | result = 0 18 | # 遍历列表 A 中的元素 19 | for i in range(len(A)): 20 | numA = A[i] 21 | # 判断当前元素 numA 与列表 B 的第一个元素的大小关系 22 | if numA < B[0]: 23 | # 让当前元素与列表B的最大元素比较,输1分。 24 | result -= 1 25 | B.pop() 26 | else: 27 | # 如果大于,则赢1分,如果相等,则不加分 28 | result += 1 if numA > B[0] else 0 29 | B.pop(0) 30 | 31 | return result 32 | 33 | 34 | if __name__ == '__main__': 35 | assert solve_method([4, 8, 10], [3, 6, 4]) == 3 36 | -------------------------------------------------------------------------------- /codes/questions200/002_the-number-of-Linux-distributions.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: ZhouLixuan 5 | @file: 002_the-number-of-Linux-distributions.py 6 | @time: 2023/7/15 7 | @project: huawei-od-python 8 | @desc: 002 Linux发行版的数量 9 | """ 10 | 11 | 12 | def dfs(matrix, i, count, visited): 13 | # 标记节点i已访问 14 | visited[i] = 1 15 | # 记录遍历的结点数 16 | count[0] += 1 17 | 18 | for j in range(len(matrix)): 19 | # 对邻接矩阵中相连结点做DFS 20 | if matrix[i][j] == 1 and visited[j] == 0: 21 | dfs(matrix, j, count, visited) 22 | 23 | 24 | def solve_method(matrix): 25 | # 初始化访问数组 26 | visited = [0] * len(matrix) 27 | res = 0 28 | 29 | for i in range(len(matrix)): 30 | # 发现新连通分量进行DFS 31 | if visited[i] == 0: 32 | count = [0] 33 | dfs(matrix, i, count, visited) 34 | # 记录最大结点数 35 | res = max(res, count[0]) 36 | 37 | return res 38 | 39 | 40 | if __name__ == '__main__': 41 | matrix = [[1, 1, 0, 0], 42 | [1, 1, 1, 0], 43 | [0, 1, 1, 0], 44 | [0, 0, 0, 1]] 45 | assert solve_method(matrix) == 3 46 | -------------------------------------------------------------------------------- /codes/questions200/004_number-without-101.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: ZhouLixuan 5 | @file: 004_number-without-101.py 6 | @time: 2023/7/23 7 | @project: huawei-od-python 8 | @desc: 004 不含101的数 9 | """ 10 | 11 | 12 | def solve_method(l, r): 13 | count = 0 14 | for i in range(l, r + 1): 15 | i_bin = bin(i) 16 | if '101' not in i_bin: 17 | count += 1 18 | 19 | return count 20 | 21 | 22 | if __name__ == '__main__': 23 | assert solve_method(1, 10) == 8 24 | assert solve_method(10, 20) == 7 25 | -------------------------------------------------------------------------------- /codes/questions200/005_Cloud-SMS-platform-promotional-activity.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: ZhouLixuan 5 | @file: 005_Cloud-SMS-platform-promotional-activity.py 6 | @time: 2023/7/24 7 | @project: huawei-od-python 8 | @desc: 005 Excel 云短信平台优惠活动 9 | """ 10 | 11 | max_count = 0 12 | 13 | 14 | def solve_method(M, P): 15 | backstracking(P, M, [], 0) 16 | return max_count 17 | 18 | 19 | def backstracking(P, n, paths, index): 20 | global max_count 21 | # 预算已耗光,计算当前总条数,并记录最大值 22 | if n == 0: 23 | count = sum(paths) 24 | max_count = max(max_count, count) 25 | return 26 | 27 | for i in range(index, len(P)): 28 | x = int(P[i]) 29 | paths.append(x) 30 | # 继续遍历所有可能性 31 | backstracking(P, n - (i + 1), paths, i + 1) 32 | paths.pop() 33 | 34 | 35 | if __name__ == '__main__': 36 | nums = [10, 20, 30, 40, 60] 37 | assert solve_method(6, nums) == 70 38 | 39 | nums = [10, 20, 30, 40, 60, 60, 70, 80, 90, 150] 40 | assert solve_method(15, nums) == 210 41 | -------------------------------------------------------------------------------- /codes/questions200/006_task-hybridization.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: ZhouLixuan 5 | @file: 006_task-hybridization.py 6 | @time: 2023/7/25 7 | @project: huawei-od-python 8 | @desc: 006 任务混部 9 | """ 10 | from collections import defaultdict 11 | 12 | 13 | def solve_method(tasks): 14 | # 时刻所占服务器数量的字典,key为时刻,value为服务器数量 15 | time_servers = defaultdict(int) 16 | 17 | for task in tasks: 18 | parallelism = task[2] 19 | for i in range(task[0], task[1]): 20 | time_servers[i] += parallelism 21 | 22 | # 取出服务器数量最大的那个 23 | return max(time_servers.values()) 24 | 25 | 26 | if __name__ == '__main__': 27 | tasks = [[2, 3, 1], 28 | [6, 9, 2], 29 | [0, 5, 1]] 30 | 31 | assert solve_method(tasks) == 2 32 | 33 | tasks = [[3, 9, 2], 34 | [4, 7, 3]] 35 | 36 | assert solve_method(tasks) == 5 37 | -------------------------------------------------------------------------------- /codes/questions200/008_elegant-array.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: ZhouLixuan 5 | @file: 008_elegant-array.py 6 | @time: 2023/7/27 7 | @project: huawei-od-python 8 | @desc: 008 优雅数组 9 | """ 10 | from collections import Counter 11 | 12 | 13 | def solve_method(arr, k): 14 | res = 0 15 | n = len(arr) 16 | # i是子数组起点 17 | for i in range(n): 18 | # j是子数组终点 19 | for j in range(i, n): 20 | # 记录子数组的数字频数 21 | counter = Counter(arr[i: j + 1]) 22 | # 统计最大的次数是否大于等于k,是否为k-优雅数组 23 | if counter.most_common(1)[0][1] >= k: 24 | res += 1 25 | return res 26 | 27 | 28 | if __name__ == '__main__': 29 | assert solve_method([1, 2, 3, 1, 2, 3, 1], 3) == 1 30 | assert solve_method([1, 2, 3, 1, 2, 3, 1], 2) == 10 31 | -------------------------------------------------------------------------------- /codes/questions200/011_bonus-distribution.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: ZhouLixuan 5 | @file: 011_bonus-distribution.py 6 | @time: 2023/8/14 7 | @project: huawei-od-python 8 | @desc: 011 分奖金 9 | """ 10 | 11 | 12 | def solve_method(rand_numbers): 13 | res = [] 14 | 15 | for i in range(len(rand_numbers)): 16 | num = rand_numbers[i] 17 | find = False 18 | # 往后找大于该员工随机数的值 19 | for j in range(i, len(rand_numbers)): 20 | other = rand_numbers[j] 21 | if other > num: 22 | # 如果遇到第一个比自己数字大的,则奖金等于距离*数字差值,并跳出循环 23 | res.append((j - i) * (other - num)) 24 | find = True 25 | break 26 | if not find: 27 | # 如果找不到更大的随机数,则奖金等于该员工的随机数值 28 | res.append(num) 29 | 30 | return res 31 | 32 | 33 | if __name__ == '__main__': 34 | rand_numbers = [2, 10, 3] 35 | assert solve_method(rand_numbers) == [8, 10, 3] 36 | -------------------------------------------------------------------------------- /codes/questions200/018_finding-similar-words.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: ZhouLixuan 5 | @file: 018_finding-similar-words.py 6 | @time: 2023/8/19 7 | @project: huawei-od-python 8 | @desc: 018 寻找相似单词 9 | """ 10 | from collections import Counter 11 | 12 | 13 | def solve_method(words, target_word): 14 | result = [] 15 | for word in words: 16 | if Counter(word) == Counter(target_word): 17 | result.append(word) 18 | 19 | result.sort() 20 | return result if len(result) != 0 else "null" 21 | 22 | 23 | if __name__ == '__main__': 24 | words = ["abc", "dasd", "tad", "bca"] 25 | assert solve_method(words, "abc") == ["abc", "bca"] 26 | 27 | assert solve_method(words, "abd") == "null" 28 | -------------------------------------------------------------------------------- /codes/questions200/019_finding-the-longest-substring-meeting-requirements.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: ZhouLixuan 5 | @file: 019_finding-the-longest-substring-meeting-requirements.py 6 | @time: 2023/7/30 7 | @project: huawei-od-python 8 | @desc: 019 寻找符合要求的最长子串 9 | """ 10 | from collections import Counter 11 | 12 | 13 | def solve_method(ch, chars): 14 | chars_lst = chars.split(ch) 15 | 16 | result = [] 17 | for sub_chars in chars_lst: 18 | counter = Counter(sub_chars) 19 | # 判断子串的任意字符最多出现2次 20 | if all([True if v <= 2 else False for k, v in counter.items()]): 21 | result.append(sub_chars) 22 | 23 | # 将满足条件的子串按照子串长度从大到小排序 24 | result.sort(key=lambda x: len(x), reverse=True) 25 | # 返回最长的子串 26 | return len(result[0]) if len(result) != 0 else 0 27 | 28 | 29 | if __name__ == '__main__': 30 | assert solve_method('D', "ABC123") == 6 31 | assert solve_method('D', "ABACABD12321") == 5 32 | -------------------------------------------------------------------------------- /codes/questions200/021_express-delivery-station.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: ZhouLixuan 5 | @file: 021_express-delivery-station.py 6 | @time: 2023/7/24 7 | @project: huawei-od-python 8 | @desc: 021 快递业务站 9 | """ 10 | 11 | 12 | def solve_method(N, stations_relations): 13 | cover = set() 14 | res = 0 15 | 16 | for i in range(N): 17 | if i not in cover: 18 | res += 1 19 | cover.add(i) 20 | for j in filter(lambda x: x != i, range(N)): 21 | if stations_relations[i][j] == 1: 22 | cover.add(j) 23 | 24 | return res 25 | 26 | 27 | if __name__ == '__main__': 28 | stations_relations = [ 29 | [1, 1, 1, 1], 30 | [1, 1, 1, 0], 31 | [1, 1, 1, 0], 32 | [1, 0, 0, 1], 33 | ] 34 | 35 | assert solve_method(4, stations_relations) == 1 36 | 37 | stations_relations = [ 38 | [1, 1, 0, 0], 39 | [1, 1, 0, 0], 40 | [0, 0, 1, 0], 41 | [0, 0, 0, 1], 42 | ] 43 | 44 | assert solve_method(4, stations_relations) == 3 45 | -------------------------------------------------------------------------------- /codes/questions200/023_searching-land-plot-establishment.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: ZhouLixuan 5 | @file: 023_searching-land-plot-establishment.py 6 | @time: 2023/7/24 7 | @project: huawei-od-python 8 | @desc: 023 探索地块建立 9 | """ 10 | 11 | 12 | def solve_method(mat, n, m, c, threshold): 13 | result = 0 14 | # 遍历矩阵中的每个元素 15 | for i in range(n - c + 1): 16 | for j in range(m - c + 1): 17 | total = 0 18 | 19 | # 计算以该元素为起点的c*c个元素之和 20 | for k in range(c): 21 | for l in range(c): 22 | total += mat[i + k][j + l] 23 | 24 | # 判断是否超预期 25 | if total >= threshold: 26 | result += 1 27 | 28 | return result 29 | 30 | 31 | if __name__ == '__main__': 32 | matrix = [[1, 3, 4, 5, 8], 33 | [2, 3, 6, 7, 1]] 34 | 35 | assert solve_method(matrix, 2, 5, 2, 6) == 4 36 | -------------------------------------------------------------------------------- /codes/questions200/024_number-addition-and-subtraction-game.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: HuRuiFeng 5 | @file: 024_number-addition-and-subtraction-game.py 6 | @time: 2023/8/16 15:18 7 | @project: huawei-od-python 8 | @desc: 024 数字加减游戏 9 | """ 10 | 11 | 12 | def solve_method(s, t, a, b): 13 | diff = abs(s - t) 14 | min_count = 0 15 | while ((diff - min_count * a) % b != 0) and ((diff + min_count * a) % b != 0): 16 | min_count += 1 17 | return min_count 18 | 19 | 20 | if __name__ == "__main__": 21 | assert solve_method(1, 10, 5, 2) == 1 22 | assert solve_method(11, 33, 4, 10) == 2 23 | -------------------------------------------------------------------------------- /codes/questions200/025_undirected-graph-coloring-problem.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: Libihan 5 | @file: 025_undirected-graph-coloring-problem.py 6 | @time: 2023/8/16 15:18 7 | @project: huawei-od-python 8 | @desc: 025 无向图染色问题、红黑图 9 | """ 10 | 11 | 12 | def solve_method(n, m, edges): 13 | count = 0 14 | for num in range(1 << n): 15 | # 标记是否满足要求 16 | flag = True 17 | for edge in edges: 18 | if (num & (1 << edge[0])) and (num & (1 << edge[1])): 19 | flag = False 20 | break 21 | count += flag 22 | return count 23 | 24 | 25 | if __name__ == "__main__": 26 | # 3 3 27 | # 0 1 28 | # 0 2 29 | # 1 2 30 | # n个节点,m条边 31 | # n, m = map(int, input().split()) 32 | # edges = [] 33 | # for _ in range(m): 34 | # edges.append(list(map(int, input().split()))) 35 | # print(solve_method(n, m, edges)) 36 | assert solve_method(3, 3, [[0, 1], [0, 2], [1, 2]]) == 4 37 | assert solve_method(4, 3, [[0, 1], [1, 2], [2, 3]]) == 8 38 | -------------------------------------------------------------------------------- /codes/questions200/026_optimal-resource-allocation.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: Libihan 5 | @file: 026_optimal-resource-allocation.py 6 | @time: 2023/8/17 14:08 7 | @project: huawei-od-python 8 | @desc: 026 最优资源分配 9 | """ 10 | 11 | 12 | def solve_method(M, N, config): 13 | cores = [M] * N 14 | used = [1, 2, 8] 15 | for c in config: 16 | cur = used[ord(c) - ord('A')] 17 | # 资源分配规则:按芯片编号从小到大分配资源 18 | for i in range(N): 19 | # 当前芯片容量,满足用户配置 20 | if cores[i] >= cur: 21 | cores[i] -= cur 22 | break 23 | return ['1' * (M - core) + '0' * core for core in cores] 24 | 25 | 26 | if __name__ == "__main__": 27 | # 8 28 | # 2 29 | # ACABA 30 | # M = int(input()) 31 | # N = int(input()) 32 | # config = input().strip() 33 | # for core in solve_method(M, N, config): 34 | # print(core) 35 | assert solve_method(8, 2, "ACABA") == ["11111000", "11111111"] 36 | assert solve_method(8, 2, "ACBCB") == ["11111000", "11111111"] 37 | -------------------------------------------------------------------------------- /codes/questions200/030_min-number-of-segments-covered.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: Libihan 5 | @file: 030_min-number-of-segments-covered.py 6 | @time: 2023/8/21 11:30 7 | @project: huawei-od-python 8 | @desc: 030 最少数量线段覆盖 9 | """ 10 | from collections import defaultdict 11 | 12 | 13 | def solve_method(n, points): 14 | # line_dict存储整数点和出现的次数 15 | line_dict = defaultdict(int) 16 | for point in points: 17 | for i in range(point[0], point[1] + 1): 18 | line_dict[i] += 1 19 | # del_count记录可删除的线段数 20 | del_count = 0 21 | for point in points: 22 | # 如果若线段上每个点出现次数均大于1,则可以删除该线段 23 | if all([True if line_dict[i] > 1 else False for i in range(point[0], point[1] + 1)]): 24 | for i in range(point[0], point[1] + 1): 25 | line_dict[i] -= 1 26 | del_count += 1 27 | return n - del_count 28 | 29 | 30 | if __name__ == "__main__": 31 | # n = int(input()) 32 | # points = [] 33 | # for _ in range(n): 34 | # x, y = map(int, input().split(',')) 35 | # points.append((x, y)) 36 | # print(solve_method(n, points)) 37 | 38 | assert solve_method(3, [[1, 4], [2, 5], [3, 6]]) == 2 39 | -------------------------------------------------------------------------------- /codes/questions200/031_worst-product-award.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: Libihan 5 | @file: 031_worst-product-award.py 6 | @time: 2023/8/21 15:19 7 | @project: huawei-od-python 8 | @desc: 031 最差产品奖 9 | """ 10 | 11 | 12 | def solve_method(M, scores): 13 | min_scores = [] 14 | for i in range(len(scores) - M + 1): 15 | min_score = min(scores[i:i + M]) 16 | min_scores.append(min_score) 17 | return min_scores 18 | 19 | 20 | if __name__ == "__main__": 21 | # 3 22 | # 12,3,8,6,5 23 | # M = int(input()) 24 | # scores = list(map(int, input().split(','))) 25 | # print(solve_method(M, scores)) 26 | 27 | assert solve_method(3, [12, 3, 8, 6, 5]) == [3, 3, 5] 28 | -------------------------------------------------------------------------------- /codes/questions200/039_combine-the-legal-minimum.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: Libihan 5 | @file: 039_combine-the-legal-minimum.py 6 | @time: 2023/8/22 9:50 7 | @project: huawei-od-python 8 | @desc: 039 组合出合法最小数 9 | """ 10 | 11 | 12 | def solve_method(nums): 13 | nums_zero, nums_non_zero = [], [] 14 | for num in nums: 15 | if num.startswith('0'): 16 | nums_zero.append(num) 17 | else: 18 | nums_non_zero.append(num) 19 | 20 | nums_zero.sort() 21 | nums_non_zero.sort() 22 | 23 | # 如果不存在0开头的数字 24 | if len(nums_zero) == 0: 25 | return "".join(nums_non_zero) 26 | # 如果存在0开头的数字,也存在非0开头的数字 27 | if len(nums_non_zero) > 0: 28 | return "".join([nums_non_zero[0]] + nums_zero + nums_non_zero[1:]) 29 | # 如果只存在0开头的数字 30 | return "".join(nums_zero).lstrip('0') 31 | 32 | 33 | if __name__ == "__main__": 34 | # 20 1 35 | # nums = list(map(str, input().strip().split())) 36 | # print(solve_method(nums)) 37 | 38 | assert solve_method(["20", "1"]) == "120" 39 | assert solve_method(["08", "10", "2"]) == "10082" 40 | assert solve_method(["01", "02"]) == "102" 41 | -------------------------------------------------------------------------------- /codes/questions200/040_assemble-new-array.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: Libihan 5 | @file: 040_assemble-new-array.py 6 | @time: 2023/8/22 10:15 7 | @project: huawei-od-python 8 | @desc: 040 组装新的数组 9 | """ 10 | 11 | 12 | def solve_method(nums, m): 13 | min_val = nums[0] 14 | nums = [num for num in nums if num <= m] 15 | return dfs(nums, m, 0, min_val, 0) 16 | 17 | 18 | def dfs(nums, m, index, min_val, count): 19 | if m < 0: 20 | return count 21 | if m == 0 or m < min_val: 22 | return count + 1 23 | 24 | for i in range(index, len(nums)): 25 | count = dfs(nums, m - nums[i], i, min_val, count) 26 | return count 27 | 28 | 29 | if __name__ == "__main__": 30 | # 2 31 | # 5 32 | # N = list(map(int, input().strip().split())) 33 | # M = int(input().strip()) 34 | # print(solve_method(N, M)) 35 | 36 | assert solve_method([2], 5) == 1 37 | assert solve_method([2, 3], 5) == 2 38 | assert solve_method([3, 4], 2) == 1 39 | -------------------------------------------------------------------------------- /codes/questions200/042_count-the-number-of-matching-pairs.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: Libihan 5 | @file: 042_count-the-number-of-matching-pairs.py 6 | @time: 2023/8/22 16:58 7 | @project: huawei-od-python 8 | @desc: 042 统计匹配的二元组个数 9 | """ 10 | from collections import Counter 11 | 12 | 13 | def solve_method(A, B): 14 | dict_A = Counter(A) 15 | dict_B = Counter(B) 16 | 17 | count = 0 18 | for key in dict_A: 19 | if key in dict_B: 20 | count += dict_A[key] * dict_B[key] 21 | return count 22 | 23 | 24 | if __name__ == "__main__": 25 | # 5 26 | # 4 27 | # 1 2 3 4 5 28 | # 4 3 2 1 29 | # M = int(input().strip()) 30 | # N = int(input().strip()) 31 | # A = list(map(int, input().strip().split())) 32 | # B = list(map(int, input().strip().split())) 33 | # print(solve_method(A, B)) 34 | 35 | assert solve_method([1, 2, 3, 4, 5], [4, 3, 2, 1]) == 4 36 | assert solve_method([1, 2, 4, 4, 2, 1], [1, 2, 3]) == 4 37 | -------------------------------------------------------------------------------- /codes/questions200/043_comparison-of-similarities-and-differences.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: Libihan 5 | @file: 043_comparison-of-similarities-and-differences.py 6 | @time: 2023/8/22 18:46 7 | @project: huawei-od-python 8 | @desc: 043 统计差异值大于相似值二元组个数 9 | """ 10 | 11 | 12 | def solve_method(n, nums): 13 | count = 0 14 | for i in range(n): 15 | for j in range(i + 1, n): 16 | diff = nums[i] ^ nums[j] 17 | simi = nums[i] & nums[j] 18 | if diff > simi: 19 | count += 1 20 | return count 21 | 22 | 23 | if __name__ == "__main__": 24 | # 4 25 | # 4 3 5 2 26 | # n = int(input().strip()) 27 | # nums = list(map(int, input().strip().split())) 28 | # print(solve_method(n, nums)) 29 | 30 | assert solve_method(4, [4, 3, 5, 2]) == 4 31 | assert solve_method(5, [3, 5, 2, 8, 4]) == 8 32 | -------------------------------------------------------------------------------- /codes/questions200/046_increment-string.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: Libihan 5 | @file: 046_increment-string.py 6 | @time: 2023/8/22 21:32 7 | @project: huawei-od-python 8 | @desc: 046 递增字符串 9 | """ 10 | 11 | 12 | def solve_method(chars): 13 | total_A = chars.count("A") 14 | res = total_A 15 | count_A = 0 16 | for i in range(len(chars)): 17 | if chars[i] == "A": 18 | count_A += 1 19 | # 将区间[0, i]全部转成A的修改次数 20 | count_before_i = i + 1 - count_A 21 | # 将区间[i+1, len(chars)]全部转为B的修改次数 22 | count_after_i = total_A - count_A 23 | # 计算最小值 24 | res = min(res, count_before_i + count_after_i) 25 | return res 26 | 27 | 28 | if __name__ == "__main__": 29 | # AABBA 30 | # str = input().strip() 31 | # print(solve_method(str)) 32 | 33 | assert solve_method("AABBA") == 1 34 | assert solve_method("BAABBABBAB") == 3 35 | -------------------------------------------------------------------------------- /codes/questions200/047_divide-the-string.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: Libihan 5 | @file: 047_divide-the-string.py 6 | @time: 2023/9/3 23:18 7 | @project: huawei-od-python 8 | @desc: 047 划分字符串 9 | """ 10 | 11 | 12 | def solve_method(s): 13 | n = len(s) 14 | left, right = 1, n - 2 15 | sum_total = sum(ord(c) for c in s) 16 | sum_left = ord(s[0]) 17 | sum_right = ord(s[n - 1]) 18 | while left < right: 19 | sum_middle = sum_total - sum_left - sum_right - ord(s[left]) - ord(s[right]) 20 | if sum_left == sum_middle == sum_right: 21 | return f"{left},{right}" 22 | if sum_left > sum_right: 23 | sum_right += ord(s[right]) 24 | right -= 1 25 | elif sum_left < sum_right: 26 | sum_left += ord(s[left]) 27 | left += 1 28 | else: 29 | sum_left += ord(s[left]) 30 | sum_right += ord(s[right]) 31 | left += 1 32 | right -= 1 33 | return "0,0" 34 | 35 | 36 | if __name__ == '__main__': 37 | assert solve_method("acdbbbca") == "2,5" 38 | assert solve_method("abcabc") == "0,0" 39 | -------------------------------------------------------------------------------- /codes/questions200/049_enhanced-strstr.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: Libihan 5 | @file: 049_enhanced-strstr.py 6 | @time: 2023/9/3 23:18 7 | @project: huawei-od-python 8 | @desc: 049 增强的strstr 9 | """ 10 | import re 11 | 12 | 13 | def solve_method(haystack, needle): 14 | match = re.search(needle, haystack) 15 | return match.start() if match else -1 16 | 17 | 18 | if __name__ == '__main__': 19 | assert solve_method("abcd", "b[cd]") == 1 20 | assert solve_method("aabcdefg", "b[cd]d[eg]") == 2 21 | -------------------------------------------------------------------------------- /codes/questions200/051_print-task-sequencing.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | """ 4 | @author: Libihan 5 | @file: 051_print-task-sequencing.py 6 | @time: 2023/9/3 23:18 7 | @project: huawei-od-python 8 | @desc: 051 打印任务排序 9 | """ 10 | 11 | 12 | def solve_method(tasks): 13 | task_index = [(task, idx) for idx, task in enumerate(tasks)] 14 | n = len(tasks) 15 | 16 | # 标记当前优先级最高的任务 17 | tasks.sort() 18 | max_idx = n - 1 19 | # 标记打印顺序 20 | res = [0] * n 21 | printIdx = 0 22 | while task_index: 23 | task, idx = task_index.pop(0) 24 | # 是优先级最高的任务 25 | if task == tasks[max_idx]: 26 | res[idx] = printIdx 27 | max_idx -= 1 28 | printIdx += 1 29 | # 不是优先级最高的任务,加入队尾 30 | else: 31 | task_index.append((task, idx)) 32 | return res 33 | 34 | 35 | if __name__ == '__main__': 36 | assert solve_method([9, 3, 5]) == [0, 2, 1] 37 | assert solve_method([1, 2, 2]) == [2, 0, 1] 38 | -------------------------------------------------------------------------------- /docs/.nojekyll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/datawhalechina/huawei-od-python/98cb81c5e15df351f831b9dfc19caf0e5af1e18a/docs/.nojekyll -------------------------------------------------------------------------------- /docs/choice100/025_choose-location-of-the-new-school.md: -------------------------------------------------------------------------------- 1 | # 025 新学校选址 2 | 3 | ## 题目描述 4 | 5 | 为了解新学期学生暴涨的问题,小乐村要建立一所新学校,考虑到学生上学安全问题,需要所有学生家到学校的距离最短,假设学校和所有学生家都走在一条直线上,请问学校建立在什么位置,才能使得学校到各个学生家的距离和最短。 6 | 7 | ## 输入描述 8 | 9 | 第一行是`n`,表示有`n`户家庭,整数`n`取值范围为[1, 1000]。 10 | 11 | 第二行是一组整数,表示每户家庭的位置,所有家庭的位置都不相同,其中每个元素取值范围[0, 10000]。 12 | 13 | ## 输出描述 14 | 15 | 一个整数,确定学校的位置,如果有多个位置,则输出最小的。 16 | 17 | ## 示例描述 18 | 19 | ### 示例一 20 | 21 | **输入:** 22 | ```text 23 | 5 24 | 0 20 40 10 30 25 | ``` 26 | 27 | **输出:** 28 | ```text 29 | 20 30 | ``` 31 | 32 | ## 解题思路 33 | 34 | 基本思路:本题是求数组的中位数。 35 | 1. 使用`sort`方法,将数组排序。 36 | 2. 获取数组的中间值,即为距离和最小的位置。 37 | - 如果数组元素个数为偶数,返回数组的中间位置的前一个数据。 38 | - 如果数组元素个数为奇数,返回数组的中间位置的数据。 39 | 40 | ## 解题代码 41 | 42 | ```python 43 | def solve_method(arr): 44 | arr.sort() 45 | if len(arr) % 2 == 0: 46 | return arr[len(arr) // 2 - 1] 47 | else: 48 | return arr[len(arr) // 2] 49 | 50 | 51 | if __name__ == '__main__': 52 | assert solve_method([0, 20, 40, 10, 30]) == 20 53 | ``` -------------------------------------------------------------------------------- /docs/choice100/039_car-rental-ride-Green-Island.md: -------------------------------------------------------------------------------- 1 | # 039 租车骑绿岛 2 | 3 | ## 题目描述 4 | 5 | 部门组织绿岛骑行团建活动。租用公共双人自行车骑行,每辆自行车最多坐两人、最大载重`M`。给出部门每个人的体重,请问最多需要租用多少双人自行车。 6 | 7 | ## 输入描述 8 | 9 | 第一行两个数字`m`、`n`,`m`表示自行车限重,`n`表示部门总人数。 10 | 11 | 第二行,`n`个数字,代表每个人的体重。体重都小于等于自行车限重`m`。 12 | 13 | 取值范围为: 14 | - 0 < m <= 200 15 | - 0 < n <= 1000000 16 | 17 | ## 输出描述 18 | 19 | 最小需要的双人自行车数量。 20 | 21 | ## 示例描述 22 | 23 | ### 示例一 24 | 25 | **输入:** 26 | ```text 27 | 3 4 28 | 3 2 2 1 29 | ``` 30 | 31 | **输出:** 32 | ```text 33 | 3 34 | ``` 35 | 36 | ## 解题思路 37 | 38 | **基本思路:** 使用双指针解题。 39 | 1. 将所有人体重从小到大排序。 40 | 2. 左指针设置为0,右指针设置为列表末尾位置。 41 | 3. 使用`while`循环,从右到左遍历: 42 | - 如果两个人的体重小于等于上限,已经找到了可以共享骑车的人,左指针向右移动。 43 | - 如果两个人的体重大于上限,独占一车,右指针向左移动。 44 | - 统计结果加1。 45 | 4. 返回统计结果。 46 | 47 | ## 解题代码 48 | 49 | ```python 50 | def solve_method(m, arr): 51 | arr.sort() 52 | count = 0 53 | 54 | i = 0 55 | j = len(arr) - 1 56 | 57 | while i < j: 58 | if arr[i] + arr[j] <= m: 59 | i += 1 60 | j -= 1 61 | count += 1 62 | 63 | if i == j: 64 | count += 1 65 | 66 | return count 67 | 68 | 69 | if __name__ == '__main__': 70 | assert solve_method(3, [3, 2, 2, 1]) == 3 71 | ``` -------------------------------------------------------------------------------- /docs/choice100/images/041-001-zigzag.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/datawhalechina/huawei-od-python/98cb81c5e15df351f831b9dfc19caf0e5af1e18a/docs/choice100/images/041-001-zigzag.png -------------------------------------------------------------------------------- /docs/images/qrcode.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/datawhalechina/huawei-od-python/98cb81c5e15df351f831b9dfc19caf0e5af1e18a/docs/images/qrcode.jpeg -------------------------------------------------------------------------------- /docs/others100/015_swap-characters.md: -------------------------------------------------------------------------------- 1 | # 015 交换字符 2 | 3 | ## 题目描述 4 | 5 | 给定一个字符串`S`,将字符串进行变换,变化规则是交换字符串中任意两个不同位置的字符`M`,其中`S`都是由小写字符组成,字符串的长度范围是`1 <= S.length <= 1000`。 6 | 7 | ## 输入描述 8 | 9 | 一串小写字母组成的字符串。 10 | 11 | ## 输出描述 12 | 13 | 按照要求变换得到最小字符串。 14 | 15 | ## 示例描述 16 | 17 | ### 示例一 18 | 19 | **输入:** 20 | ```text 21 | abcdef 22 | ``` 23 | 24 | **输出:** 25 | ```text 26 | abcdef 27 | ``` 28 | 29 | ### 示例二 30 | 31 | **输入:** 32 | 33 | ```text 34 | bcdefa 35 | ``` 36 | 37 | **输出:** 38 | 39 | ```text 40 | acdefb 41 | ``` 42 | 43 | ## 解题思路 44 | 45 | 1. 初始化最小的字符`min_char`、字符对应的位置`pos`。 46 | 2. 遍历字符串:获取按字典序最小的字符,并得到对应的位置。 47 | 3. 将最小的字符与第一个字符进行交换。 48 | 4. 返回结果字符串。 49 | 50 | ## 解题代码 51 | 52 | ```python 53 | def solve_method(string): 54 | string = list(string) 55 | pos = 0 56 | min_char = string[0] 57 | for i in range(1, len(string)): 58 | char = string[i] 59 | if char <= min_char: 60 | min_char, pos = char, i 61 | 62 | if pos != 0: 63 | string[pos], string[0] = string[0], min_char 64 | 65 | return "".join(string) 66 | 67 | 68 | if __name__ == '__main__': 69 | assert solve_method("abcdef") == "abcdef" 70 | assert solve_method("bcdefa") == "acdefb" 71 | ``` 72 | 73 | -------------------------------------------------------------------------------- /docs/others100/033_find-prime-factors.md: -------------------------------------------------------------------------------- 1 | # 033 分解质因数 2 | 3 | ## 题目描述 4 | 5 | 给定一个正整数,将其分解成两个质数的乘积,输出两个质数,按从小到大排序,有多组只需输出一组,如果没有则输出`NO`。 6 | 7 | ## 示例描述 8 | 9 | ### 示例一 10 | 11 | **输入:** 12 | 13 | ```text 14 | 15 15 | ``` 16 | 17 | **输出:** 18 | 19 | ```text 20 | 3 5 21 | ``` 22 | 23 | ### 示例二 24 | 25 | **输入:** 26 | 27 | ```text 28 | 7 29 | ``` 30 | 31 | **输出:** 32 | 33 | ```text 34 | NO 35 | ``` 36 | 37 | ## 解题思路 38 | 39 | 1. 找出所有小于num的素数,存入因数为素数的`factors`集合中。 40 | 2. 遍历`factors`集合,找出两个素数,满足因数分解。 41 | 3. 如果找到,返回两个素数,如果找不到,返回`NO`。 42 | 43 | ## 解题代码 44 | 45 | ```Python 46 | def solve_method(num): 47 | # 找出所有小于num的素数 48 | tmp = num 49 | f = 2 50 | factors = set() 51 | while tmp != 1: 52 | if tmp % f != 0: 53 | f += 1 54 | else: 55 | factors.add(f) 56 | tmp //= f 57 | 58 | # 找出两个素数,满足因数分解 59 | for f1 in factors: 60 | for f2 in factors: 61 | if f1 * f2 == num: 62 | return min(f1, f2), max(f1, f2) 63 | 64 | return "NO" 65 | 66 | 67 | if __name__ == '__main__': 68 | assert solve_method(15) == (3, 5) 69 | assert solve_method(7) == "NO" 70 | ``` 71 | 72 | -------------------------------------------------------------------------------- /docs/others100/045_sum-number.md: -------------------------------------------------------------------------------- 1 | # 045 去重求和 2 | 3 | ## 题目描述 4 | 5 | 给定一个数组,编写一个函数,计算他的最大`N`个数和最小`N`个数的和,需要对数组进行去重。 6 | 7 | ## 输入描述 8 | 9 | 第一行输入`M`,表示数组大小。 10 | 11 | 第二行输入`M`个数,表示数组内容。 12 | 13 | 第三行输入`N`,表示需要计算的最大最小`N`的个数。 14 | 15 | ## 输出描述 16 | 17 | 输出最大N个数和最小N个数的和。 18 | 19 | ## 示例描述 20 | 21 | ### 示例一 22 | 23 | **输入:** 24 | ``` 25 | 5 26 | 95 88 83 64 100 27 | 2 28 | ``` 29 | 30 | **输出:** 31 | ``` 32 | 342 33 | ``` 34 | **说明** 35 | 36 | 最大2个数是100和95,最小2个数是83和64,求和之后输出342。 37 | 38 | ### 示例二 39 | 40 | **输入:** 41 | 42 | ``` 43 | 5 44 | 3 2 3 4 2 45 | 2 46 | ``` 47 | 48 | **输出:** 49 | ``` 50 | -1 51 | ``` 52 | 53 | **说明** 54 | 55 | 最大2个数是4和3,最小2个数是3和2,有重叠输出为-1。 56 | 57 | ## 解题思路 58 | 59 | 1. 对数组使用`set`和`sorted`进行去重排序。 60 | 2. 如果数组长度足够分成最大的前n个数和最小的前n个数据,返回求和的值。否则,返回-1。 61 | 62 | ## 解题代码 63 | 64 | ```python 65 | def solve_method(nums, n): 66 | nums = sorted(set(nums)) 67 | result = -1 68 | if len(nums) >= 2 * n: 69 | result = sum(nums[:n]) + sum(nums[-n:]) 70 | 71 | return result 72 | 73 | 74 | if __name__ == '__main__': 75 | assert solve_method([95, 88, 83, 64, 100], 2) == 342 76 | assert solve_method([3, 2, 3, 4, 2], 2) == -1 77 | ``` 78 | 79 | -------------------------------------------------------------------------------- /docs/others100/049_the-boundary-value-of-the-matrix-element.md: -------------------------------------------------------------------------------- 1 | # 049 矩阵元素的边界值 2 | 3 | ## 题目描述 4 | 5 | 给定一个`N*M`矩阵,请先找出`M`个该矩阵中每列元素的最大值,然后输出这`M`个值中的最小值。 6 | 7 | **补充说明:** 8 | 9 | `N`和`M`的取值范围均为:[0,100]。 10 | 11 | ## 输入描述 12 | 13 | 输入一个二维矩阵。 14 | 15 | ## 输出描述 16 | 17 | 输出一个数,表示该矩阵中每列元素的最大值的最小值。 18 | 19 | ## 示例描述 20 | 21 | ### 示例一 22 | 23 | **输入:** 24 | ```text 25 | [[1,2],[3,4]] 26 | ``` 27 | 28 | **输出:** 29 | ```text 30 | 3 31 | ``` 32 | 33 | **说明:** 34 | 35 | 第一列元素为:1和3,最大值为3。 36 | 第二列元素为:2和4,最大值为4。 37 | 38 | 各列最大值3和4的最小值为3。 39 | 40 | ## 解题思路 41 | 42 | 1. 初始化结果列表,设置为矩阵的第一个元素。 43 | 2. 从第二个元素开始,遍历矩阵: 44 | - 得到每列元素的最大值,存入结果列表中。 45 | 3. 将结果列表从小到大排序。 46 | 4. 返回结果列表中的第一个元素,即每列元素的最大值的最小值。 47 | 48 | ## 解题代码 49 | 50 | ```python 51 | def solve_method(arr): 52 | result_lst = arr[0] 53 | for lst in arr[1:]: 54 | result_lst = [max(x, y) for x, y in zip(result_lst, lst)] 55 | 56 | result_lst.sort() 57 | return result_lst[0] 58 | 59 | 60 | if __name__ == '__main__': 61 | arr = [[1, 2], [3, 4]] 62 | assert solve_method(arr) == 3 63 | 64 | arr = [[1, 2]] 65 | assert solve_method(arr) == 1 66 | ``` -------------------------------------------------------------------------------- /docs/others100/072_express-truck.md: -------------------------------------------------------------------------------- 1 | # 072 快递货车 2 | 3 | ## 题目描述 4 | 5 | 一辆运送快递的货车,运送的快递放在大小不等的长方体快递盒中,为了能够装载更多的快递,同时不能让货车超载,需要计算最多能装多少个快递。 6 | 7 | 注: 8 | - 快递的体积不受限制。 9 | - 快递数最多1000个,货车载重最大50000。 10 | 11 | ## 输入描述 12 | 13 | 第一行输入每个快递的重量,用英文逗号分隔,如`5,10,2,11`。 14 | 15 | 第二行输入货车的载重量,如20。 16 | 17 | ## 输出描述 18 | 19 | 输出最多能装多少个快递,如3。 20 | 21 | ## 示例描述 22 | 23 | ### 示例一 24 | 25 | **输入:** 26 | ```text 27 | 5,10,2,11 28 | 20 29 | ``` 30 | 31 | **输出:** 32 | ```text 33 | 3 34 | ``` 35 | 36 | ## 解题思路 37 | 38 | 1. 将输入的重量按从小到大的顺序排序, 39 | 2. 遍历所有快递: 40 | - 如果累加的快递总重量小于等于货车载重,就继续选取下一个快递,计数器累加1; 41 | - 否则,结束遍历。 42 | 3. 返回计数器的值,即已选取的快递个数。 43 | 44 | ## 解题代码 45 | 46 | ```python 47 | def solve_method(nums, weight): 48 | nums.sort() 49 | count = 0 50 | sum_weight = 0 51 | for i in range(len(nums)): 52 | sum_weight += nums[i] 53 | if sum_weight > weight: 54 | return count 55 | else: 56 | count += 1 57 | return count 58 | 59 | 60 | if __name__ == '__main__': 61 | assert solve_method([5, 10, 2, 11], 20) == 3 62 | ``` 63 | 64 | -------------------------------------------------------------------------------- /docs/others100/082_find-characters.md: -------------------------------------------------------------------------------- 1 | # 082 找字符 2 | 3 | ## 题目描述 4 | 5 | 给定两个字符串,从字符串2中找出字符串1中的所有字符,去重并按照ASCII码值从小到大排列。 6 | 7 | ## 输入描述 8 | 9 | 字符范围满足ASCII编码要求,输入字符串1长度不超过1024,字符串2长度不超过100。 10 | 11 | ## 输出描述 12 | 13 | 按照ASCII由小到大排序。 14 | 15 | ## 示例描述 16 | 17 | ### 示例一 18 | 19 | **输入:** 20 | ```text 21 | bach 22 | bbaaccddfg 23 | ``` 24 | 25 | **输出:** 26 | ```text 27 | abc 28 | ``` 29 | 30 | ### 示例二 31 | 32 | **输入:** 33 | ```text 34 | fach 35 | bbaaccedfg 36 | ``` 37 | 38 | **输出:** 39 | ```text 40 | acf 41 | ``` 42 | 43 | ## 解题思路 44 | 45 | 1. 将字符串`str1`和字符串`str2`分别转换成对应的集合。 46 | 2. 求集合的交集,并按照字典序排序。 47 | 3. 组合成字符串后,返回结果。 48 | 49 | ## 解题代码 50 | 51 | ```python 52 | def solve_method(str1, str2): 53 | str1 = set(str1) 54 | str2 = set(str2) 55 | return "".join(sorted(str1.intersection(str2))) 56 | 57 | 58 | if __name__ == '__main__': 59 | assert solve_method("bach", "bbaaccddfg") == "abc" 60 | assert solve_method("fach", "bbaaccedfg") == "acf" 61 | ``` -------------------------------------------------------------------------------- /docs/others100/114_mingming's-random-number.md: -------------------------------------------------------------------------------- 1 | # 114 明明的随机数 2 | 3 | ## 题目描述 4 | 5 | 明明生成了`N`个1到500之间的随机整数。请你删去其中重复的数字,即相同的数字只保留一个,把其余相同的数去掉,然后再把这些数从小到大排序,按照排好的顺序输出。 6 | 7 | 数据范围: 1 <= n <= 1000,输入的数字大小满足1 <= val <= 500 8 | 9 | ## 输入描述 10 | 11 | 第一行先输入随机整数的个数`N`。 12 | 13 | 接下来的`N`行,每行输入一个整数,代表明明生成的随机数。具体格式可以参考下面的"示例"。 14 | 15 | ## 输出描述 16 | 17 | 输出多行,表示输入数据处理后的结果。 18 | 19 | ## 示例描述 20 | 21 | ### 示例一 22 | 23 | **输入:** 24 | 25 | ```text 26 | 3 27 | 2 28 | 2 29 | 1 30 | ``` 31 | 32 | **输出:** 33 | 34 | ```text 35 | 1 36 | 2 37 | ``` 38 | 39 | **说明:** 40 | 41 | 第一个数字是3,表示这个示例用计算机生成了3个1到500之间的随机整数,接下来每行一个随机数字,分别是2、2、1。 42 | 43 | 去重,并重新排序之后,得到输出结果是1、2。 44 | 45 | ## 解题思路 46 | 47 | 1. 使用`set`方法去重。 48 | 2. 将`set`集合转换为`list`列表,并用`sort`排序,返回结果。 49 | 50 | ## 解题代码 51 | 52 | ```python 53 | def solve_method(lst): 54 | lst= list(set(lst)) 55 | lst.sort 56 | return lst 57 | 58 | 59 | 60 | if __name__ == '__main__': 61 | assert solve_method([2,2,1]) == [1,2] 62 | assert solve_method([3,6,9,8,2,1,1,9,8]) == [1,2,3,6,8,9] 63 | ``` 64 | 65 | -------------------------------------------------------------------------------- /docs/others100/116_lowest-order.md: -------------------------------------------------------------------------------- 1 | # 116 最低位排序 2 | 3 | ## 题目描述 4 | 5 | 给定一个非空数组(列表),其元素数据类型为整型,请按照数组元素十进制最低位从小到大进行排序,十进制最低位相同的元素,相对位置保持不变,当数组元素为负值时,十进制最低为等同于去除符号位后对应十进制值最低位。 6 | 7 | ## 输入描述 8 | 9 | 给定一个非空数组(列表),其元素数据类型为32位有符号整数,数组长度为[1,100]。 10 | 11 | ## 输出描述 12 | 13 | 输出排序后的数组。 14 | 15 | ## 示例描述 16 | 17 | ### 示例一 18 | 19 | **输入:** 20 | 21 | ```text 22 | 1,2,5,-21,22,11,55,-101,42,8,7,32 23 | ``` 24 | 25 | **输出:** 26 | 27 | ```text 28 | 1,-21,11,-101,2,22,42,32,5,55,7,8 29 | ``` 30 | 31 | ## 解题思路 32 | 33 | **基本思路:** 使用sort函数求解。 34 | 35 | 1. 对列表中的数字进行从小到大排序,排序方式是对数字取绝对值,并模10。 36 | 2. 返回结果列表。 37 | 38 | ## 解题代码 39 | 40 | ```python 41 | def solve_method(nums): 42 | nums.sort(key=lambda x: abs(x) % 10) 43 | return nums 44 | 45 | 46 | if __name__ == '__main__': 47 | arr = [1, 2, 5, -21, 22, 11, 55, -101, 42, 8, 7, 32] 48 | assert solve_method(arr) == [1, -21, 11, -101, 2, 22, 42, 32, 5, 55, 7, 8] 49 | ``` 50 | 51 | ## 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /docs/others100/119_maximum-permutation.md: -------------------------------------------------------------------------------- 1 | # 119 最大排列 2 | 3 | ## 题目描述 4 | 5 | 给定一组整数,重排序后输出一个最大的整数。 6 | 7 | ## 输入描述 8 | 9 | 输入一行数字组合,用空格分隔。 10 | 11 | ## 输出描述 12 | 13 | 输出一个数字,表示最大的整数。 14 | 15 | ## 示例描述 16 | 17 | ### 示例一 18 | 19 | **输入:** 20 | 21 | ```text 22 | 10 9 23 | ``` 24 | 25 | **输出:** 26 | 27 | ```text 28 | 910 29 | ``` 30 | 31 | ## 解题思路 32 | 33 | 1. 遍历字符串数组。 34 | 2. 再次遍历后面的字符串数组: 35 | - 比较交换前组成的数字和交换后组成的数字,如果交换后的较大,则交换两个字符串的位置。 36 | 3. 将排列好的字符串拼接成结果字符串,并返回。 37 | 38 | ## 解题代码 39 | 40 | ```python 41 | def solve_method(nums): 42 | length = len(nums) 43 | 44 | for i in range(length): 45 | for j in range(i + 1, length): 46 | if int(nums[i] + nums[j]) < int(nums[j] + nums[i]): 47 | nums[i], nums[j] = nums[j], nums[i] 48 | 49 | return int("".join(nums)) 50 | 51 | 52 | if __name__ == '__main__': 53 | assert solve_method(["10", "9"]) == 910 54 | ``` 55 | -------------------------------------------------------------------------------- /docs/others100/128_the-nearest-hospital.md: -------------------------------------------------------------------------------- 1 | # 128 最近的医院 2 | 3 | ## 题目描述 4 | 5 | 新型冠状病毒疫情的肆虐,使得家在武汉的大壮不得不思考自己家和附近定点医院的具体情况。经过一番调查,大壮明白了举例自己家最近的定点医院有两家。其中医院A距离自己X公里,医院B距离自己是Y公里。由于武汉封城,公交停运,私家车不能上路,交通十分不便。 6 | 7 | 现在达到医院A只能搭乘志愿者计程车,已知计程车的平均速度是M 米/分钟,上车平均等待时间是L分钟。到达医院B只能步行,平均速度是N 米/分钟; 8 | 9 | 给出X、Y、M、L、N的数据,请问大壮到达哪家医院最快? 10 | 11 | ## 输入描述 12 | 13 | 输入为一行数字,一共5个数字,分别是到达A医院的距离、到达B医院的距离、计程车平均速度、上车等待时间、步行速度。 14 | 15 | ## 输出描述 16 | 17 | 输出为一个字符串,计程车表示`Taxi`、步行表示`Walk`、相等表示`Same`。 18 | 19 | ## 示例描述 20 | 21 | ### 示例一 22 | 23 | **输入:** 24 | ```text 25 | 50 5 500 30 90 26 | ``` 27 | 28 | **输出:** 29 | ```text 30 | Walk 31 | ``` 32 | 33 | ## 解题思路 34 | 35 | 1. 计算计程车到达医院A的时间`tA`:`l + (x * 1000 / m)` 36 | 2. 计算步行到达医院B的时间`tB`:`y * 1000 / n` 37 | 3. 比较`tA`和`tB`的值,返回结果。 38 | 39 | ## 解题代码 40 | 41 | ```python 42 | def solve_method(x, y, m, l, n): 43 | tA = l + (x * 1000 / m) 44 | tB = y * 1000 / n 45 | 46 | if tA < tB: 47 | return "Taxi" 48 | elif tA > tB: 49 | return "Walk" 50 | else: 51 | return "Same" 52 | 53 | 54 | if __name__ == '__main__': 55 | assert solve_method(50, 5, 500, 30, 90) == "Walk" 56 | ``` -------------------------------------------------------------------------------- /docs/others100/132_the-longest-vowel-string.md: -------------------------------------------------------------------------------- 1 | # 132 最长的元音字符串 2 | 3 | ## 题目描述 4 | 5 | 定义当一个字符串只有元音字母(a、e、i、o、u、A、E、I、O、U)组成,称为元音字符串。现给定一个字符串,请找出其中最长的元音字符串,并返回其长度,如果找不到,请返回0。 6 | 7 | 注:字符串中任意一个连续字符串组成的子序列成为该字符串的子串。 8 | 9 | ## 输入描述 10 | 11 | 一个字符串,长度`length > 0`,字符串仅由字符a-z或A-Z组成。 12 | 13 | ## 输出描述 14 | 15 | 一个整数,表示最长的元音字符子串的长度 16 | 17 | ## 示例描述 18 | 19 | ### 示例一 20 | 21 | **输入:** 22 | ```text 23 | asdbuiodevauufgh 24 | ``` 25 | 26 | **输出:** 27 | ```text 28 | 3 29 | ``` 30 | 31 | **说明:** 32 | 最长的元音字符子串为`uio`和`auu`,长度都为3,因此输出3。 33 | 34 | ## 解题思路 35 | 36 | 1. 设置正则表达式`[aeiouAEIOU]+`。 37 | 2. 使用`findall`方法,找到所有符合正则的字符串。 38 | 3. 按照字符串长度从大到小排序。 39 | 4. 如果找到了,返回第一个字符串的长度,即为最长的元音字符串,如果没有找到,返回0。 40 | 41 | ## 解题代码 42 | 43 | ```python 44 | import re 45 | 46 | 47 | def solve_method(string): 48 | p = re.compile("[aeiouAEIOU]+") 49 | result = p.findall(string) 50 | result.sort(key=lambda x: len(x), reverse=True) 51 | return len(result[0]) if len(result) != 0 else 0 52 | 53 | 54 | if __name__ == '__main__': 55 | assert solve_method("asdbuiodevauufgh") == 3 56 | assert solve_method("fgh") == 0 57 | ``` -------------------------------------------------------------------------------- /docs/others100/153_sliding-window.md: -------------------------------------------------------------------------------- 1 | # 153 滑动窗口 2 | 3 | ## 题目描述 4 | 5 | 有一个`N`个整数的数组,和一个长度为`M`的窗口。窗口从数组内的第一个数开始滑动,直到窗口不能滑动为止。每次滑动产生一个窗口和窗口内所有数的和,求窗口滑动产生的所有窗口和的最大值。 6 | 7 | ## 输入描述 8 | 9 | 第一行输入一个正整数`N`,表示整数个数,取值范围是0 < N < 100000。 10 | 11 | 第二行输入`N`个整数,整数取值范围`[-100,100]`。 12 | 13 | 第三行输入正整数`N`,表示窗口的大小,取值范围是M <= N <= 100000。 14 | 15 | ## 输出描述 16 | 17 | 窗口滑动产生所有窗口和的最大值。 18 | 19 | ## 示例描述 20 | 21 | ### 示例一 22 | 23 | **输入:** 24 | 25 | ```text 26 | 6 27 | 12 10 20 30 15 23 28 | 3 29 | ``` 30 | 31 | **输出:** 32 | 33 | ```text 34 | 68 35 | ``` 36 | 37 | ## 解题思路 38 | 39 | 1. 当滑动窗口大于数组长度,则返回数组所有元素之和。 40 | 2. 遍历数组:计算滑动窗口内的和,并比较大小,取最大和。 41 | 3. 返回结果,即窗口滑动产生所有窗口和的最大值。 42 | 43 | ## 解题代码 44 | 45 | ```python 46 | import math 47 | 48 | 49 | def solve_method(nums, window_size): 50 | if window_size > len(nums): 51 | return sum(nums) 52 | 53 | max_sum = - math.inf 54 | for i in range(len(nums) - window_size + 1): 55 | max_sum = max(max_sum, sum(nums[i:i + window_size])) 56 | 57 | return max_sum 58 | 59 | 60 | if __name__ == '__main__': 61 | assert solve_method([12, 10, 20, 30, 15, 23], 3) == 68 62 | ``` 63 | 64 | -------------------------------------------------------------------------------- /docs/others100/185_spacecraft.md: -------------------------------------------------------------------------------- 1 | # 185 航天器 2 | 3 | ## 题目描述 4 | 5 | 给航天器一侧加装长方形和正方形的太阳能板,需要先安装两个支柱,再在支柱的中间部分固定太阳能板,但航天器不同位置的支柱长度不同,太阳能板的安装面积受限于最短一侧的那个支柱的长度。 6 | 7 | 现提供一组整型数组的支柱高度数据,假设每个支柱间的距离相等为一个单位长度,计算如何选择两根支柱可以使太阳能板的面积最大。 8 | 9 | ## 输入描述 10 | 11 | 输入一个数组,表示各个支柱的高度,其中支柱至少有两根,最多10000根,能支持的高度范围1 \~ 10^9的整数,柱子的高度是无序的。 12 | 13 | ## 输出描述 14 | 15 | 可以支持的最大太阳板面积。 16 | 17 | ## 示例描述 18 | 19 | ### 示例一 20 | 21 | **输入:** 22 | 23 | ```text 24 | 10,9,8,7,6,5,4,3,2,1 25 | ``` 26 | 27 | **输出:** 28 | 29 | ```text 30 | 25 31 | ``` 32 | 33 | **说明:** 34 | 35 | 0米高支柱和5米高支柱之间宽度为5,高度取小的支柱高度也是5,面积为25。 36 | 37 | 任取其他两根支柱所能获得的面积都小于25,所以最大面积为25。 38 | 39 | ## 解题思路 40 | 41 | 1. 遍历数组: 42 | - 计算两个数字的最小值的距离乘积。 43 | - 比较并得到最大值。 44 | 2. 返回最大值,即最大太阳板的面积。 45 | 46 | ## 解题代码 47 | 48 | ```python 49 | def solve_method(nums): 50 | result = 0 51 | for i in range(len(nums)): 52 | for j in range(i + 1, len(nums)): 53 | area = min(nums[i], nums[j]) * (j - i) 54 | result = max(result, area) 55 | 56 | return result 57 | 58 | 59 | if __name__ == '__main__': 60 | assert solve_method([10, 9, 8, 7, 6, 5, 4, 3, 2, 1]) == 25 61 | ``` 62 | 63 | -------------------------------------------------------------------------------- /docs/others100/199_height-sorting.md: -------------------------------------------------------------------------------- 1 | # 199 身高排序 2 | 3 | ## 题目描述 4 | 5 | 小明今年升学到了小学一年级,来到新班级后,发现其他小朋友身高参差不齐,然后就想基于各小朋友和自己的身高差,对他们进行排序,请帮他实现排序。 6 | 7 | ## 输入描述 8 | 9 | 第一行是正整数`H`和`N`,`H`表示小明的身高,取值范围是0 < H < 200。`N`为新班级其他小朋友个数,取值范围是0 < N < 50。 10 | 11 | 第二行是`N`个正整数,`H1 ~ Hn`分别是其他小朋友的身高,取值范围 0 < H[i] < 200, 且`N`个正整数各不相同。 12 | 13 | ## 输出描述 14 | 15 | 输出排序结果,各正整数以空格分隔,排序规则如下: 16 | 17 | - 和小明身高差绝对值最小的小朋友排在前面。 18 | - 和小明身高差绝对值最大的小朋友排在后面 19 | - 如果两个小朋友和小明身高差一样,则个子较小的小朋友排在前面 20 | 21 | ## 示例描述 22 | 23 | ### 示例一 24 | 25 | **输入:** 26 | 27 | ```text 28 | 100 10 29 | 95 96 97 98 99 101 102 103 104 105 30 | ``` 31 | 32 | **输出:** 33 | 34 | ```text 35 | 99 101 98 102 97 103 96 104 95 105 36 | ``` 37 | 38 | ## 解题思路 39 | 40 | 1. 将身高按照与小明身高的之差的绝对值从小到大进行排序。 41 | 2. 返回排序之后的结果。 42 | 43 | ## 解题代码 44 | 45 | ```python 46 | def solve_method(H, N, heights): 47 | heights.sort(key=lambda x: (abs(x - H), x)) 48 | return heights 49 | 50 | 51 | if __name__ == '__main__': 52 | heights = [95, 96, 97, 98, 99, 101, 102, 103, 104, 105] 53 | assert solve_method(100, 10, heights) == [99, 101, 98, 102, 97, 103, 96, 104, 95, 105] 54 | 55 | ``` 56 | 57 | -------------------------------------------------------------------------------- /docs/others100/202_continuous-substrings.md: -------------------------------------------------------------------------------- 1 | # 202 连续子串 2 | 3 | ## 题目描述 4 | 5 | 给定两个字符串`t`和`p`,要求从`t`中找到一个和`p`相同的连续子串,并输出该子串第一个字符的下标。 6 | 7 | ## 输入描述 8 | 9 | 输入包括两行,分别表示字符串`t`和`p`,保证`t`的长度不小于`p`,且`t`的长度不超过1000000,`p`的长度不超过10000。 10 | 11 | ## 输出描述 12 | 13 | 如果能从`t`中找到一个和`p`相等的连续子串,则输出该子串第一个字符串在`t`中的下标,下标从1开始。如果不能,则输出`No`,如果含有多个这样的子串,则输出第一个字符串下标最小的。 14 | 15 | ## 示例描述 16 | 17 | ### 示例一 18 | 19 | **输入:** 20 | ```text 21 | AVERDXIVYERDIAN 22 | RDXI 23 | ``` 24 | 25 | **输出:** 26 | ```text 27 | 4 28 | ``` 29 | 30 | ## 解题思路 31 | 32 | **基本思路:** 用滑动窗口的思想,实现`str`的`find`方法。 33 | 1. 设置滑动窗口大小`window_length`为字符串`p`的长度。 34 | 2. 按字符串`t`的长度逐个遍历窗口: 35 | - 获取滑动窗口中的子串`substring`。 36 | - 判断子串`substring`是否与字符串`p`相等,如果相等,返回下标。 37 | 3. 如果找不到,返回`No`。 38 | 39 | ## 解题代码 40 | ```python 41 | def solve_method(t, p): 42 | # 实现 t.find(p) + 1 43 | window_length = len(p) 44 | for i in range(len(t) - window_length + 1): 45 | substring = t[i:i + window_length] 46 | if substring == p: 47 | return i + 1 48 | 49 | return "No" 50 | 51 | 52 | if __name__ == '__main__': 53 | assert solve_method("AVERDXIVYERDIAN", "RDXI") == 4 54 | ``` -------------------------------------------------------------------------------- /docs/others100/212_collect-five-blessing-cards.md: -------------------------------------------------------------------------------- 1 | # 212 集五福 2 | 3 | ## 题目描述 4 | 5 | 集五福作为近年来大家喜闻乐见迎新春的或哦那个,收集爱国福、富强福、和谐福、友善福、敬业福,即可分享超大红包。 6 | 7 | 以0和1组成长度为5的字符串,代表每个人所得到的福卡,每一位代表一种福卡,1表示已经获得该福卡,单类型福卡不超过1张,随机抽取一个小于10人团队,求该团队最多可以集齐多少套五福? 8 | 9 | ## 输入描述 10 | 11 | 输入若干个`11010`、`00110`的由0和1组成的长度等于5位的字符串,代表团队中每个人福卡的获得情况。 12 | 13 | 注意: 14 | - 1人也可以是一个团队。 15 | - 1人可以有0到5张福卡,但福卡不能重复。 16 | 17 | ## 输出描述 18 | 19 | 输出该团队能凑齐多少套五福 20 | 21 | ## 示例描述 22 | 23 | ### 示例一 24 | 25 | **输入:** 26 | ```text 27 | 11001,11101 28 | ``` 29 | 30 | **输出:** 31 | ```text 32 | 0 33 | ``` 34 | 35 | ### 示例二 36 | 37 | **输入:** 38 | ```text 39 | 11101,10111 40 | ``` 41 | 42 | **输出:** 43 | ```text 44 | 1 45 | ``` 46 | 47 | ## 解题思路 48 | 49 | 1. 初始化结果列表,表示每种福卡的个数。 50 | 2. 遍历团队: 51 | - 遍历每人的福卡:如果为1,将该种福卡的个数加1 52 | 3. 将结果列表排序,返回里面的最小值 53 | 54 | ## 解题代码 55 | 56 | ```python 57 | def solve_method(arr): 58 | result = [0] * 5 59 | 60 | for cards in arr: 61 | for i in range(5): 62 | if cards[i] == "1": 63 | result[i] += 1 64 | 65 | return min(result) 66 | 67 | 68 | if __name__ == '__main__': 69 | assert solve_method(["11001", "11101"]) == 0 70 | assert solve_method(["11101", "10111"]) == 1 71 | ``` -------------------------------------------------------------------------------- /docs/others100/images/138_robot-mazes-eg2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/datawhalechina/huawei-od-python/98cb81c5e15df351f831b9dfc19caf0e5af1e18a/docs/others100/images/138_robot-mazes-eg2.png -------------------------------------------------------------------------------- /docs/others100/images/138_robots-mazes-demo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/datawhalechina/huawei-od-python/98cb81c5e15df351f831b9dfc19caf0e5af1e18a/docs/others100/images/138_robots-mazes-demo.png -------------------------------------------------------------------------------- /docs/others100/images/170_vertical-quad1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/datawhalechina/huawei-od-python/98cb81c5e15df351f831b9dfc19caf0e5af1e18a/docs/others100/images/170_vertical-quad1.png -------------------------------------------------------------------------------- /docs/others100/images/170_vertical-quad2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/datawhalechina/huawei-od-python/98cb81c5e15df351f831b9dfc19caf0e5af1e18a/docs/others100/images/170_vertical-quad2.png -------------------------------------------------------------------------------- /docs/others100/images/192_comment-sample1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/datawhalechina/huawei-od-python/98cb81c5e15df351f831b9dfc19caf0e5af1e18a/docs/others100/images/192_comment-sample1.png -------------------------------------------------------------------------------- /docs/questions200/images/001-001-sample1-analysis.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/datawhalechina/huawei-od-python/98cb81c5e15df351f831b9dfc19caf0e5af1e18a/docs/questions200/images/001-001-sample1-analysis.png -------------------------------------------------------------------------------- /docs/questions200/images/002-001-sample-analysis.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/datawhalechina/huawei-od-python/98cb81c5e15df351f831b9dfc19caf0e5af1e18a/docs/questions200/images/002-001-sample-analysis.png -------------------------------------------------------------------------------- /docs/questions200/images/003-001-sample-analysis.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/datawhalechina/huawei-od-python/98cb81c5e15df351f831b9dfc19caf0e5af1e18a/docs/questions200/images/003-001-sample-analysis.png -------------------------------------------------------------------------------- /docs/questions200/images/003-002-dfs-description.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/datawhalechina/huawei-od-python/98cb81c5e15df351f831b9dfc19caf0e5af1e18a/docs/questions200/images/003-002-dfs-description.png -------------------------------------------------------------------------------- /docs/questions200/images/003-003-debug.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/datawhalechina/huawei-od-python/98cb81c5e15df351f831b9dfc19caf0e5af1e18a/docs/questions200/images/003-003-debug.png -------------------------------------------------------------------------------- /docs/questions200/images/007-001-sample-analysis.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/datawhalechina/huawei-od-python/98cb81c5e15df351f831b9dfc19caf0e5af1e18a/docs/questions200/images/007-001-sample-analysis.png -------------------------------------------------------------------------------- /docs/questions200/images/009-001-description.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/datawhalechina/huawei-od-python/98cb81c5e15df351f831b9dfc19caf0e5af1e18a/docs/questions200/images/009-001-description.png -------------------------------------------------------------------------------- /docs/questions200/images/014-001-description.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/datawhalechina/huawei-od-python/98cb81c5e15df351f831b9dfc19caf0e5af1e18a/docs/questions200/images/014-001-description.png -------------------------------------------------------------------------------- /docs/questions200/images/020-001-description.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/datawhalechina/huawei-od-python/98cb81c5e15df351f831b9dfc19caf0e5af1e18a/docs/questions200/images/020-001-description.png -------------------------------------------------------------------------------- /docs/questions200/images/033-001-sample-analysis.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/datawhalechina/huawei-od-python/98cb81c5e15df351f831b9dfc19caf0e5af1e18a/docs/questions200/images/033-001-sample-analysis.png -------------------------------------------------------------------------------- /docs/questions200/images/035-001-sample-analysis.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/datawhalechina/huawei-od-python/98cb81c5e15df351f831b9dfc19caf0e5af1e18a/docs/questions200/images/035-001-sample-analysis.png -------------------------------------------------------------------------------- /docs/questions200/images/045-001-sample-analysis1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/datawhalechina/huawei-od-python/98cb81c5e15df351f831b9dfc19caf0e5af1e18a/docs/questions200/images/045-001-sample-analysis1.png -------------------------------------------------------------------------------- /docs/questions200/images/045-002-sample-analysis2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/datawhalechina/huawei-od-python/98cb81c5e15df351f831b9dfc19caf0e5af1e18a/docs/questions200/images/045-002-sample-analysis2.png -------------------------------------------------------------------------------- /docs/template.md: -------------------------------------------------------------------------------- 1 | # 2xx 标题 2 | 3 | ## 题目描述 4 | 5 | ## 输入描述 6 | 7 | ## 输出描述 8 | 9 | ## 示例描述 10 | 11 | ### 示例一 12 | 13 | **输入:** 14 | ```text 15 | 16 | ``` 17 | 18 | **输出:** 19 | ```text 20 | 21 | ``` 22 | 23 | **说明:** 24 | xxxxx 25 | 26 | ## 解题思路 27 | 28 | **基本思路:** xxxxx(注:如果存在基本思路,可编写) 29 | 1. xxxxx 30 | 2. xxxxx 31 | 3. xxxxx 32 | 4. 返回结果。 33 | 34 | ## 解题代码 -------------------------------------------------------------------------------- /images/qrcode.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/datawhalechina/huawei-od-python/98cb81c5e15df351f831b9dfc19caf0e5af1e18a/images/qrcode.jpeg --------------------------------------------------------------------------------